Home > Net >  How to access JS Objects in Go through Web assembly
How to access JS Objects in Go through Web assembly

Time:02-16

I am using to go build wasm file to run on my browser ,I am able to pass the simple integer string values to the method but not able to pass complex objects ,key value pairs or array

This is my go method

func Transform(jsV js.Value, inputs []js.Value) interface{} {
    message := inputs[0].String()
    fmt.Println(inputs)  // How to access objects here 
    h := js.Global().Get("document").Call("getElementById", "message")
    h.Set("textContent", message)
    return nil
}

func init() {
    fmt.Println("Hello, WebAssembly!")
    c = make(chan bool)
}

func main() {
    js.Global().Set("Transform", js.FuncOf(Transform))
    println("Done.. done.. done...")
    <-c
}

When I am passing object like {name:"Something"} it only prints object ,I have searched in docs but unable to find any link

CodePudding user response:

If you are calling Transform as:

globalThis.Transform({name:"Something"})

In that case the inputs[0] is the Object. In order to get the name attribute you should use Get:

message := inputs[0].Get("name").String()

In case of array you have .Index() and for object (as show above) you have .Get().

  • Related