Fat Old Yeti

Fat Old Yeti

Being a blog of thoughts and tutorials from a hobby game developer.

11 Feb 2021

Web Deployment

Web Deployment

Go has a large variety of deployment targets. The Ebiten library, which we are using for graphics, can deploy to Windows, Mac, Linux, Android, IOS and the Web.
Let’s go over how to deploy our application to the Web.

First, in our directory, we need to compile our game:

GOOS=js GOARCH=wasm go build -o rrogue.wasm

Replace rrogue with whatever you wish to name your game.

This compiles the game to web assembly using JavaScript as the target. Setting the GOOS (or Go Operating System) variable for a build command tells Go what operating system to build it for. This is handy when we want to deploy our game to multiple environments.

Next we need to keep the wasm_exec.js file in the same directory as our wasm file. It comes with go and can be copied as such:

cp $(go env GOROOT)/misc/wasm/wasm_exec.js .

With those two files, it’s time to make a simple index.html to hold our stuff.

<!DOCTYPE html>
<script src="wasm_exec.js"></script>
<script>
	// Polyfill
	if (!WebAssembly.instantiateStreaming) {
	  WebAssembly.instantiateStreaming = async (resp, importObject) => {
	    const source = await (await resp).arrayBuffer();
	    return await WebAssembly.instantiate(source, importObject);
	  };
	}

	const go = new Go();
	WebAssembly.instantiateStreaming(fetch("rrogue.wasm"), go.importObject).then(result => {
	  go.run(result.instance);
	});
</script>

Note again, you can replace the rrogue.wasm with the name of your game.

Now, make sure to copy your assets folder to the same location as our wasm file.

At this point, run a local webserver and see it work. Here is the latest code (at any given time) running.

That is all you need to do to deploy your game to the web!

As always, any questions are welcome. I can be reached at fatoldyeti@gmail.com, or @idiotcoder on the gophers slack. In addition, I have that discord server set up with an invitation attached to the telephone icon on the front page.