1

I am trying to import a function from my golang WASM file to use in javascript but anytime i try and use the function i get a undefined error. But if i import my WASM file and then use the console i can manually call the function. I need to be able to call it within the js program. Thank's!

Browser Console Error

Uncaught ReferenceError: add is not defined

JS

    async function init() {
        const go = new Go();
        const result = await WebAssembly.instantiateStreaming(
          fetch("main.wasm"),
          go.importObject
        );
        go.run(result.instance);
    }

    add("Hello")

    init();

GOLANG

package main

import (
    "fmt"
    "syscall/js"
)

func add(this js.Value, inputs []js.Value) interface{} {
    var input string = inputs[0].String()

    return "you typed: " + input
}

func main() {
    fmt.Println("hi")
    js.Global().Set("add", js.FuncOf(add))

    <-make(chan bool)
}

1 Answer 1

1

I figured out a solution IDK if it is the most effective but i defiantly works. If you just make another function and use your WASM functions in it but call it right after everything in the init func it will work.

    function alt() {
    var h = add("Hello World")
    console.log(h)
}

async function init() {
    const go = new Go();
    const result = await WebAssembly.instantiateStreaming(
    fetch("main.wasm"), go.importObject);
    
    var wasmModule = result.instance;
    go.run(wasmModule);
    console.log(wasmModule.exports)
    alt()
}

init();

Not the answer you're looking for? Browse other questions tagged or ask your own question.