go-in-browser

Run the real Go compiler in the browser. This is not an interpreter: cmd/compile and
cmd/link from the official Go 1.27 release, compiled to WebAssembly (GOOS=wasip1), build your
program in a Web Worker against a precompiled standard library. The result is a wasip1 binary that
runs right there. No server, no install.
This is the engine behind the online Go compiler on
Devtoolry.
Try it
git clone https://github.com/devtoolry/go-in-browser
cd go-in-browser
node scripts/build-toolchain.mjs # Node 20+, ~2 min; downloads Go from go.dev into a temp folder
python3 -m http.server 8000 # any static server; module workers need http(s), not file://
Open http://localhost:8000 and press Run. The first run downloads the toolchain (~23 MB
gzipped).
How it works
- Build time (scripts/build-toolchain.mjs): download the Go
release for your machine (the checksum is verified against go.dev’s release list), then
GOOS=wasip1 GOARCH=wasm go build cmd/compile and cmd/link. go list -export -deps builds
the standard library packages a playground program needs (and everything they depend on) for
the same target. They are packed into std.pack: a 4-byte index length, a JSON index, then the
archives back to back.
- In the browser (src/go-worker.mjs): an in-memory file system
(browser_wasi_shim) holds
main.go, an
importcfg that maps import paths to the archives, and the archives. The worker runs
compile -p main -complete -importcfg importcfg -pack main.go, then link -o main.wasm, then
instantiates main.wasm with stdin, stdout and stderr.
A typical program compiles, links and runs in about 2–3 s once the toolchain is cached.
Things we learned the hard way
- We tried an interpreter first. Yaegi is small, but on
playground code it lacked the
min/max built-ins, got some generic type inference wrong,
blocked os.Exit and hung on deadlocks instead of reporting them. The real compiler gives the real
error messages, so we switched.
poll_oneoff. Go’s scheduler waits on several clocks (and on file descriptors) at once; the
shim handles a single clock subscription. The worker wraps it: descriptors are always ready, and
clocks are waited for by spinning, since a worker cannot block without cross-origin isolation.
PWD. Go’s wasip1 port takes its working directory from the PWD environment variable.
Compile in /work with PWD=/work and -trimpath=/work, and panics say main.go:7 instead
of a long build path.
- Size. The whole standard library for wasip1 is ~140 MB. The package list in the build script
(
fmt, strings, sort, slices, encoding/json, math/big, time, …) is ~42 MB, or ~10 MB
gzipped. Add a package to PACKAGES and rebuild to make it importable.
- Stopping. A busy WebAssembly program cannot be interrupted, so the page terminates the worker
on a timeout or when the user presses Stop, and starts a fresh one for the next run.
Use it in your page
const worker = new Worker(new URL('./src/go-worker.mjs', import.meta.url), { type: 'module' });
worker.onmessage = ({ data }) => {
if (data.type === 'output') console.log(`[${data.stream}]`, data.text);
if (data.type === 'done') console.log('exit code', data.exitCode);
};
worker.postMessage({ type: 'run', code: 'package main\nimport "fmt"\nfunc main() { fmt.Println("hi") }', stdin: '' });
The worker also sends { type: 'status', status: 'loading' | 'busy' | 'ready' }. Start your time
limit at ready. index.html shows the whole loop.
What it cannot do
No network, no cgo, no goroutines running in parallel (WebAssembly has a single thread here), and
only the packages listed in the build script.
License
MIT for this repository. The toolchain you build is Go’s own (BSD-3-Clause). Made by
Devtoolry, free developer tools that run in your browser.
评论