1

I'm using "syscall/js" to export a Go-WebAssembly function to JavaScript. The function in Go is required to be like this:

func x ( this js.Value, args []js.Value ) interface{}

Now I want to pass a 2-d array from JavaScript to Go. I receive it ( which is [][]byte in JavaScript ) as args[0], it is simply a js.Value which don't support indexing like args[0][i], so I can't process it further like

buffer := make([][]byte, args[0].Length())
for i:=0; i<len(buffer); i++{
    js.CopyBytesToGo(buffer[i], args[0][i])
}

The Error info will be:

invalid operation: args[0][i] (type js.Value does not support indexing)

What can I do to receive args[0] properly as a 2-d array []js.Value? I've tried to receive args[0] with an interface{} but it won't work.

Xavier Yu
  • 11
  • 1

1 Answers1

0

Well, I've found the answer myself, according to the syscall/js package document. The solution is to use args[0].Index(int i) as the js.Value 1-d array that I want. Thanks for viewing my question.

Xavier Yu
  • 11
  • 1
  • I agree that this information is not easily findable; over the past weeks I've struggled with questions like this. I just finished a writeup on this, including examples: https://blog.claude.nl/tech/howto/interface-between-go-1.16-and-javascript-syscall-js/ ; hopefully of some use to you or others. – Claude Aug 05 '21 at 11:38