Build WASM
Build for Browsers
with go
Build WebAssembly using go command.
Following two files are used in this section.
- prime.go
- main_go.go
Set the environmental variables for targeting.
// Windows
set GOOS=js
set GOARCH=wasm
// Linux
GOOS=js
GOARCH=wasm
Build WebAssembly with command
go build -o prime.wasm -tags=release -ldflags="-s -w" src/main_go.go src/prime.go
These command generate prime.wasm
.
JavaScript should be fetched from ${GOROOT}/misc/wasm/wasm_exec.js
.
(js is renamed to wasm_exec_go.js
in this repository)
select wasm_exec_go.js
in index.html before running on browser
loading...
with tinygo
Build WebAssembly using tinygo.
Following two files are used in this section.
- prime.go
- main_tinygo.go
Before build, we need to uncomment main()
function for build.
loading...
Let's build WebAssembly with this command.
tinygo build -opt=2 -wasm-abi=generic -target=wasm -tags=release -o prime.wasm src/prime.go
These command generate prime.wasm
.
JavaScript should be fetched from ${TINYGOROOT}/targets/wasm_exec.js
.
(js is renamed to wasm_exec_tinygo.js
in this repository)
select wasm_exec_tinygo.js
in index.html before running on browser
loading...
Build for Standalone
with tinygo
Building go program to standalone wasm is not currently supported by go command.
So, tinygo
is used here.
Make sure tinygo is installed. Binaries can be downloaded at github releases.
Before build, main()
function have to be uncommented.
loading...
tinygo build -wasm-abi=generic -target=wasi -o prime_standalone.wasm src/prime.go
tinygo build -opt=2 -wasm-abi=generic -target=wasi -tags=release -o prime_standalone.wasm src/prime.go
Those commands generate prime_standalone.wasm
.
When using WAVM runtime, uncomment print_prime()
function and build WebAssembly with
tinygo build -wasm-abi=generic -target=wasi -o print_prime_standalone.wasm src/prime.go
This generate print_prime_standalone.wasm
.