September 14th, 2024

Notes on running Go in the browser with WebAssembly

Eli Bendersky's article explores integrating Go with WebAssembly, showcasing examples of calling Go functions from JavaScript, using TinyGo for smaller binaries, and employing Web Workers to maintain UI responsiveness.

Read original articleLink Icon
Notes on running Go in the browser with WebAssembly

Eli Bendersky's article discusses the integration of Go with WebAssembly (WASM) for running Go code in web browsers. He highlights the potential of WebAssembly for executing existing Go projects in the browser and provides practical examples of how to call Go functions from JavaScript. The article outlines a basic setup for invoking Go functions, such as calculating the harmonic series, and demonstrates how to manipulate the Document Object Model (DOM) using Go. Bendersky also introduces TinyGo, a lightweight alternative to the standard Go compiler, which produces smaller WASM binaries but comes with some limitations. Additionally, he addresses the issue of blocking the main thread during long computations and suggests using Web Workers to run Go code in a separate thread, allowing the UI to remain responsive. The article includes code snippets and links to a GitHub repository for further exploration of the examples provided.

- WebAssembly allows Go code to run in web browsers, enabling the use of existing Go projects.

- The article provides examples of calling Go functions from JavaScript and manipulating the DOM using Go.

- TinyGo is introduced as a lightweight alternative for compiling Go to WebAssembly, producing smaller binaries.

- Using Web Workers can prevent UI blocking during long-running Go computations in the browser.

- The article includes practical code examples and links to a GitHub repository for further learning.

Related

Dynamic Translation of Smalltalk to WebAssembly

Dynamic Translation of Smalltalk to WebAssembly

The article explores Smalltalk code translation to WebAssembly (WASM) within the Catalyst project. It details levels of Smalltalk, JavaScript, and WASM, focusing on direct Smalltalk to WASM optimization. A new translator, WATCompiledMethodTranslator, aids in this process, exemplified by a Smalltalk expression conversion. Creation of a WASM module for Smalltalk methods is explained, highlighting performance advantages and potential future comparisons with dynamic JS translations. Collaboration in the Smalltalk community is encouraged for further advancement.

Show HN: Compile Erlang to WASM and run as HTTP/incoming-handler

Show HN: Compile Erlang to WASM and run as HTTP/incoming-handler

The "erwasm-http" GitHub repository demonstrates compiling Erlang to WebAssembly for HTTP handling. Users need specific tools and dependencies to set up and run the server, with location reporting concerns noted.

Compilation of JavaScript to WASM, Part 2: Ahead-of-Time vs. JIT

Compilation of JavaScript to WASM, Part 2: Ahead-of-Time vs. JIT

Chris Fallin's blog post discusses his AOT compiler for JavaScript to WebAssembly, achieving 3-5x performance improvements while addressing challenges like dynamic typing and Wasm platform constraints.

Compilation of JavaScript to WASM, Part 3: Partial Evaluation

Compilation of JavaScript to WASM, Part 3: Partial Evaluation

The article concludes a series on compiling JavaScript to WebAssembly, discussing challenges, proposing a portable interpreter for debugging, and introducing the Futamura projection for program compilation and analysis.

Goja: A Golang JavaScript Runtime

Goja: A Golang JavaScript Runtime

Goja is a JavaScript runtime library for Go, enabling easy integration and manipulation of data, supporting exception handling, sandboxing, and optimizing performance through sync.Pool for VM initialization.

Link Icon 1 comments