blob: d589d04d6f815df90526ec0be81768eab01026c8 [file] [log] [blame] [view]
Dmitri Shuralyovbb850ad2018-08-23 14:53:11 -04001Go 1.11 adds an experimental port to WebAssembly.
2
3WebAssembly is described on its [home page](https://webassembly.org) as:
4
5> WebAssembly (abbreviated _Wasm_) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable target for compilation of high-level languages like C/C++/Rust, enabling deployment on the web for client and server applications.
6
Brad Fitzpatrick1b2abf22018-08-24 08:46:59 -07007This page will be updated over time with more information relevant to Go's support for WebAssembly.
8
Go101fa3e0c92018-08-27 08:22:57 -04009# Prerequisite
10
Go101e6405282018-08-27 08:26:36 -040011Note, if you ever set the `GOROOT` environment variable to an old Go SDK path, please remove this environment variable. This environment variable is not needed any more. Otherwise, the `go build` command below will report error:
12
13```
Go101fa3e0c92018-08-27 08:22:57 -040014go tool compile: exit status 2
15compile: unknown architecture "wasm"
16```
17
Brad Fitzpatrick1b2abf22018-08-24 08:46:59 -070018# Example
19
20To compile the basic Go program for the web:
21
22```go
23package main
24
25func main() {
26 println("Hello, WebAssembly!")
27}
28```
29
30Run:
31
32```sh
33$ GOARCH=wasm GOOS=js go build -o test.wasm main.go
34```
35
36And copy over the HTML & JS support files:
37
38```sh
39$ cp $(go env GOROOT)/misc/wasm/wasm_exec.{html,js} .
40```
41
42Then serve those three files (`wasm_exec.html`, `wasm_exec.js`, and `test.wasm`) to a web browser.
43
44For a basic HTTP server:
45
46```go
47package main
48
49import (
50 "flag"
51 "log"
52 "net/http"
Johan Brandhorst28eca6e2018-08-27 08:56:00 +010053 "strings"
Brad Fitzpatrick1b2abf22018-08-24 08:46:59 -070054)
55
56var (
Brad Fitzpatrick3c560b92018-08-24 09:30:49 -070057 listen = flag.String("listen", ":8080", "listen address")
58 dir = flag.String("dir", ".", "directory to serve")
Brad Fitzpatrick1b2abf22018-08-24 08:46:59 -070059)
60
61func main() {
62 flag.Parse()
63 log.Printf("listening on %q...", *listen)
Johan Brandhorst28eca6e2018-08-27 08:56:00 +010064 log.Fatal(http.ListenAndServe(*listen, http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
65 if strings.HasSuffix(req.URL.Path, ".wasm") {
66 resp.Header().Set("content-type", "application/wasm")
67 }
68
69 http.FileServer(http.Dir(*dir)).ServeHTTP(resp, req)
70 })))
Brad Fitzpatrick1b2abf22018-08-24 08:46:59 -070071}
72```
73
Peterson Domingos94ea75b2018-08-27 11:25:41 -030074Now navigate to http://localhost:8080/wasm_exec.html, click "Run", and you should see the output in the JavaScript debug console.
Brad Fitzpatrick7ff1c192018-08-24 08:49:05 -070075
76# Interacting with the DOM
77
78See https://tip.golang.org/pkg/syscall/js/
79
Elliot Forbesfaab9de2018-08-26 14:55:46 +010080# Tutorials + Articles
81
Justin Cliftb2b432d2018-08-31 14:35:27 +010082* [Building a Calculator with Go and WebAssembly](https://youtu.be/4kBvvk2Bzis)
Larry Clapp9bfee2a2018-08-30 15:33:47 -060083([Source code](https://blog.owulveryck.info/2018/06/08/some-notes-about-the-upcoming-webassembly-support-in-go.html))
Justin Cliftb2b432d2018-08-31 14:35:27 +010084* [Get Going with WebAssembly](https://www.youtube.com/watch?v=iTrx0BbUXI4)
Justin Clift028c2012018-08-30 17:47:30 +010085
86# Further reference examples
87
Justin Cliftb2b432d2018-08-31 14:35:27 +010088* [GoWasm Experiments](https://github.com/stdiopt/gowasm-experiments) - Demonstrates working code for several common call types
Justin Cliftaf0bf552018-08-31 16:35:50 +010089* [Drawing simple 3D objects on the 2D canvas](https://justinclift.github.io/wasmGraph1/) ([source code](https://github.com/justinclift/wasmGraph1/))
Justin Clift8b8da332018-08-31 14:37:02 +010090 * Displays wireframe solids on the 2d canvas, using basic matrix maths. Use wasd/keypad keys to rotate.