如何在错误后保持 Golang Web 装配实例运行并退出 (代码:2)

我有这个程序使用代码将字节从JavaScript发送到戈朗网络组件:


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

    if len(args) < 1 {

        return 0

    }

    array := args[0]

    fmt.Println(array.Type())


    buf := make([]byte, array.Length())

    n := js.CopyBytesToGo(buf, array)

    fmt.Println(buf)

    return n

}


func registerCallbacks() {

    js.Global().Set("sendBytes", js.FuncOf(sendBytes))

}

当我使用其他内容执行时,我会在浏览器中杀死我的整个实例,我必须再次运行它。这是我如何执行发送字节()的屏幕截图。sendBytesUint8Array 

http://img4.mukewang.com/633bf81e0001ba5d08490527.jpg

无论发生什么错误,我该如何保持其运行?


慕尼黑5688855
浏览 143回答 1
1回答

12345678_0001

根据你的截图,当你把一个数字传递给 时,代码行会导致恐慌,然后WASM代码就会退出。如您所知,不应在号码上调用。sendBytes()array.Length()sendBytes()如果您确实需要它来保持运行,则可以在Go中使用恢复,类似于:func sendBytes(this js.Value, args []js.Value) interface{} {&nbsp; &nbsp; defer func() {&nbsp; &nbsp; &nbsp; &nbsp; if r := recover(); r != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Recovered from", r)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }()&nbsp;&nbsp; &nbsp; if len(args) < 1 {&nbsp; &nbsp; &nbsp; &nbsp; return 0&nbsp; &nbsp; }&nbsp; &nbsp; // ...}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go