Can't run a wasm file in mac using wasmtime

505 Views Asked by At

My go code

package main

import "fmt"

func main() {
  fmt.Println("Hello Web Assembly!")
}

Then I build the wasm file using the following command

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

I got main.wasm file. Then I installed wasmtime to run the wasm files. When I tried to run the wasm file with wasmtime, I'm getting the following error,

Error: failed to run main module main.wasm

Caused by: >0: failed to instantiate "main.wasm" >1: unknown import: go::debug has not been defined

My Go version: 1.20, My wasmtime version: wasmtime-cli 9.0.4, OS: Mac

This is just a simple example. Is there anything, I'm doing wrong? TIA.

2

There are 2 best solutions below

0
Tuong Le On

You'll need to compile to wasi instead of wasm if you wanna run this using wasmtime.

0
Zubair Hassan On

You are using js as the target os in your build target but trying to run the wasm file in wasi runtime.

Change the value of GOOS

Use this

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

Insted of this

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