DEMO: go-fmt-wasm.netlify.app
This example uses the go/format
package instead of compiling the go fmt
command line tool because we don't have access to a file-system replacement out of the box in the browser which go fmt
would need.
TinyGo produces significantly smaller wasm files so we use that instead of the the standard Go compiler.
Compared to the standard Go version, the size of the wasm file drops from ~3MB to ~0,5MB (uncompressed).
The time from initial page load to when the format button can be pressed was reduced from ~1s to ~0.5s.
- Install tinygo, see https://tinygo.org/getting-started/install/
- There is a custom wasm compile configuration needed (
wasm.json
) otherwise the stack size limit is too low to perform the formatting when there are more then ~100 (?) lines of code. With the current setting in the config, 3000 lines of code can be formatted without problems (takes ~130ms). - Compile the Go code with
tinygo build -o format-go-code.wasm -target ./wasm.json -no-debug ./go/main.go
- To execute the wasm file from JS you also need some glue code. This code can also be found in your local go installation under
{path to your tinygo installation}/targets/wasm_exec.js
. This script needs to be imported as a script in your index.html. (On Linux, the tinygo directory is usually/usr/local/lib/tinygo
.) - To avoid a memory leak, one function in the
wasm_exec.js
file needs to be replaced, see tinygo-org/tinygo#1140 (comment)
- The glue code needs to be imported via
<script src="wasm_exec.js"></script>
. - The WASM file needs to be loaded, see index.html.
- Afterwards, the "exported" function (
formatGoCode
) is available in JavaScript.
- If the formatting did not succeed, an empty string is returned. In that case, the result should not be used to replace the non-formatted code the user wrote.
- https://tinygo.org/docs/guides/webassembly/
- https://github.com/tinygo-org/tinygo/tree/release/src/examples/wasm/slices
This is a standalone prototype for exercism/exercism#5986.