Skip to content

internet-computer/go-canister

Folders and files

NameName
Last commit message
Last commit date

Latest commit

1d070b2 · Mar 21, 2025

History

1 Commit
Mar 21, 2025
Mar 21, 2025
Mar 21, 2025
Mar 21, 2025
Mar 21, 2025
Mar 21, 2025
Mar 21, 2025
Mar 21, 2025

Repository files navigation

A Golang Canister

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!".

Code

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() {}

Running the Canister

To deploy and test the canister, follow these steps:

  1. Compile the Go code to WebAssembly:

Use TinyGo to compile the Go code into a .wasm file:

make build
  1. 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"
                }
            ]
        }
    }
}
  1. Create a minimal Candid file:

Create a service.did file with the following content:

service: {}
  1. Start the Internet Computer locally:

Run the following command to start the Internet Computer:

dfx start
  1. Deploy the canister:

Deploy the canister using:

dfx deploy
  1. 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!

Dependencies

Releases

No releases published

Packages

No packages published