Skip to content
Documentation
Write StreamFunction in Go

Implement StreamFunction in Golang (opens in a new tab)

🚧

This feature is currently in alpha and subject to change.

Install CLI

$ curl -fsSL "https://get.yomo.run" | sh

Install TinyGo (opens in a new tab)

on Mac:

$ brew tap tinygo-org/tools
$ brew install tinygo

Write a StreamFunction in Go

package main
 
import (
	"fmt"
	"strings"
 
	"github.com/yomorun/yomo/serverless"
	"github.com/yomorun/yomo/serverless/guest"
)
 
func main() {
	guest.DataTags = DataTags
	guest.Handler = Handler
	guest.Init = Init
}
 
// Init will initialize the stream function
func Init() error {
	fmt.Println("wasm go sfn init")
	return nil
}
 
func Handler(ctx serverless.Context) {
	// load input data
	tag := ctx.Tag()
	input := ctx.Data()
	fmt.Printf("wasm go sfn received %d bytes with tag[%#x]\n", len(input), tag)
 
	// process app data
	output := strings.ToUpper(string(input))
 
	// write result to zipper
	ctx.Write(0x34, []byte(output))
}
 
func DataTags() []uint32 {
	return []uint32{0x33}
}

Compile to WASI (opens in a new tab)

$ tinygo build -o sfn.wasm -no-debug -target wasi

Run Streaming Serverless Function

yomo run /path/to/sfn.wasm