2

This is my first attempt to do Webassembly and I've bumped into an issue.

I'm using: go version go1.14.3 linux/amd64

The code compiles properly with:

GOARCH=wasm GOOS=js go build -o lib.wasm main.go

When I do: go run main.go

I get the following error:

main.go:8:2: build constraints exclude all Go files in /usr/local/go/src/syscall/js

Any ideas how to solve this?

package main import ( "flag" "log" "net/http" "strconv" "syscall/js" ) var ( listen = flag.String("listen", ":8080", "listen address") dir = flag.String("dir", ".", "directory to serve") ) func add(i []js.Value) { value1 := js.Global().Get("document").Call("getElementById", i[0].String()).Get("value").String() value2 := js.Global().Get("document").Call("getElementById", i[1].String()).Get("value").String() int1, _ := strconv.Atoi(value1) int2, _ := strconv.Atoi(value2) js.Global().Get("document").Call("getElementById", i[2].String()).Set("value", int1+int2) } func subtract(i []js.Value) { value1 := js.Global().Get("document").Call("getElementById", i[0].String()).Get("value").String() value2 := js.Global().Get("document").Call("getElementById", i[1].String()).Get("value").String() int1, _ := strconv.Atoi(value1) int2, _ := strconv.Atoi(value2) js.Global().Get("document").Call("getElementById", i[2].String()).Set("value", int1-int2) } func registerCallbacks() { js.Global().Set("add", new(func())) js.Global().Set("subtract", new(func())) //js.Global().Set("subtract", js.NewCallback(subtract)) //cannot use add (type func([]js.Value)) as type func(js.Value, []js.Value) interface {} in argument to js.FuncOf //js.Global().Set("add", js.FuncOf(add)) //js.Global().Set("subtract", js.FuncOf(subtract)) } func main() { flag.Parse() log.Printf("listening on %q...", *listen) log.Fatal(http.ListenAndServe(*listen, http.FileServer(http.Dir(*dir)))) c := make(chan struct{}, 0) println("WASM Go Initialized") // register functions registerCallbacks() <-c } 
3
  • 1
    Don't use go run. (Why do you want to use go run?) Commented May 20, 2020 at 1:25
  • This go run main.go does more harm than it does good. It looks innocent but isn't Commented May 20, 2020 at 5:36
  • As the accepted answer below pointed out, there is a way to get 'GOOS=js GOARCH=wasm go run main.go' to work. Use a tool like wasmbrowsertest to put a go_js_wasm_exec binary in the PATH and 'go run' and 'go test' will pick it up and use it to pass the wasm file to chromedp. Makes getting a test result feel instantaneous. Commented Oct 14, 2020 at 0:44

1 Answer 1

4

You can't use go run with GOARCH=wasm; go run executes the built binary and because it is not OS native binary but wasm it will end up with operating system not understanding format of it.

Instructions how to execute WebAssembly binaries both directly using wasm_exec.js shim directly and Node.js are written down on WebAssembly wiki page of golang/go repository.

To run tests in browser there is nice Golang package agnivade/wasmbrowsertest.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reference to the wasmbrowsertest. It is super useful. After following its install instructions, 'GOOS=js GOARCH=wasm go run main.go' did work - because it uses a trick of 'go run' to find an executable that matches the name go_js_wasm_exec to pass the wasm to chromedp. Seems like magic until one has read a little bit about it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.