How to Call Go Functions from JavaScript Wasm

Web
Export Go functions with //export, compile to wasm, and call them via WebAssembly.instantiate in JavaScript.

You call Go functions from JavaScript by exporting them with //export and using WebAssembly.instantiate to load the module.

//export Add
func Add(a, b int) int {
    return a + b
}

func main() {}

Compile with GOOS=js GOARCH=wasm go build -o main.wasm main.go, then in JavaScript:

const wasm = await WebAssembly.instantiateStreaming(fetch('main.wasm'));
console.log(wasm.instance.exports.Add(2, 3)); // 5