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

Update TinyGo docs to reflect statics fix #1451

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
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=wasi -gc=leaking -no-debug -o main.wasm main.go"
command = "tinygo build -target=wasip1 -gc=leaking -scheduler=none -buildmode=c-shared -no-debug -o main.wasm ."
```

{{ blockEnd }}
Expand Down
17 changes: 4 additions & 13 deletions content/spin/v3/go-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Using TinyGo to compile components for Spin is currently required, as the

## Versions

TinyGo `0.30.0` is recommended, which requires Go `v1.19+`.
TinyGo `0.35.0` is recommended, which requires Go `v1.22+`. Older versions of TinyGo may not support the command-line flags used when building Spin applications.

## HTTP Components

Expand Down 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 -o main.wasm -target=wasi main.go
$ tinygo build -target=wasip1 -gc=leaking -scheduler=none -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 -o main.wasm -target=wasi main.go
$ tinygo build -target=wasip1 -gc=leaking -scheduler=none -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=wasi -gc=leaking -no-debug -o main.wasm main.go"
command = "tinygo build -target=wasip1 -gc=leaking -scheduler=none -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
14 changes: 6 additions & 8 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=wasi -gc=leaking -no-debug -o main.wasm main.go"
command = "tinygo build -target=wasip1 -gc=leaking -scheduler=none -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=wasi -gc=leaking -no-debug -o main.wasm main.go
Executing the build command for component hello-go: tinygo build -target=wasip1 -gc=leaking -scheduler=none -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 @@ -831,14 +829,14 @@ If the build fails, check:

* Are you in the `hello_go` directory?
* Did you successfully [install TinyGo](#install-the-tools)?
* Are your versions of Go and TinyGo up to date? The Spin SDK needs TinyGo 0.27 or above.
* Are your versions of Go and TinyGo up to date? The Spin SDK needs TinyGo 0.35 or above and Go 1.22 or above.
* Set Environment Variable `CGO_ENABLED=1`. (Since the Go SDK is built using CGO, it requires the CGO_ENABLED=1 environment variable to be set.)

If you would like to know what build command Spin runs for a component, you can find it in the manifest, in the `component.(id).build` section:

```toml
[component.hello-go.build]
command = "tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go"
command = "tinygo build -target=wasip1 -gc=leaking -scheduler=none -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
Loading