WebAssembly

Option 1. WasmServe

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

go run github.com/hajimehoshi/wasmserve@latest ./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

On a Unix/Linux shell:

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

On Windows PowerShell:

$Env:GOOS = 'js'
$Env:GOARCH = 'wasm'
go build -o yourgame.wasm github.com/yourname/yourgame
Remove-Item Env:GOOS
Remove-Item Env:GOARCH

Copying wasm_exec.js to execute the WebAssembly binary

On a Unix/Linux shell:

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

On Windows PowerShell:

$goroot = go env GOROOT
cp $goroot\misc\wasm\wasm_exec.js .

Creating an HTML

Create this 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 this HTML in your browser. You might need a local HTTP server.

If you want to embed your game into your web page, using iframe is strongly recommended. The screen scale is automatically adjusted. If the above HTML's name is main.html, the host HTML will be like this:

<!DOCTYPE html>
<iframe src="main.html" width="640" height="480"></iframe>

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.