tiny go

In this tutorial we’ll see how to run the WebAssembly examples of TinyGo on windows 10

I have already set up the enviroment from this tutorial, I’m ready!

Great! then you know you need to have a linux subsystem on your windows

Sure!

Note: the following process can be done using only the WSL, but it would require using Vim to look at the code, which is fine, but out of scope in this tutorial

now le’t clone this repository locally. I will put it in the folder C:/Users/${myUser}, where myUser is the current user on my windows machine

the example folder we’ll use is C:\Users\${myUser}\tinygo\src\examples\wasm

What does this folder contain?

this folder contains two examples for the wasm build, this is the list of content in the folder

08/07/2019  13:22    <DIR>          callback
08/07/2019  13:22    <DIR>          export
08/07/2019  13:22    <DIR>          main
08/07/2019  13:22               553 Makefile
08/07/2019  13:22             4.111 README.md
08/07/2019  13:22               500 server.go

the folders with the examples are export and main

export uses JavaScript’s event listeners to interact with user input

main will print “hello world!” to the console

ok, how do I run the examples?

the process to run the examples is the same for both, first install the make command on your WSL

sudo apt install make

now move to the folder where you have cloned the examples

wait, I cloned the examples in my windows system, how do I access it throw the WSL?

the WSL have a special folder in the root, called mnt, this contains all the folders and files in your windows system, to access the examples folder this is the path

/mnt/c/Users/${myUser}/tinyGo/src/examples/wasm

ok! I was able to reach this directory using the WSL, now what?

now we’ll build and run the main example, but the same exact process can be done for the export example

first, let’s use make to create the html folder and move in this folder all the needed files, to do it just run the following

make main

this command will create a new folder called html, and move in this folder the html files, the js files and the wasm files.

now I only need to serve this files as I would do with a static site?

yes!, but actually no, some browser will accept wasm files ONLY if served as application/wasm as content-type, the server.go file does this for us, so all we have to do is run this file

go run server.go

this command will start a server on localhost:8080 and then serve all the content of the html folder, using the right content type

Great! what should I do now?

now look around, try to understand what the JS and Go code does, make little modification, and see what happens

How can I use external packages?

We’ll answer this question the next time

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.