Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to access JS Objects in Go through Web assembly

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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().

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading