Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

384
Visualizações
How to keep Golang WebAssembly Instance Running after error and Exit (Code:2)

I have this program to send bytes from JavaScript to Golang WebAssembly using the code:

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))
}

When I execute sendBytes with something else than Uint8Array, I am getting my whole instance killed in the browser and I have to run it again. This is screenshot of how I execute sendBytes(). enter image description here

How can I keep it running no matter what error happened?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

According to your screenshot, when you passed a number to sendBytes(), the line of code array.Length() would lead to a panic, then the WASM code would exit. As you know sendBytes() should not be called on a number.

If you indeed need it to keep running, you can use recover in Go, similar to:

func sendBytes(this js.Value, args []js.Value) interface{} {
    defer func() {
        if r := recover(); r != nil {
            fmt.Println("Recovered from", r)
        }
    }() 

    if len(args) < 1 {
        return 0
    }
    // ...
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda