Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

376
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!