Wasm Builders 🧱

Cover image for WebAssembly with Golang
sahitya
sahitya

Posted on

WebAssembly with Golang

Golang program for implementation of Insertion Sort

Environment setup

To compile this demo, you must install the following.

Golang

Go to go.dev and follow the instructions using rustup.

tinygo

Go to tinygo.org and follow the instructions using rustup.

please note: tinygo requires go version 1.15 through 1.17

Wasmtime

You will find wasmtime at wasmtime.dev

Golang program for implementation of Insertion Sort

cd WebAssembly_with_golang
go mod init Golang-to-WASI
Enter fullscreen mode Exit fullscreen mode

create a file main.go, add following code into your main.go file and save the file.

// simple programm on Insertion sort.

package main

import (
    "fmt"
    "math/rand"
    "time"
)

func main() {

    slice := generateSlice(20)
    fmt.Println("\n--- Unsorted --- \n\n", slice)
    insertionsort(slice)
    fmt.Println("\n--- Sorted ---\n\n", slice, "\n")
}

// Generates a slice of size, size filled with random numbers
func generateSlice(size int) []int {

    slice := make([]int, size, size)
    rand.Seed(time.Now().UnixNano())
    for i := 0; i < size; i++ {
        slice[i] = rand.Intn(999) - rand.Intn(999)
    }
    return slice
}

func insertionsort(items []int) {
    var n = len(items)
    for i := 1; i < n; i++ {
        j := i
        for j > 0 {
            if items[j-1] > items[j] {
                items[j-1], items[j] = items[j], items[j-1]
            }
            j = j - 1
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Run your code and provide input to see the result

go run main.go
Enter fullscreen mode Exit fullscreen mode

vmware_mhVvRMcZ3R

We will can compile to WASM using the following command:

tinygo build -wasm-abi=generic -target=wasi -o main.wasm main.go
Enter fullscreen mode Exit fullscreen mode

Now let us run the WebAssembly generated using wasmtime

wasmtime main.wasm
Enter fullscreen mode Exit fullscreen mode

vmware_VP4bq7gQ0U

Oldest comments (0)