Building a WebAssembly hello-world app using TinyGo + wasip2

Rajat Jindal

Aug 7th 2024, 10:30AM

WebAssembly is the talk of the town, and TinyGo recently added support for wasip2. In this blog, we will walkthrough the steps to implement a WebAssembly Hello World app using TinyGo and run it using Wasmtime.

Prerequisites

Install TinyGo

TinyGo recently added support for wasip2, and it is available since v0.33.0 release.

  • Follow the install guide for tinygo.
  • Verify that tinygo is installed successfully
> tinygo version  
tinygo version 0.33.0 darwin/arm64 (using go version go1.23.1 and LLVM version 18.1.2)

Install wasm-tools

wasm-tools provide a number of subcommands for working with wasm modules and components. This is triggered during compilation by tinygo to create a webassembly component.

  • Download and add wasm-tools to your path from their GitHub releases page
  • Verify that wasm-tools is installed successfully
> wasm-tools --version
wasm-tools 1.211.1 (d4b0ccde6 2024-06-19)

Install wasmtime

  • Download and add wasmtime to your path from their GitHub releases page
  • Verify that wasmtime is installed successfully
> wasmtime --version
wasmtime-cli 22.0.0 (761f044ef 2024-06-20)

With that, we are now ready to dive into developing our first hello-world WebAssembly app:

mkdir hello-world
cd hello-world

cat >> main.go <<EOF
package main

import "fmt"

func main() {
    fmt.Println("Hello World !")
}
EOF

Now, we will build it using tinygo:

tinygo build -target=wasip2 -o main.wasm main.go

and finally run it using wasmtime

> wasmtime main.wasm
Hello World !

Congratulations !! You just executed your first app using WebAssembly + TinyGo + Wasmtime.

In the next article, we will discuss how to create a serverless http app using TinyGo + wasip2 and run using wasmtime.