25% off

Use code FUNCMAIN at checkout for 25% off all premium courses.

Get started →

Running your Go WebAssembly App in the Browser

September 11, 2020
Elliot Forbes

Elliot Forbes

Course Instructor

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.

Now that we have been able to compile our Go application as a WebAssembly file and execute it with node. Let’s take it a step further and try running this within a browser.

Start off by creating a new index.html file within your project directory and then within here add the following:

<html>
	<head>
		<meta charset="utf-8"/>
		<script src="wasm_exec.js"></script>
		<script>
			const go = new Go();
			WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject).then((result) => {
				go.run(result.instance);
			});
		</script>
	</head>
	<body></body>
</html>

Next, you can try serve this using live-server which can be installed using npm install -g live-server

Let’s open up our application and then open up the browser console before refreshing the page. When we refresh the page, we should see that our Go WebAssembly file has been successfully executed within the browser!