Skip to content

Instantly share code, notes, and snippets.

@flavio
Last active June 6, 2023 10:00
Show Gist options
  • Save flavio/f24fc3f349c09ac134e2e130109488e9 to your computer and use it in GitHub Desktop.
Save flavio/f24fc3f349c09ac134e2e130109488e9 to your computer and use it in GitHub Desktop.
logrus and WASI

This is a demo project showing how the smallest Go program using logrus can be compiled to the WASI target using both TinyGo and Go 1.21 (not yet released)

Requirements

  • wasmtime cli
  • docker
  • gotip

Demo

Build and run using tinygo:

make demo-tinygo.wasm

Build and run using go official compiler:

make demo-go.wasm
module github.com/flavio/logrus-demo
go 1.20
replace github.com/sirupsen/logrus => github.com/flavio/logrus v0.0.0-wasi-p1
require github.com/sirupsen/logrus v1.9.3
require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/flavio/logrus v0.0.0-wasi-p1 h1:soiCY4FPSpIfTS9TIlSEJXo0LyJZ+dKTgAPx9wyq8vk=
github.com/flavio/logrus v0.0.0-wasi-p1/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
package main
import (
log "github.com/sirupsen/logrus"
)
func main() {
log.WithFields(log.Fields{
"animal": "walrus",
}).Info("A walrus appears")
}
SOURCE_FILES := $(shell find . -type f -name '*.go')
demo-go.wasm: $(SOURCE_FILES) go.mod go.sum
GOOS=wasip1 GOARCH=wasm gotip build -o demo-go.wasm
wasmtime run demo-go.wasm
demo-tinygo.wasm: $(SOURCE_FILES) go.mod go.sum
docker run \
--rm \
-e GOFLAGS="-buildvcs=false" \
-v ${PWD}:/src \
-w /src tinygo/tinygo:0.27.0 \
tinygo build -o demo-tinygo.wasm -target=wasi -no-debug .
wasmtime run demo-tinygo.wasm
clean:
rm *.wasm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment