<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Juan Moreno</title>
    <description>The latest articles on DEV Community by Juan Moreno (@morjuax).</description>
    <link>https://dev.to/morjuax</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4241471%2F87b2de7f-529e-46e1-9906-ee4efc2e101c.jpg</url>
      <title>DEV Community: Juan Moreno</title>
      <link>https://dev.to/morjuax</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/morjuax"/>
    <language>en</language>
    <item>
      <title>How I built a native Go playground with Wails: AST rewriting, a tiny runtime module, and a binary cache</title>
      <dc:creator>Juan Moreno</dc:creator>
      <pubDate>Sat, 28 Nov 2026 19:10:28 +0000</pubDate>
      <link>https://dev.to/morjuax/how-i-built-a-native-go-playground-with-wails-ast-rewriting-a-tiny-runtime-module-and-a-binary-2kno</link>
      <guid>https://dev.to/morjuax/how-i-built-a-native-go-playground-with-wails-ast-rewriting-a-tiny-runtime-module-and-a-binary-2kno</guid>
      <description>&lt;p&gt;I kept doing the same five-step dance every time I wanted to test a small idea in Go: create a folder, &lt;code&gt;go mod init&lt;/code&gt;, write a &lt;code&gt;main.go&lt;/code&gt;, &lt;code&gt;go run&lt;/code&gt;, delete everything. For a language that compiles this fast, the ceremony around trying one line of code felt wrong.&lt;/p&gt;

&lt;p&gt;So I built &lt;strong&gt;GoLab&lt;/strong&gt;, a native desktop Go playground built with &lt;a href="https://wails.io" rel="noopener noreferrer"&gt;Wails&lt;/a&gt;. You open it, type Go, and see results next to the line that produced them. No &lt;code&gt;package main&lt;/code&gt;, no &lt;code&gt;go.mod&lt;/code&gt;, no project. It runs locally with your own Go toolchain, so there is no sandbox, no fake clock, and &lt;code&gt;net/http&lt;/code&gt; and file I/O work normally.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  The stack
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Backend:&lt;/strong&gt; Go + Wails v2. The Go side exposes methods (&lt;code&gt;RunGoCode&lt;/code&gt;, &lt;code&gt;StopGoCode&lt;/code&gt;, formatting, module management) that Wails binds straight into TypeScript.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Frontend:&lt;/strong&gt; Vue 3, Pinia, and Monaco Editor (the editor behind VS Code).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Execution:&lt;/strong&gt; the user's code is rewritten, compiled with the real &lt;code&gt;go build&lt;/code&gt;, and executed as a normal process.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Wails was a good fit because the hard part of this app is &lt;em&gt;all&lt;/em&gt; Go: parsing, rewriting, compiling, process management. I didn't want to ship a Chromium runtime for that, and Wails uses the system webview instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 1: running code that isn't a valid Go program
&lt;/h2&gt;

&lt;p&gt;Users write things like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Name&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="nc"&gt;Age&lt;/span&gt;  &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"Ana"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;31&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;u&lt;/span&gt;
&lt;span class="n"&gt;strings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ToUpper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is not a Go program. There's no package clause, no &lt;code&gt;main&lt;/code&gt;, a short variable declaration at top level, and bare expressions.&lt;/p&gt;

&lt;p&gt;GoLab handles this in two passes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pass 1: split and wrap.&lt;/strong&gt; A line-level scan sorts the source into three buckets: imports, declarations (&lt;code&gt;func&lt;/code&gt;, &lt;code&gt;type&lt;/code&gt;, &lt;code&gt;const&lt;/code&gt;, top-level &lt;code&gt;var&lt;/code&gt;), and executable statements. Brace depth is tracked so a function body stays with its declaration. Then the program is reassembled:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="cm"&gt;/* user imports + a default set: fmt, strings, time, ... */&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="cm"&gt;/* declarations */&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/* executable statements, in order */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the user &lt;em&gt;did&lt;/em&gt; write &lt;code&gt;func main()&lt;/code&gt;, it gets renamed to &lt;code&gt;__user_main&lt;/code&gt; and called from the generated &lt;code&gt;main&lt;/code&gt;, so both styles work.&lt;/p&gt;

&lt;p&gt;Default imports that the user never touches would break the build with "imported and not used", so the wrapper adds blank uses like &lt;code&gt;var _ = strings.Contains&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pass 2: rewrite the AST to capture values.&lt;/strong&gt; The reassembled source is parsed with &lt;code&gt;go/parser&lt;/code&gt;, and &lt;code&gt;ast.Inspect&lt;/code&gt; walks every block. Two rewrites happen:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;fmt.Println(x)&lt;/code&gt; / &lt;code&gt;fmt.Printf(...)&lt;/code&gt; becomes &lt;code&gt;__golab.Print(line, x)&lt;/code&gt; / &lt;code&gt;__golab.Printf(line, ...)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A bare call expression on its own line, like &lt;code&gt;strings.ToUpper(u.Name)&lt;/code&gt;, gets wrapped: &lt;code&gt;__golab.Print(line, strings.ToUpper(u.Name))&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The &lt;code&gt;line&lt;/code&gt; argument is the &lt;em&gt;original&lt;/em&gt; line in the editor, not the line in the generated file. Then &lt;code&gt;go/printer&lt;/code&gt; writes the tree back out.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 2: getting values back to the right line
&lt;/h2&gt;

