Some small examples of using Go and WebAssembly
The examples require at least Go version 1.12!
The server
directory contains a very small HTTP server implementation that
simply hosts the files of the current working directory.
It also downloads the wasm_exec.js
JavaScript bridge from the official golang
repository, if the file doesn't already exist.
go build -o server.bin ./server
./server.bin
You can also use any other web-server that provides static file hosting.
The first example prints a "Hello World" to the console of the browser. Browse to http://localhost:3000 after building the example:
GOARCH=wasm GOOS=js go build -o test.wasm ./wasm1
The second example interacts with the DOM of the page and enables the "Stop" button. It also registers a click handler on the button and keeps the go program alive until the button was pressed. Browse to http://localhost:3000 after building the example:
GOARCH=wasm GOOS=js go build -o test.wasm ./wasm2
The third example shows how to create elements in the DOM. It uses the
github.com/PaulRosset/go-hacknews
package to fetch the top 10 stories from
Hacker News and displays them as list of anchors in the DOM.
Browse to http://localhost:3000 after building the example:
go get -u github.com/PaulRosset/go-hacknews
GOARCH=wasm GOOS=js go build -o test.wasm ./wasm3
The fourth example implements a very simple markdown editor that uses the
package github.com/shurcooL/github_flavored_markdown
to render
github-flavoured markdown to HTML.
Browse to http://localhost:3000 after building the example:
go get -u github.com/shurcooL/github_flavored_markdown
GOARCH=wasm GOOS=js go build -o test.wasm ./wasm4