🚀 Get 25% off access to all my premium courses - use discount code FUNCMAIN at checkout - view the pricing page now!

Video:

Getting Started with Go and WebAssembly

September 11, 2020

Course Instructor: Elliot Forbes

Hey Gophers! My name is Elliot and I'm the creator of TutorialEdge and I've been working with Go systems for roughly 5 years now.

Twitter: @Elliot_f

Let’s dive in and have a look at how we can take a simple Go application and compile it down to a WebAssembly file using the go build command.

As you can see here, we have a simple hello world application which will be the starting point for our course.

Let’s open up the terminal and ensure we are in the same directory as our main.go file and then execute the following:

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

This will take our Go source code and produce an executable WebAssembly module file called main.wasm

Now, in order to import this main.wasm file into a web page and execute it, we’ll need to pull in the wasm_exec.js file which exists within our GOROOT and then import that prior to importing our WebAssembly file into our application:

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

With this in place we can then attempt to run this using node by calling:

node wasm_exec.js main.wasm

This should then execute our newly built WebAssembly program that we’ve written in Go!