Building Fast and Lightweight Web Applications with TinyGo and WebAssembly
Modern web development demands high performance, low latency, and lightweight applications that provide seamless experiences to users across devices and connections. With the rise of WebAssembly (Wasm), developers now have a powerful tool to achieve these goals. But what if we told you that you could write WebAssembly modules in Go? Thanks to TinyGo, that dream is a reality.
In this article, we'll explore how TinyGo bridges the gap between Go and WebAssembly, highlight its advantages, and walk through building a simple WebAssembly-powered web application using TinyGo. Whether you're a seasoned Go developer or just curious about WebAssembly, this post will give you a solid foundation for getting started.
TinyGo is a Go compiler that targets small devices like microcontrollers and WebAssembly. It's designed to work with constrained environments where regular Go's runtime and memory usage are too heavy. Specifically, TinyGo provides:
Website: https://tinygo.org
WebAssembly is a low-level binary format that runs in the browser at near-native speed. It allows languages like C, Rust, and now Go (via TinyGo) to operate within browser environments. This brings several advantages:
Using WebAssembly with TinyGo means Go developers can write performant UI-side code without having to switch entirely to JavaScript or TypeScript.
Before we begin, let's install TinyGo. It works on Linux, macOS, and Windows.
Make sure Go 1.20 or later is installed:
$ go version
If not, download it from https://golang.org/dl/
Refer to the install page from the official site. Here's how to install it on macOS using Homebrew:
brew tap tinygo-org/tools brew install tinygo
Check installation:
tinygo version
Let’s build a simple example that computes factorial of a number and exposes it through WebAssembly.
mkdir tinygo-wasm-demo && cd tinygo-wasm-demo
main.go
) package main import "strconv" //export factorial func factorial(n int) int { if n == 0 { return 1 } return n * factorial(n - 1) } func main() {}
Note: The //export
directive makes Go functions accessible from JavaScript.
tinygo build -o main.wasm -target wasm main.go
index.html
<!doctype html> <html> <head> <title>TinyGo Wasm Demo</title> </head> <body> <h1>Factorial Calculator via TinyGo & WebAssembly</h1> <input type="number" id="input" value="5" min="0"> <button onclick="calculate()">Compute Factorial</button> <p>Result: <span id="result"></span></p> <script type="module"> let wasm; async function loadWasm() { const go = new Go(); const result = await WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject); wasm = result.instance; go.run(wasm); } async function calculate() { const value = parseInt(document.getElementById('input').value); const result = wasm.exports.factorial(value); document.getElementById('result').textContent = result; } loadWasm(); </script> <script src="https://tinygo.org/assets/wasm_exec.js"></script> </body> </html>
WebAssembly modules must be served via HTTP (not opened locally).
Use a simple Python server:
python3 -m http.server 8080
Navigate to http://localhost:8080
and try computing different factorial values.
Now that you've built a simple app, you might wonder how this stacks up in real-world scenarios. Consider the use cases:
TinyGo makes it feasible to bring Go’s clean type system and concurrency-safe primitives to the browser.
Despite how exciting TinyGo is, it’s important to note its limitations:
If your app depends heavily on DOM manipulation or external packages that require full Go standard library support, consider mixing Go with idiomatic JavaScript.
Here are some ideas to deepen your TinyGo skills:
WebAssembly is reshaping how we build for the web. It's fast, safe, and portable. With TinyGo, you can bring Go’s powerful features into the browser and write efficient WASM modules that empower your front-end applications.
Whether you're targeting embedded systems or optimizing web apps, TinyGo is a lightweight yet powerful tool that deserves a place in your development toolkit.
Dare to go Tiny — and Go fast!
💡 If you need help building high-performance browser apps with WebAssembly and Go, we offer such services: https://ekwoster.dev/service/frontend-development
Information