Goは一つのコードから各種OSで動作するバイナリが生成できます。Raspberry Piでも動くバイナリが生成できるので、IoTなどでも使えます。しかし、それはRaspberry PiでLinuxが動くからに他なりません。 マイコンの場合はどうでしょうか。そこで使ってみたいのがTinyGoです。

TinyGoの使い方

TinyGoはWebAssemblyにも対応しています。そのコード例です。

package main

// This calls a JS function from Go.
func main() {
    println("adding two numbers:", add(2, 3)) // expecting 5
}

// This function is imported from JavaScript, as it doesn't define a body.
// You should define a function named 'main.add' in the WebAssembly 'env'
// module from JavaScript.
func add(x, y int)

// This function is exported to JavaScript, so can be called using
// exports.multiply() in JavaScript.
//go:export multiply
func multiply(x, y int) int {
    return x * y;
}

TinyGoはArduinoやAdafruit ItsyBitsyのようなマイコンであったり、WebAssemblyへの変換も可能です。標準のGoでもWebAssemblyバイナリは作成できますが、サイズの大きさな懸念点でした。TinyGoを使うことで幅広い環境でGoが活用できるでしょう。

TinyGoはGo製のオープンソース・ソフトウェア(BSD)です。

Home :: TinyGo - Go on Microcontrollers and WASM tinygo-org/tinygo: Go compiler for small places. Microcontrollers, WebAssembly, and command-line tools. Based on LLVM.