Is it possible to call a Go WebAssembly function, other than main
, in Javascript?
Let me first show what I did. My Go functions are defined as follows:
package main
import "fmt"
func main() {
fmt.Println("it works!")
}
func add(a, b int) int {
return a + b
}
I can only invoke the main
function:
const go = new Go();
const data = await fetch("http://localhost:3333/main.wasm");
const result = await WebAssembly.instantiateStreaming(data, go.importObject);
go.run(result.instance);
which returns it works!
as expected.
However, whenever I try to invoke the add
function, I receive TypeError: Cannot read property 'add' of undefined at Welcome.getWasm
, because both result.exports
, result.instance.exports
do not contain my function. I also tried capitalizing the Go function, but at no avail.
Thus, I started wondering what could be a problem – is it even possible to call a random Go function from Javascript? Or can I only call the default main()
function?