WebAssembly with Go: Powering the Web with Speed and Efficiency
GopherCon AU 2023
@imJenal
Jyotsna Gupta
@imJenal
@imJenal
WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable compilation target for programming languages, enabling deployment on the web for client and server applications.
@imJenal
Source: https://webassembly.org/
@imJenal
Source: https://webassembly.org/
What's wrong with JS ?
@imJenal
@imJenal
@imJenal
Text
@imJenal
@imJenal
Games, Music applications (streaming, caching )
Virtual Reality /Augmented Reality
Image/Video editing, Image Recognition
Encryption, VPN, Remote desktop
CAD applications
Language interpreters & virtual machines.
Developer tooling (editors, compilers, debuggers)
@imJenal
Find other use cases: https://webassembly.org/docs/use-cases/
@imJenal
@imJenal
@imJenal
@imJenal
package main
import "fmt"
func main() {
fmt.Println("Go Web Assembly")
}
@imJenal
@imJenal
<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>
@imJenal
@imJenal
@imJenal
@imJenal
1. Compile to wasm:
GOOS=js GOARCH=wasm go build -o main.wasm
2. Copying js-glue files:
cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" .
3. Serve the files:
goexec 'http.ListenAndServe(":9090", http.FileServer(http.Dir(".")))'
@imJenal
@imJenal
@imJenal
@imJenal
Currently, that process is a bit cumbersome:
@imJenal
GOOS=js GOARCH=wasm go test
, and it'll automatically executes the tests inside a browser@imJenal
Figma
@imJenal
@imJenal
WASI: WebAssembly System Interface
@imJenal
@imJenal