dqzuo0327 2019-04-05 13:23
浏览 79
已采纳

无法在js函数调用的go函数中返回数组。 恐慌:ValueOf:无效值

There is a gocode, which is compiled into a wasm file. I want one of the functions to return an array, but when I do so I see a panic: ValueOf: invalid value error. js.ValueOf function seems to be able to handle an array:

...
case []interface{}:
    a := arrayConstructor.New(len(x))
    for i, s := range x {
        a.SetIndex(i, s)
    }
    return a
...

but still panics, when I give it a []int value.

package main

import (
    "fmt"
    "syscall/js"
)

var signal = make(chan int)

func keepAlive() {
    for {
        <-signal
    }
}

func main() {
    js.Global().Set("PanicStation", js.FuncOf(PanicStation))
    keepAlive()
}

func PanicStation(this js.Value, args []js.Value) interface{} {
    arr := make([]int, 1)
    return arr
}
  • 写回答

1条回答 默认 最新

  • duanjia4097 2019-04-05 13:53
    关注

    Using []interface{}

    []int is not the same as []interface{}. If []interface{} suits you, create and return that:

    arr := make([]interface{}, 1)
    

    For example, if you return this slice:

    func PanicStation(this js.Value, args []js.Value) interface{} {
        return []interface{}{1, "two"}
    }
    

    Running PanicStation() in the JavaScript console will output:

    > PanicStation()
    > (2) [1, "two"]
    

    Returning a typed array

    Documentation of js.ValueOf() details what types are supported and how they are converted:

    | Go                     | JavaScript             |
    | ---------------------- | ---------------------- |
    | js.Value               | [its value]            |
    | js.TypedArray          | typed array            |
    | js.Func                | function               |
    | nil                    | null                   |
    | bool                   | boolean                |
    | integers and floats    | number                 |
    | string                 | string                 |
    | []interface{}          | new array              |
    | map[string]interface{} | new object             |
    

    Note that it is possible to return a value which will be a typed array in JavaScript. For that, use js.TypedArray in Go (obtained by js.TypedArrayOf()). The supported types are:

    The supported types are []int8, []int16, []int32, []uint8, []uint16, []uint32, []float32 and []float64. Passing an unsupported value causes a panic.

    Here's an example how to do it:

    func PanicStation(this js.Value, args []js.Value) interface{} {
        return js.TypedArrayOf([]int32{1, 2})
    }
    

    This time calling PanicStation() from JavaScript, the output will be:

    > PanicStation()
    > Int32Array(2) [1, 2]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?