Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(wasm-languages): go updates #1403

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions content/wasm-languages/go-lang.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ As with any Go project, the first step is to create a `go.mod` file:
```
module github.com/fermyon/example-go

go 1.17
go 1.23
```

Since Spin has a Go SDK which is nice and easy to use, we'll fetch that and use it:
Since Spin has a [Go SDK](https://github.com/fermyon/spin-go-sdk) which is nice and easy to use, we'll fetch that and use it:

```console
$ go mod download github.com/fermyon/spin/sdk/go
$ go get github.com/fermyon/spin-go-sdk
```

Next, create a simple Go program named `main.go`:
Expand All @@ -71,11 +71,11 @@ import (
"fmt"
"net/http"

spin "github.com/fermyon/spin/sdk/go/http"
spinhttp "github.com/fermyon/spin-go-sdk/http"
)

func main() {
spin.HandleRequest(func(w http.ResponseWriter, r *http.Request) {
spinhttp.Handle(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, World!")
})
}
Expand All @@ -84,28 +84,32 @@ func main() {
When it comes to compiling, though, we will need to use TinyGo instead of Go. This particular set of flags has produced the best results for us:

```
tinygo build -wasm-abi=generic -target=wasi -gc=leaking -o main.wasm main.go
tinygo build -target=wasi -gc=leaking -o main.wasm main.go
```

The above will output a `main.wasm` file. A simple `spin.toml` for running the above in Spin looks like this:

```toml
spin_version = "1"
authors = ["Fermyon Engineering <[email protected]>"]
description = "Hello world app."
spin_manifest_version = 2

[application]
name = "spin-go-hello"
trigger = { type = "http", base = "/" }
version = "1.0.0"
description = "Hello world app."
authors = ["Fermyon Engineering <[email protected]>"]

[[component]]
id = "hello"
source = "main.wasm"
[component.trigger]
[[trigger.http]]
component = "hello"
route = "/"
# Spin components written in Go use the Wagi HTTP executor
executor = { type = "wagi" }

[component.hello]
source = "main.wasm"
[component.hello.build]
command = "tinygo build -target=wasi -gc=leaking -o main.wasm main.go"
```

> Note: we've set the `hello` component's build command to be the tinygo invocation above, so that `spin build` will run it from now on

From there, it's just a matter of using `spin up` to start the server. As usual, we can test using a web browser or Curl:

```console
Expand All @@ -118,6 +122,6 @@ Hello, World!
Here are some great resources:

- TinyGo has [a step-by-step walkthrough](https://tinygo.org/docs/guides/webassembly/) for building and running Go WebAssembly modules
- There are [instructions](https://spin.fermyon.dev/go-components/) and [examples](https://github.com/fermyon/spin-kitchensink)
- Get started quickly with [Yo-Wasm](https://github.com/deislabs/yo-wasm), which has support for Go as well as several other languages.
- There are [instructions](https://developer.fermyon.com/spin/go-components/) and [examples](https://github.com/fermyon/spin-go-sdk/tree/main/examples)
- Get started quickly with Spin templates for Go: e.g. `spin templates list --tag go` and `spin new -t http-go`
- A [short article](https://golangbot.com/webassembly-using-go/) on compiling to Go's "JS/Wasm" target
Loading