https://github.com/junedev/go-fmt-wasm
Go format in the browser via WebAssembly
https://github.com/junedev/go-fmt-wasm
formatting golang wasm webassembly
Last synced: 7 months ago
JSON representation
Go format in the browser via WebAssembly
- Host: GitHub
- URL: https://github.com/junedev/go-fmt-wasm
- Owner: junedev
- License: mit
- Created: 2021-10-08T19:11:07.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2022-02-05T12:53:58.000Z (over 4 years ago)
- Last Synced: 2025-03-27T23:28:37.507Z (over 1 year ago)
- Topics: formatting, golang, wasm, webassembly
- Language: JavaScript
- Homepage: https://go-fmt-wasm.netlify.app
- Size: 1.06 MB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go fmt in the browser via WebAssembly
**DEMO: [go-fmt-wasm.netlify.app](https://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](https://tinygo.org/) 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.
## Creating the wasm file
- 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 https://github.com/tinygo-org/tinygo/issues/1140#issuecomment-718145455
## Using the wasm file
- The glue code needs to be imported via ``.
- The WASM file needs to be loaded, see index.html.
- Afterwards, the "exported" function (`formatGoCode`) is available in JavaScript.
## Error handling
- 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.
## Sources
- https://tinygo.org/docs/guides/webassembly/
- https://github.com/tinygo-org/tinygo/tree/release/src/examples/wasm/slices
## Motivation
This is a standalone prototype for https://github.com/exercism/exercism/issues/5986.