Skip to content

Commit 53ea70d

Browse files
committed
Port Go build changes documentation
Signed-off-by: itowlson <[email protected]>
1 parent 25a61a2 commit 53ea70d

10 files changed

+11
-38
lines changed

content/v3/ai-sentiment-analysis-api-tutorial.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -736,9 +736,6 @@ func performSentimentAnalysis(w http.ResponseWriter, r *http.Request, ps spinhtt
736736
return
737737
}
738738
}
739-
740-
func main() {}
741-
742739
```
743740

744741
{{ blockEnd }}

content/v3/build.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ The build command calls TinyGo with the WASI backend and appropriate options:
135135

136136
```toml
137137
[component.hello.build]
138-
command = "tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go"
138+
command = "tinygo build -target=wasip1 -gc=leaking -buildmode=c-shared -no-debug -o main.wasm ."
139139
```
140140

141141
{{ blockEnd }}

content/v3/go-components.md

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Using TinyGo to compile components for Spin is currently required, as the
3030

3131
## Versions
3232

33-
TinyGo `0.30.0` is recommended, which requires Go `v1.19+`.
33+
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.
3434

3535
## HTTP Components
3636

@@ -61,16 +61,14 @@ func init() {
6161
fmt.Fprintln(w, "Hello Fermyon!")
6262
})
6363
}
64-
65-
func main() {}
6664
```
6765

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

7068
<!-- @selectiveCpy -->
7169

7270
```bash
73-
$ tinygo build -o main.wasm -target=wasi main.go
71+
$ tinygo build -target=wasip1 -gc=leaking -buildmode=c-shared -no-debug -o main.wasm .
7472
```
7573

7674
Once built, we can run our Spin HTTP component using the Spin up command:
@@ -141,16 +139,14 @@ func init() {
141139
}
142140
})
143141
}
144-
145-
func main() {}
146142
```
147143

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

150146
<!-- @selectiveCpy -->
151147

152148
```bash
153-
$ tinygo build -o main.wasm -target=wasi main.go
149+
$ tinygo build -target=wasip1 -gc=leaking -buildmode=c-shared -no-debug -o main.wasm .
154150
```
155151

156152
Before we can execute this component, we need to add the
@@ -228,9 +224,6 @@ func init() {
228224
return nil
229225
})
230226
}
231-
232-
// main function must be included for the compiler but is not executed.
233-
func main() {}
234227
```
235228

236229
The manifest for a Redis application must contain the address of the Redis instance. This is set at the application level:
@@ -255,7 +248,7 @@ component = "echo-message"
255248
[component.echo-message]
256249
source = "main.wasm"
257250
[component.echo-message.build]
258-
command = "tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go"
251+
command = "tinygo build -target=wasip1 -gc=leaking -buildmode=c-shared -no-debug -o main.wasm ."
259252
```
260253

261254
The application will connect to `redis://localhost:6379`, and for every new message
@@ -351,8 +344,6 @@ func init() {
351344
}
352345
})
353346
}
354-
355-
func main() {}
356347
```
357348

358349
As with all networking APIs, you must grant access to Redis hosts via the `allowed_outbound_hosts` field in the application manifest:

content/v3/http-trigger.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,6 @@ class IncomingHandler(http.IncomingHandler):
300300
301301
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.
302302

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

content/v3/key-value-store-tutorial.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,6 @@ func init() {
399399
}
400400
})
401401
}
402-
403-
func main() {}
404-
405402
```
406403

407404
{{ blockEnd }}

content/v3/quickstart.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ component = "hello-go"
628628
source = "main.wasm"
629629
allowed_outbound_hosts = []
630630
[component.hello-go.build]
631-
command = "tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go"
631+
command = "tinygo build -target=wasip1 -gc=leaking -buildmode=c-shared -no-debug -o main.wasm ."
632632
```
633633

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

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

668666
{{ blockEnd }}
@@ -817,7 +815,7 @@ You can always run this command manually; `spin build` is a shortcut.
817815

818816
```bash
819817
$ spin build
820-
Executing the build command for component hello-go: tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go
818+
Executing the build command for component hello-go: tinygo build -target=wasip1 -gc=leaking -buildmode=c-shared -no-debug -o main.wasm .
821819
go: downloading github.com/fermyon/spin/sdk/go v0.10.0
822820
Finished building all Spin components
823821
```
@@ -826,14 +824,14 @@ If the build fails, check:
826824

827825
* Are you in the `hello_go` directory?
828826
* Did you successfully [install TinyGo](#install-the-tools)?
829-
* Are your versions of Go and TinyGo up to date? The Spin SDK needs TinyGo 0.27 or above.
827+
* 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.
830828
* 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.)
831829

832830
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:
833831

834832
```toml
835833
[component.hello-go.build]
836-
command = "tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go"
834+
command = "tinygo build -target=wasip1 -gc=leaking -buildmode=c-shared -no-debug -o main.wasm ."
837835
```
838836

839837
You can always run this command manually; `spin build` is a shortcut to save you having to remember it.

content/v3/rdbms-storage.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,6 @@ func init() {
201201
json.NewEncoder(w).Encode(pets)
202202
})
203203
}
204-
205-
func main() {}
206204
```
207205

208206
{{ blockEnd }}

content/v3/redis-trigger.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,6 @@ func init() {
137137
return nil
138138
})
139139
}
140-
141-
func main() {}
142140
```
143141

144142
{{ blockEnd }}

content/v3/sqlite-api-guide.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,6 @@ func init() {
233233
json.NewEncoder(w).Encode(todos)
234234
})
235235
}
236-
237-
func main() {}
238236
```
239237

240238
**General Notes**

content/v3/variables.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,6 @@ func init() {
242242
fmt.Fprintln(w, "Used an API")
243243
})
244244
}
245-
246-
func main() {}
247245
```
248246

249247
{{ blockEnd }}

0 commit comments

Comments
 (0)