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 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