webassembly

command
v0.0.0-...-2e4d9bc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 19, 2021 License: BSD-3-Clause Imports: 2 Imported by: 0

README

"Hello world" WebAssembly sample

Create main.go:

package main

import (
  "fmt"
  "syscall/js"
)

func main() {
  fmt.Println("Hello, WebAssembly!")
  js.Global().Get("document").Call("getElementsByTagName", "body").
    Index(0).Set("innerHTML", "Hello, World!")
}

Compile it for WebAssembly:

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

Copy the JavaScript support file:

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

Create index.html:

<html>
    <head>
        <meta charset="utf-8"/>
        <script src="wasm_exec.js"></script>
        <script>
            const go = new Go();
            WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject).then((result) => {
                go.run(result.instance);
            });
        </script>
    </head>
    <body></body>
</html>

Install goexec (if not installed yet):

go get -u github.com/shurcooL/goexec

Serve the three files (index.html, wasm_exec.js, and main.wasm) from a web server, with goexec:

goexec "http.ListenAndServe(\`:8080\`, http.FileServer(http.Dir(\`.\`)))"

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL