Skip to content

Commit

Permalink
Go components no longer need main
Browse files Browse the repository at this point in the history
Signed-off-by: itowlson <[email protected]>
  • Loading branch information
itowlson committed Jan 26, 2025
1 parent d868b37 commit 8fdec56
Show file tree
Hide file tree
Showing 10 changed files with 9 additions and 38 deletions.
3 changes: 0 additions & 3 deletions content/spin/v3/ai-sentiment-analysis-api-tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -757,9 +757,6 @@ func performSentimentAnalysis(w http.ResponseWriter, r *http.Request, ps spinhtt
return
}
}

func main() {}

```

{{ blockEnd }}
Expand Down
2 changes: 1 addition & 1 deletion content/spin/v3/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ The build command calls TinyGo with the WASI backend and appropriate options:

```toml
[component.hello.build]
command = "tinygo build -target=wasip1 -gc=leaking -scheduler=none -buildmode=c-shared -no-debug -o main.wasm ."
command = "tinygo build -target=wasip1 -gc=leaking -buildmode=c-shared -no-debug -o main.wasm ."
```

{{ blockEnd }}
Expand Down
15 changes: 3 additions & 12 deletions content/spin/v3/go-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,14 @@ func init() {
fmt.Fprintln(w, "Hello Fermyon!")
})
}

func main() {}
```

The Spin HTTP component (written in Go) can be built using the `tingygo` toolchain:

<!-- @selectiveCpy -->

```bash
$ tinygo build -target=wasip1 -gc=leaking -scheduler=none -buildmode=c-shared -no-debug -o main.wasm .
$ tinygo build -target=wasip1 -gc=leaking -buildmode=c-shared -no-debug -o main.wasm .
```

Once built, we can run our Spin HTTP component using the Spin up command:
Expand Down Expand Up @@ -141,16 +139,14 @@ func init() {
}
})
}

func main() {}
```

The Outbound HTTP Request example above can be built using the `tingygo` toolchain:

<!-- @selectiveCpy -->

```bash
$ tinygo build -target=wasip1 -gc=leaking -scheduler=none -buildmode=c-shared -no-debug -o main.wasm .
$ tinygo build -target=wasip1 -gc=leaking -buildmode=c-shared -no-debug -o main.wasm .
```

Before we can execute this component, we need to add the
Expand Down Expand Up @@ -228,9 +224,6 @@ func init() {
return nil
})
}

// main function must be included for the compiler but is not executed.
func main() {}
```

The manifest for a Redis application must contain the address of the Redis instance. This is set at the application level:
Expand All @@ -255,7 +248,7 @@ component = "echo-message"
[component.echo-message]
source = "main.wasm"
[component.echo-message.build]
command = "tinygo build -target=wasip1 -gc=leaking -scheduler=none -buildmode=c-shared -no-debug -o main.wasm ."
command = "tinygo build -target=wasip1 -gc=leaking -buildmode=c-shared -no-debug -o main.wasm ."
```

The application will connect to `redis://localhost:6379`, and for every new message
Expand Down Expand Up @@ -351,8 +344,6 @@ func init() {
}
})
}

func main() {}
```

As with all networking APIs, you must grant access to Redis hosts via the `allowed_outbound_hosts` field in the application manifest:
Expand Down
4 changes: 0 additions & 4 deletions content/spin/v3/http-trigger.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,6 @@ class IncomingHandler(http.IncomingHandler):
In Go, you register the handler as a callback in your program's `init` function. Call `spinhttp.Handle`, passing your handler as the sole argument. Your handler takes a `http.Request` record, from the standard `net/http` package, and a `ResponseWriter` to construct the response.

> The do-nothing `main` function is required by TinyGo but is not used; the action happens in the `init` function and handler callback.
```go
package main

Expand All @@ -312,8 +310,6 @@ func init() {
fmt.Fprintln(w, "Hello Fermyon!")
})
}

func main() {}
```

> If you are moving between languages, note that in most other Spin SDKs, your handler _constructs and returns_ a response, but in Go, _Spin_ constructs a `ResponseWriter`, and you write to it; your handler does not return a value.
Expand Down
3 changes: 0 additions & 3 deletions content/spin/v3/key-value-store-tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,6 @@ func init() {
}
})
}

func main() {}

```

{{ blockEnd }}
Expand Down
12 changes: 5 additions & 7 deletions content/spin/v3/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ component = "hello-go"
source = "main.wasm"
allowed_outbound_hosts = []
[component.hello-go.build]
command = "tinygo build -target=wasip1 -gc=leaking -scheduler=none -buildmode=c-shared -no-debug -o main.wasm ."
command = "tinygo build -target=wasip1 -gc=leaking -buildmode=c-shared -no-debug -o main.wasm ."
```

This represents a simple Spin HTTP application (triggered by an HTTP request). It has:
Expand All @@ -635,8 +635,8 @@ This represents a simple Spin HTTP application (triggered by an HTTP request).
[Learn more about the manifest here.](./writing-apps)

Now let's have a look at the code. Below is the complete source
code for a Spin HTTP component written in Go. Notice where the work is done. The
`main` function is empty (and Spin never calls it). Instead, the `init` function
code for a Spin HTTP component written in Go. Notice where the work is done. Because this is a component
rather than an application, there is no `main` function. Instead, the `init` function
sets up a callback, and passes that callback to `spinhttp.Handle` to register it as
the handler for HTTP requests. You can learn more about this structure
in the [Go language guide](go-components).
Expand All @@ -657,8 +657,6 @@ func init() {
fmt.Fprintln(w, "Hello Fermyon!")
})
}

func main() {}
```

{{ blockEnd }}
Expand Down Expand Up @@ -822,7 +820,7 @@ You can always run this command manually; `spin build` is a shortcut.

```bash
$ spin build
Executing the build command for component hello-go: tinygo build -target=wasip1 -gc=leaking -scheduler=none -buildmode=c-shared -no-debug -o main.wasm .
Executing the build command for component hello-go: tinygo build -target=wasip1 -gc=leaking -buildmode=c-shared -no-debug -o main.wasm .
go: downloading github.com/fermyon/spin/sdk/go v0.10.0
Finished building all Spin components
```
Expand All @@ -838,7 +836,7 @@ If you would like to know what build command Spin runs for a component, you can

```toml
[component.hello-go.build]
command = "tinygo build -target=wasip1 -gc=leaking -scheduler=none -buildmode=c-shared -no-debug -o main.wasm ."
command = "tinygo build -target=wasip1 -gc=leaking -buildmode=c-shared -no-debug -o main.wasm ."
```

You can always run this command manually; `spin build` is a shortcut to save you having to remember it.
Expand Down
2 changes: 0 additions & 2 deletions content/spin/v3/rdbms-storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,6 @@ func init() {
json.NewEncoder(w).Encode(pets)
})
}

func main() {}
```

{{ blockEnd }}
Expand Down
2 changes: 0 additions & 2 deletions content/spin/v3/redis-trigger.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ func init() {
return nil
})
}

func main() {}
```

{{ blockEnd }}
Expand Down
2 changes: 0 additions & 2 deletions content/spin/v3/sqlite-api-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,6 @@ func init() {
json.NewEncoder(w).Encode(todos)
})
}

func main() {}
```

**General Notes**
Expand Down
2 changes: 0 additions & 2 deletions content/spin/v3/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,6 @@ func init() {
fmt.Fprintln(w, "Used an API")
})
}

func main() {}
```

{{ blockEnd }}
Expand Down

0 comments on commit 8fdec56

Please sign in to comment.