- run the plugins with the Extism CLI
- use the host functions provided by the PDK
Extism Go PDK: https://github.com/extism/go-pdk
mkdir 07-extism-go-plug-in
cd 07-extism-go-plug-in
go mod init hello-extism-go
touch main.go
package main
//export hello()
func hello() int32 {
// read the function argument from the memory
// display the argument
// read the config object
// display the value of url
// use the url to make an http request
// display the response
// return a message: output := "🎉 Extism is 💜"
return 0
}
func main() {}
//export hello()
, then the function is "visible" for the host- the empty
main
function is mandatoryint32
: the wasm functions return a number (if you make a void function it should work)
go get github.com/extism/go-pdk
The wasm plugin will be launched with the Extism CLI, like this:
extism call go-plugin.wasm hello \
--input "😀 Hello World 🌍! (from TinyGo)" \
--log-level "info" \
--allow-host "*" \
--set-config '{"url":"https://jsonplaceholder.typicode.com/todos/1"}' \
--wasi
# info, debug, warn, error
extism call go-plugin.wasm hello \
--input "😀 Hello World 🌍! (from TinyGo)" \
--log-level "debug" \
--allow-host "*" \
--set-config '{"url":"https://jsonplaceholder.typicode.com/todos/1"}' \
--wasi
We call the
hello
function in the wasm plugin
--input
will pass the string to the plugin--log-level
will set the log level--allow-host "*"
will allow the plugin to make http requests--set-config
will set the configuration for the plugin (another way to pass data to the plugin)--wasi
will enable WASI
The function will:
- read the function argument from the memory
- display the result
- get the value of
url
from the config object - display the value of
url
- make a
GET
HTTP request to theurl
- display the response
- return a value to the host (the CLI)
// read the function argument from the memory
input := pdk.Input()
// display input
pdk.Log(pdk.LogInfo, string(input))
// read a key from the config object
url, _ := pdk.GetConfig("url")
// make an HTTP request
req := pdk.NewHTTPRequest("GET", url)
res := req.Send()
// you can get the result with res.Body()
- If you want to test the response status , use
res.Status()
- If you want to display the response body, use
res.Body()
- If you want to display the response status, use
strconv.FormatUint(uint64(res.Status()), 10)
// return a message
// copy string to host memory
mem := pdk.AllocateString("🎉 Extism is 💜")
pdk.OutputMemory(mem)
tinygo build -scheduler=none --no-debug \
-o go-plugin.wasm \
-target wasi main.go
ls -lh *.wasm
... And, then, run the plugin with the Extism CLI 🚀