TL;DR
Compiling Go to WebAssembly (WASM) for browser execution typically involves setting GOOS=js, but this approach fails in isolated environments. Instead, using GOOS=wasip1 allows for proper execution in isolated-vm, which lacks a DOM and event loop.
✦ Why It Matters
Engineers can implement GOOS=wasip1 to enable Go code execution in isolated environments without hanging issues.
Key Takeaways
Full Summary
dailyprog, a coding puzzle site, initially supported JavaScript, Python, and C, each requiring unique setups for browser execution. For Go, the common method involves compiling with GOOS=js and GOARCH=wasm, utilizing a JavaScript syscall bridge and a support file called wasm_exec.js.
However, dailyprog executes user-submitted code in isolated-vm, a V8 isolate that does not support the standard asynchronous WebAssembly instantiation process. This leads to hanging programs since the Promise from async instantiation never resolves.
By switching to GOOS=wasip1, the Go code can run effectively in this restricted environment. This approach not only resolves the hanging issue but also opens up new possibilities for running Go in isolated contexts.
Related