WebAssembly

Option 1. WasmServe

If you want to see your game working on browsers, wasmserve is the easiest way.

go get github.com/hajimehoshi/wasmserve
wasmserve ./path/to/yourgame

Then access http://localhost:8080/.

Option 2. Compiling by yourself

If you want to publish your game, you'd need to compile your game by yourself.

Compiling your game

GOOS=js GOARCH=wasm go build -o yourgame.wasm github.com/yourname/yourgame

Copying wasm_exec.js to execute the Wasm binary

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

Creating an HTML

<!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("yourgame.wasm"), go.importObject).then(result => {
  go.run(result.instance);
});
</script>

Then open the HTML. You might need a local HTTP server.

If you want to embed your game into your web page, using iframe is strongly recommended.

You might find this message with Chrome:

The AudioContext was not allowed to start. It must be resume (or created) after a user gesture on the page. https://goo.gl/7K7WLu

In this case, you can solve this by putting allow="autoplay" on the iframe.