This repository contains a simple example of a canister written in Go using TinyGo. The canister responds to a query call with the message "Hello, World!".
package main
import "unsafe"
//go:wasmimport ic0 msg_reply_data_append
func msg_reply_data_append(ptr unsafe.Pointer, length uint32)
//go:wasmimport ic0 msg_reply
func msg_reply()
//export canister_query_hi
func canister_query_hi() {
msgBytes := []byte("Hello, World!\n")
msgPtr := unsafe.Pointer(&msgBytes[0])
msg_reply_data_append(msgPtr, uint32(len(msgBytes)))
msg_reply()
}
func main() {}
To deploy and test the canister, follow these steps:
- Compile the Go code to WebAssembly:
Use TinyGo to compile the Go code into a .wasm
file:
make build
- Set up your
dfx.json
:
Create a dfx.json
file to define the canister configuration:
{
"canisters": {
"hello_go": {
"type": "custom",
"build": "make build",
"candid": "service.did",
"wasm": "main.wasm",
"metadata": [
{
"name": "candid:service",
"visibility": "public"
}
]
}
}
}
- Create a minimal Candid file:
Create a service.did
file with the following content:
service: {}
- Start the Internet Computer locally:
Run the following command to start the Internet Computer:
dfx start
- Deploy the canister:
Deploy the canister using:
dfx deploy
- Call the canister:
Test the canister by calling the hi
query method:
dfx canister call hello_go hi --output raw | xxd -r -p
You should see the output:
Hello, World!