Can not using syscall/js


(Rich) #1

hello i want to using “syscall/js”,but i get a error when i build

flb.go:5:2: build constraints exclude all Go files in C:\Go\src\syscall\js

My go version is “go1.13.5 windows/amd64”

my code as follow:

package main

import (
	"fmt"
	"syscall/js"
)

func main() {
	var cb js.Func
	cb = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
		fmt.Println("button clicked")
		cb.Release() // release the function if the button will not be clicked again
		return nil
	})
	js.Global().Get("document").Call("getElementById", "myButton").Call("addEventListener", "click", cb)
}

please help me fix it , and i’m really sorry for my poor English


(Norbert Melzer) #2

How are you compiling?

Using that package will obviously work only when you compile to JavaScript or WASM.


(Rich) #3

hello , this is my build script

SET GOOS=js
SET GOARCH=wasm
go build  -o flb.wasm fib.go

and IDE have an error also please see the picture


(Norbert Melzer) #4

Tell the IDE to build for correct OS and ARCH, it probably tries to compile and analyze in the context of your host.

At least if building from the terminal like described above works without an error, then changing the IDEs target context should help.