&lt;p&gt;The helpers print a tagged line to stdout:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nc"&gt;Print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vals&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;interface&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;strings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TrimSuffix&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Sprintln&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vals&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"\n"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;// structs get %+v so you see field names&lt;/span&gt;
    &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Marshal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"\n__GOLAB_RESULT__{\"l\": %d, \"v\": %s}\n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The frontend scans the output for &lt;code&gt;__GOLAB_RESULT__&lt;/code&gt; markers, decodes the JSON, and renders the value inline next to line &lt;code&gt;l&lt;/code&gt; in Monaco. Everything else goes to the regular console.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 3: compiler errors must point at the user's lines
&lt;/h2&gt;

&lt;p&gt;After wrapping and rewriting, line 7 in the editor might be line 23 in the file that actually compiles. While reassembling, GoLab builds a &lt;code&gt;lineMap&lt;/code&gt; (generated line → original line) and runs every compiler error through a regex that rewrites &lt;code&gt;main.go:23:&lt;/code&gt; back to &lt;code&gt;main.go:7:&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 4: you can't print a function that returns nothing
&lt;/h2&gt;

&lt;p&gt;Wrapping every bare call has a catch: &lt;code&gt;__golab.Print(line, doSomething())&lt;/code&gt; fails to compile when &lt;code&gt;doSomething()&lt;/code&gt; returns nothing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;main.go:12: doSomething() (no value) used as value
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can't tell from syntax alone whether a call returns a value. That needs type information. I could have brought in &lt;code&gt;go/types&lt;/code&gt;, but I went with something cheaper. When the build fails with &lt;code&gt;(no value) used as value&lt;/code&gt;, GoLab reads the line numbers from the error, marks those lines as "void", and rebuilds without wrapping them. It retries up to five times.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 5: making it feel instant
&lt;/h2&gt;

&lt;p&gt;GoLab can run code automatically when you stop typing, so compile time &lt;em&gt;is&lt;/em&gt; the UX. Three things made a big difference.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A binary cache keyed by content.&lt;/strong&gt; 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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A precompiled runtime package.&lt;/strong&gt; The &lt;code&gt;__golab&lt;/code&gt; helpers use &lt;code&gt;encoding/json&lt;/code&gt; and &lt;code&gt;reflect&lt;/code&gt;. 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, &lt;code&gt;golab-rt&lt;/code&gt;, written to disk once (versioned by a hash of its source) and wired in with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;go mod edit -require=golab-rt@v0.0.0 -replace=golab-rt@v0.0.0=/path/to/golab-rt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go's build cache compiles it once and reuses it across every run.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Smaller binaries.&lt;/strong&gt; Builds use &lt;code&gt;-ldflags="-s -w"&lt;/code&gt; since nobody needs a symbol table for a scratchpad.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 6: third-party modules, offline
&lt;/h2&gt;

&lt;p&gt;Users can add external modules (say &lt;code&gt;github.com/google/uuid&lt;/code&gt;) from a panel in the sidebar. That runs &lt;code&gt;go get&lt;/code&gt; once and stores the module in the normal module cache. On each build, GoLab only &lt;code&gt;require&lt;/code&gt;s the installed modules the code actually imports, and then runs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;GOPROXY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;off &lt;span class="nv"&gt;GOSUMDB&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;off go mod tidy
go build -mod=readonly ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;GOPROXY=off&lt;/code&gt; 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 7: people who don't have Go installed
&lt;/h2&gt;

&lt;p&gt;If no Go SDK is found, a setup wizard fetches the release list from &lt;code&gt;go.dev/dl/?mode=json&lt;/code&gt;, downloads the right archive for the OS and architecture, &lt;strong&gt;verifies its SHA-256 against the published checksum&lt;/strong&gt;, and extracts it into GoLab's own config directory. Your system Go install is never touched.&lt;/p&gt;

&lt;h2&gt;
  
  
  Things I learned the hard way
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Antivirus software hates freshly compiled binaries in temp folders.&lt;/strong&gt; 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".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Killing a running program is a feature.&lt;/strong&gt; 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.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Packaging and signing took as long as some features.&lt;/strong&gt; A large part of my changelog is release-pipeline fixes: macOS signing, and the NSIS installer on Windows. Budget time for it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;p&gt;GoLab is free to download, with an optional one-time $15 Pro license (tabs, unlimited snippets and modules, themes). No subscription.&lt;/p&gt;

&lt;p&gt;👉 &lt;a href="https://www.playgolab.com" rel="noopener noreferrer"&gt;playgolab.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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 &lt;code&gt;go/types&lt;/code&gt;, tell me in the comments.&lt;/p&gt;

</description>
      <category>go</category>
      <category>wails</category>
      <category>showdev</category>
      <category>macos</category>
    </item>
  </channel>
</rss>
