In-browser golang compiler

I recently learned that Go can run in the browser through WebAssembly. This made me think about making the Go compiler work in the browser, in order to compile downloadable binaries.

I’ve asked a question about this on StackOverflow and got an answer confirming that this would be possible, and providing a high-level overview of what would need to be done. However, after having a look at the Golang GitHub repository, I realised how difficult this is going to be. I didn’t even know where to start!

I’m wondering if someone could give me some guidance as to how I could go about this. I’m basically thinking of compiling the Go compiler itself to WASM.

It would allow you to download the source code and compile it in your browser but why do you want to do that instead of just downloading the binary for your platform?

Have you used play.golang.org? That site lets you enter Go code, build, and run you program. It has a download feature but it only lets you download the source code you already have in the editor. I haven’t looked at play.golang.org’s source code, but I’d recommend starting here and then adding a piece to prompt the user to select GOOS and GOARCH and then download the result.

Download the binary compiler or the binary program I’m trying to compile? If the former, I’m trying to make my project really flexible so users can simply visit the website and download their file without the need to install any other software. If the latter, my program has a few compile-time options which I want to make available to users. I figured this is not a problem since Golang compiles quite quickly.

As for your second question, I have looked at it but the way the playground does it, is by sending the code to the server and executing it there, in a sandbox, while returning the result. I hope to make my compiler browser-only so it can be used in webhosting packages. I think there have been plans to make the playground compile the code to WASM and execute it in the browser but the compiling would still happen on the server. Not sure if they implemented that yet.

I wanted to try doing that and it seems easy enough, so here is what I did :

This command should compile Go source for WASM, when I executed that I got an error, so I think your challenge will be solving this error

Forgive my ignorance, but I don’t understand what a web-hosting package is, even after Googling (I’m not a professional programmer and my interests are in low-level stuff, hence my interest in the question about compiling). The compile time options argument makes sense to me, but I still don’t understand yet why you would want to compile Go client-side in the browser. For the compile-time options, why not prompt the user with a web page for the options they want and then, server-side, check if the combination has been requested before? If so, send the pre-compiled version, otherwise, compile and send it. I, personally, use mobile devices almost all the time and compiling client-side would not be the best experience for me. I am just having a hard time understanding the scenario.

Sorry about the wait - I’m currently on holiday. How do I execute the bootstrapped code? Not too familiar with WASM.

I didn’t use WASM before so I wouldn’t be helpful at this point, I checked MDN


and it involves instantiating the binary then exporting function from it, in our case it’s an executable binary with a main entry point not a library so the method I see in this page isn’t too relevant to what you try to achieve here

A web hosting package is just something a lot of hosting providers offer - a service where the user has access to one folder on the host system only (their webroot), and has a webhosting stack pre-installed, with no other access to the machine. These deals are usually much cheaper than a VPS for example, but much more restricted. That’s why compiling server-side is not always an option. I am also considering packaging my app in something like Electron which would also pretty much require compiling client-side as there would be no server.

Thanks for the link. Will look at it in a minute. As for the compiler being a binary with a main entrypoint, can’t that just be solved by renaming “main” and then calling the new name from JavaScript when ready, or am I misunderstanding something?

1 Like