mirror of
https://github.com/wazero/wazero
synced 2026-06-21 14:12:37 +00:00
8358482d4a
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
TinyGo allocation example
This example shows how to pass strings in and out of a Wasm function defined in TinyGo, built with
(cd testdata; tinygo build -scheduler=none -target=wasip1 -buildmode=c-shared -o greet.wasm greet.go)
$ go run greet.go wazero
wasm >> Hello, wazero!
go >> Hello, wazero!
Under the covers, greet.go does a few things of interest:
- Uses
unsafe.Pointerto change a Go pointer to a numeric type. - Uses
reflect.StringHeaderto build back a string from a pointer, len pair. - Relies on CGO to allocate memory used to pass data from TinyGo to host.
See https://wazero.io/languages/tinygo/ for more tips.