WasmEdge Go SDK Introduction
The following is the guide to working with the WasmEdge Go API. You can embed the WasmEdge into your go application through the WasmEdge Go API.
Set up Environment for embedding WASM functions into your go application
Install and build WasmEdge and WasmEdge Go SDK
The Go SDK v0.14.0 requires Go 1.22 or later and the WasmEdge 0.14.0 shared library. Check your compiler with go version, then install the matching runtime in your development environment. On Linux or macOS:
curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | bash -s -- -v 0.14.0
source "$HOME/.wasmedge/env"
In the Go module that will host WasmEdge, add the SDK dependency:
go get github.com/second-state/WasmEdge-go/wasmedge@v0.14.0
Next, load a Wasm module and call an exported function from Go.
Build WasmEdge-go Extensions
The WasmEdge extensions have been deprecated after the 0.12.1 version. Please use the corresponding plug-ins after the 0.13.0 version.
By default, the WasmEdge-go only turns on the basic runtime.
WasmEdge-go has the following extensions:
-
TensorFlow
-
This extension supports the host functions in WasmEdge-tensorflow.
-
To install the
tensorflowextension, please use the-e tensorflowflag in the WasmEdge installer command. -
For using this extension, the tag
tensorflowwhen building is required:go build -tags tensorflow
-
-
Image
-
This extension supports the host functions in WasmEdge-image.
-
To install the
imageextension, please use the-e imageflag in the WasmEdge installer command. -
For using this extension, the tag
imagewhen building is required:go build -tags image
-
You can also turn on the multiple extensions when building:
go build -tags image,tensorflow
For examples, please refer to the example repository.
WasmEdge AOT Compiler in Go
The go_WasmAOT example demonstrates how to compile a WASM file into a native binary (AOT compile) from within a Go application.
Toolchain for Compile the Source Code to WASM Bytecode
To embed WasmEdge into your go application and run the WASM function, we still need language toolchain to compile the source code to WASM bytecode.
-
For Rust, please refer to the Rust setup guide
-
For Tinygo, please refer to the Tinygo setup guide
-
For C, please refer to the C setup guide
-
For Javascript, you need the WasmEdge-QuickJS into your Go application. See a community example here.
Examples
- Embed a standalone WASM app
- Embed a WASM function
- Pass complex parameters to WASM functions
- Embed a Tensorflow inference function
- Embed a bindgen function