I kept doing the same five-step dance every time I wanted to test a small idea in Go: create a folder, go mod init, write a main.go, go run, delete everything. For a language that compiles this fast, the ceremony around trying one line of code felt wrong.
So I built GoLab, a native desktop Go playground built with Wails. You open it, type Go, and see results next to the line that produced them. No package main, no go.mod, no project. It runs locally with your own Go toolchain, so there is no sandbox, no fake clock, and net/http and file I/O work normally.
This post is about how it works under the hood, because a few of the problems turned out to be more interesting than I expected.
The stack
-
Backend: Go + Wails v2. The Go side exposes methods (
RunGoCode,StopGoCode, formatting, module management) that Wails binds straight into TypeScript. - Frontend: Vue 3, Pinia, and Monaco Editor (the editor behind VS Code).
-
Execution: the user's code is rewritten, compiled with the real
go build, and executed as a normal process.
Wails was a good fit because the hard part of this app is all Go: parsing, rewriting, compiling, process management. I didn't want to ship a Chromium runtime for that, and Wails uses the system webview instead.
Problem 1: running code that isn't a valid Go program
Users write things like this:
type User struct {
Name string
Age int
}
u := User{"Ana", 31}
u
strings.ToUpper(u.Name)
That is not a Go program. There's no package clause, no main, a short variable declaration at top level, and bare expressions.
GoLab handles this in two passes.
Pass 1: split and wrap. A line-level scan sorts the source into three buckets: imports, declarations (func, type, const, top-level var), and executable statements. Brace depth is tracked so a function body stays with its declaration. Then the program is reassembled:
package main
import (/* user imports + a default set: fmt, strings, time, ... */)
/* declarations */
func main() {
/* executable statements, in order */
}
If the user did write func main(), it gets renamed to __user_main and called from the generated main, so both styles work.
Default imports that the user never touches would break the build with "imported and not used", so the wrapper adds blank uses like var _ = strings.Contains.
Pass 2: rewrite the AST to capture values. The reassembled source is parsed with go/parser, and ast.Inspect walks every block. Two rewrites happen:
-
fmt.Println(x)/fmt.Printf(...)becomes__golab.Print(line, x)/__golab.Printf(line, ...). - A bare call expression on its own line, like
strings.ToUpper(u.Name), gets wrapped:__golab.Print(line, strings.ToUpper(u.Name)).
The line argument is the original line in the editor, not the line in the generated file. Then go/printer writes the tree back out.
Problem 2: getting values back to the right line
The helpers print a tagged line to stdout:
func Print(line int, vals ...interface{}) {
res := strings.TrimSuffix(fmt.Sprintln(vals...), "\n")
// structs get %+v so you see field names
b, _ := json.Marshal(res)
fmt.Printf("\n__GOLAB_RESULT__{\"l\": %d, \"v\": %s}\n", line, string(b))
}
The frontend scans the output for __GOLAB_RESULT__ markers, decodes the JSON, and renders the value inline next to line l in Monaco. Everything else goes to the regular console.
I went with a stdout protocol instead of something fancier (a socket, a debugger) because the program stays a plain Go binary. It behaves exactly as it would from a terminal, and that is the whole point of the app.
Problem 3: compiler errors must point at the user's lines
After wrapping and rewriting, line 7 in the editor might be line 23 in the file that actually compiles. While reassembling, GoLab builds a lineMap (generated line → original line) and runs every compiler error through a regex that rewrites main.go:23: back to main.go:7:.
Problem 4: you can't print a function that returns nothing
Wrapping every bare call has a catch: __golab.Print(line, doSomething()) fails to compile when doSomething() returns nothing:
main.go:12: doSomething() (no value) used as value
You can't tell from syntax alone whether a call returns a value. That needs type information. I could have brought in go/types, but I went with something cheaper. When the build fails with (no value) used as value, GoLab reads the line numbers from the error, marks those lines as "void", and rebuilds without wrapping them. It retries up to five times.
It sounds hacky, but in practice it converges on the first retry, and it means the compiler stays the single source of truth for types.
Problem 5: making it feel instant
GoLab can run code automatically when you stop typing, so compile time is the UX. Three things made a big difference.
A binary cache keyed by content. Before touching the disk, GoLab hashes the instrumented code plus the installed modules plus the runtime version (SHA-256). If a binary with that hash exists, it skips compilation entirely and just runs it. Switching back to a tab you already ran costs almost nothing. The cache keeps at most 20 binaries / 100 MB and evicts the oldest first.
A precompiled runtime package. The __golab helpers use encoding/json and reflect. Early on they were injected into every user program, so those packages got pulled into each build. Now they live in a tiny local module, golab-rt, written to disk once (versioned by a hash of its source) and wired in with:
go mod edit -require=golab-rt@v0.0.0 -replace=golab-rt@v0.0.0=/path/to/golab-rt
Go's build cache compiles it once and reuses it across every run.
Smaller binaries. Builds use -ldflags="-s -w" since nobody needs a symbol table for a scratchpad.
Problem 6: third-party modules, offline
Users can add external modules (say github.com/google/uuid) from a panel in the sidebar. That runs go get once and stores the module in the normal module cache. On each build, GoLab only requires the installed modules the code actually imports, and then runs:
GOPROXY=off GOSUMDB=off go mod tidy
go build -mod=readonly ...
GOPROXY=off guarantees no network calls during a run. Only modules already in the local cache resolve. That keeps builds fast and predictable, and it's why GoLab works on a plane.
Problem 7: people who don't have Go installed
If no Go SDK is found, a setup wizard fetches the release list from go.dev/dl/?mode=json, downloads the right archive for the OS and architecture, verifies its SHA-256 against the published checksum, and extracts it into GoLab's own config directory. Your system Go install is never touched.
Things I learned the hard way
- Antivirus software hates freshly compiled binaries in temp folders. On Windows, Defender sometimes quarantined the cached executables. GoLab now detects that failure and shows a message explaining how to add an exclusion, instead of a cryptic "access denied".
- Killing a running program is a feature. An infinite loop in a playground is a normal Tuesday. The runner keeps a handle to the current process so the Stop button (and starting a new run) kills it right away.
- Packaging and signing took as long as some features. A large part of my changelog is release-pipeline fixes: macOS signing, and the NSIS installer on Windows. Budget time for it.
Try it
GoLab is free to download, with an optional one-time $15 Pro license (tabs, unlimited snippets and modules, themes). No subscription.
I'd love feedback from other Gophers, especially on the AST rewriting approach. If you've solved the "does this call return a value?" problem more elegantly without pulling in go/types, tell me in the comments.
Top comments (0)