BT

InfoQ Homepage News TinyGo Aims to Bring Go to the Internet of Things

TinyGo Aims to Bring Go to the Internet of Things

Bookmarks

Google-sponsored TinyGo is a new LLVM-based Go compiler to make it possible to run Go programs on microcontrollers, including the Arduino Uno, Arduino Nano33 IoT, and the BBC micro.bit, as well as modern browsers using Web Assembly.

TinyGo strives to create as small a binary as possible and attempts to optimize for memory usage. In contrast, Go compiler is optimized for speed. TinyGo also uses its own runtime, which is smaller than Go's.

The original reasoning was: if Python can run on microcontrollers, then certainly Go should be able to run on even lower level micros.

TinyGo does not aim to be able to compile any possible Go program, rather to compile most Go programs without modifications. Most language features are supported, including slices, interfaces, closures and bound methods. Others may be partially supported. In particular, since most targeted microcontrollers are single-core, goroutines are not optimized; reflection is available for most common types like number, strings, and structs. Maps are limited to using a reduced set of types such as strings, integers, and pointers as map keys. Furthermore, performance may be subpar.

The following is a program to blink a LED on any of the supported microcontrollers:

package main

import (
    "machine"
    "time"
)

func main() {
    led := machine.LED
    led.Configure(machine.PinConfig{Mode: machine.PinOutput})
    for {
        led.Low()
        time.Sleep(time.Millisecond * 1000)

        led.High()
        time.Sleep(time.Millisecond * 1000)
    }
}

Possibly the most striking feature of TinyGo is its attempt to maintain Go memory model, which requires some sort of garbage collection support. TinyGo uses a simple conservative mark-sweep collector. Collection is triggered when the heap is exhausted, which can be determined at compile time, or when the developer calls runtime.GC(). TinyGo also provides some support to optimize memory usage by inspecting where memory allocation actually happens in a program. This can be useful to avoid memory allocations in main loops.

As mentioned, TinyGo targets WASM as well. Although the official Go compiler can compile to WASM, TinyGo is able to provide a much smaller output. According to Google, it is not uncommon for the Go compiler to generate WASM files of 10+ MB when importing libraries, with ~2MB being the minimum. Instead, a simple "hello world" program is translated into a 575 bytes WASM program by TinyGo.

TinyGo can run on Linux, macOS, and Windows. A Docker image is also available to simplify its installation.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.