Skip to content

Commit b0db20b

Browse files
authored
feat: announce the new wit-bindgen-go tool (bytecodealliance#1072)
Signed-off-by: Jiaxiao Zhou (Mossaka) <duibao55328@gmail.com>
1 parent 20d33b5 commit b0db20b

File tree

1 file changed

+7
-52
lines changed

1 file changed

+7
-52
lines changed

README.md

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -318,65 +318,20 @@ e.g. Java, Kotlin, Clojure, Scala, etc.
318318

319319
### Guest: TinyGo
320320

321-
You can compile Go code into a Wasm module using the [TinyGo](https://tinygo.org/) compiler. For example, the following command compiles `main.go` to a WASI module:
321+
The **new** TinyGo WIT bindings generator is currently in development at the
322+
[wasm-tools-go](https://github.com/bytecodealliance/wasm-tools-go) repository.
322323

323-
`tinygo build -target=wasi main.go`
324-
325-
> Note: the current TinyGo `bindgen` requires TinyGo version v0.27.0 or later.
326-
327-
When using `wit-bindgen tiny-go` bindgen, `*.go` and `*.h` C header file are generated for your project. These files are generated with the [`wit-bindgen` CLI command][cli-install] in this repository.
324+
To install the `wit-bindgen-go` CLI, run:
328325

329326
```sh
330-
wit-bindgen tiny-go ./wit
331-
# Generating "host.go"
332-
# Generating "host.c"
333-
# Generating "host.h"
334-
# Generating "host_component_type.o"
335-
```
336-
337-
If your Go code uses `result` or `option` type, an additional Go file `host_types.go` will be generated. This file contains the Go types that correspond to the `result` and `option` types in the WIT file.
338-
339-
An example of using the generated Go code would look like:
340-
341-
Initialize Go:
342-
```bash
343-
go mod init example.com
344-
```
345-
346-
Create your Go main file:
347-
348-
```go
349-
// my-component.go
350-
package main
351-
352-
import (
353-
api "example.com/api"
354-
)
355-
356-
func init() {
357-
a := HostImpl{}
358-
api.SetHost(a)
359-
}
360-
361-
type HostImpl struct {
362-
}
363-
364-
func (e HostImpl) Run() {
365-
api.HostPrint("Hello, world!")
366-
}
367-
368-
//go:generate wit-bindgen tiny-go wit --out-dir=api
369-
func main() {}
327+
go install github.com/bytecodealliance/wasm-tools-go/cmd/wit-bindgen-go
370328
```
329+
> Note: it requires `wasm-tools` to be installed.
371330
372-
This setup allows you to invoke `go generate`, which generates the bindings for the Go code into an `api` directory. Afterward, you can compile your Go code into a WASI module using the TinyGo compiler. Lastly you can componentize the module using `wasm-tools`:
331+
Then, you can generate the bindings for your project:
373332

374333
```sh
375-
go generate # generate bindings for Go
376-
tinygo build -target=wasi -o main.wasm my-component.go # compile
377-
wasm-tools component embed --world host ./wit main.wasm -o main.embed.wasm # create a component
378-
wasm-tools component new main.embed.wasm --adapt wasi_snapshot_preview1.command.wasm -o main.component.wasm
379-
wasm-tools validate main.component.wasm --features component-model
334+
wit-bindgen-go generate <path-to-wit-pkg>
380335
```
381336

382337
### Guest: MoonBit

0 commit comments

Comments
 (0)