How to Run GoLang (1.15+) Code in a Browser Using WebAssembly

Photo made with Canva.

Why Do We Need WebAssembly?

Getting Started

Step 1: Create Our Go File

Step 2: Generate the Wasm JavaScript Loader

$ cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" .

Step 3: Compile the Main.go File

$ GOOS=js GOARCH=wasm go build -o main.wasm

Step 4: Create the HTML File

Step 5: Run

$ goexec 'http.ListenAndServe(`:8080`,http.FileServer(http.Dir(`.`)))'

Exposing Go Functions

Calling a Go Function

Implementing the Common Colors Example

$ go get github.com/EdlinOrg/prominentcolor
Result of the WebAssembly common colors calculation

Conclusion

Browsers that support WebAssembly. Icons made by Pixel perfect from www.flaticon.com

Software Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store