- Full Go Compiler running on the browser.
- Supports using custom build tags.
- Incremental builds (build cache).
- Supports multiple files and packages (including dependencies).
- Full cross-compiling support directly from your browser.
- No need for a server backend to build executables.
- Easy deployment (just upload the generated files).
- Full filesystem abstraction for both the compiler and running programs.
- A standalone wasm_exec.js with filesystem support is available.
- Full DOM access for running programs (and basic stdout/stderr for now).
- Browser-based code editor (Ace).
Compiling and running examples from Ebiten:
Compiling and running modified examples:
Use this to provide editable demos for your projects!
- Run
go mod vendor
on your Go Module root. - Zip the project and upload it anywhere.
- Check that there are no CORS errors.
- You may use CI to perform these steps automatically, like this workflow for ebiten.
- Publish the URL that automatically loads and builds your project.
This project builds the Go Compiler to WebAssembly and provides enough abstractions, fixes and hacks for it to be able to build executables (for any platform) from the web. The main added abstraction is a virtual file system implementation that works in memory (based on virtualfs), which can also be used separately with a custom wasm_exec.js. The frontend also runs the compiled code (if the target arch is js/wasm), with the same features available.
The result is a static website that can compile and run most Go code (see known limitations below) from the client's browser.
Why? To learn how the Go compiler works and to provide better (hackable) demos for most Go projects with easy deployment.
There are 2 approaches to handle the standard library:
- Precompiling it while building the compiler: faster first compilation but requires a bigger (slower) initial download and only supports building for one OS/arch, unless another precompiled library is downloaded.
- Compiling the standard library from the browser (only the required packages): It allows to cross-compile to any OS/arch supported by Go at the cost of an slower initial build: compiled artifacts are cached once built for the first time (this happens automatically for all packages with unmodified sources).
A mix of both was applied: the precompiled standard library for js/wasm is downloaded. Cross-compiling is also possible because the source code of the standard library is downloaded and used for any other OS/arch.
You can download production builds from the releases or the github pages branch.
Dependencies:
- Go Compiler (Go 1.13-1.18)
node
andnpm
/yarn
- Very common UNIX tools.
Just run make
: it will output a static site to dist/
that can be uploaded to any web server. To learn how it works,
start by looking at the Makefile.
To only generate the modified wasm_exec.js (already embedded if using the main app), run make wasm_exec
.
- Limitations of building on
js/wasm
:- No Cgo support.
- Limitations of running on
js/wasm
:- Limited network access (available: HTTP client, WebRTC...).
- Limited persistent storage (not implemented yet, could be blocked/deleted by user).
- Dependencies must be vendored (due to limited network access).
- Slower than the native compiler, and may run out of memory for large projects.
Updated: 03/2022
- The official Go Playground (link): limited execution time, no DOM access, no output until the program finishes, limited multi-package support.
- Better Go Playground (link): has an experimental webassembly runtime, but includes no filesystem abstraction and still requires a server backend to build the webassembly modules, no multi-package support.
- pdfcpu (link): Example of running a Go CLI tool on the web browser, inspiration for this project.
- Wasm go playground (link): No standard library, no dependencies, no multi-file support, no cross-compilation, inspiration for this project
- Go Playground WASM (link): Actually compiles Goscript (a script language like Python or Lua written in Rust, with exactly the same syntax as Go's) instead of using the official Go Compiler.