Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

1. Preface

What gogo is

gogo is a small virtual machine for microcontrollers, plus a compiler for a subset of Go. You write your program in Go syntax, the gogo tool on your computer compiles it to a compact bytecode image, and the VM runs that image on the board: today the M5StickS3 and the Cardputer ADV (both ESP32-S3), with RP2040 boards planned. The same VM also runs on macOS and Linux, so you can develop and test on your computer before you touch hardware.

A gogo program is not structured the way an ordinary Go program is. There are no goroutines, no channels and no shared memory. Instead a program is a set of actors, in the style of Erlang: small, isolated processes that each have their own memory and talk to each other only by sending messages. Peripherals (buttons, the keyboard, the battery gauge, files, network sockets) appear as ports: special actors that you send commands to and that send you typed event messages.

Here is the flavour of it. This actor counts button presses:

for {
	switch m := actor.Receive().(type) {
	case gpio.Edge: // the button was pressed
		count++
	case actor.Request: // someone asks for the count
		actor.Reply(m, count)
	}
}

Why it works the way it does

gogo makes a few choices that will feel unusual if you come from Go. They all follow from one goal: one failing part must never take the whole device down.

  • Isolation. Every actor has its own heap. No pointer, slice or map can be shared between two actors, so no actor can corrupt another’s data. There are no locks because there is nothing to lock.
  • Messages are copied. actor.Send makes a deep copy of the message (slices, nested structs and all). The receiver gets its own copy. The one exception is frozen bytes.Bytes, which are immutable and so can be shared safely.
  • Per-actor memory limits. Each actor has a memory limit. An actor that goes over it (with a runaway loop, a leak, or a flood of incoming messages) is stopped. Other actors keep running.
  • Let it crash. Instead of defensive error handling everywhere, an actor that hits something unexpected (division by zero, an index out of range, bad input) simply exits. Other actors are told through links and monitors, and a supervisor restarts the failed actor in a clean state. Resources the actor held, such as open pins and files, are released automatically when it exits.
  • Small and readable. The VM is plain C99 with no third-party libraries and a 64 KB code budget. An idle actor costs about 150 bytes, so a board with 160 KB for actors can hold a thousand of them. The whole system is meant to be small enough for one person to read.

What gogo is good for

  • Firmware for small devices built from several independent activities: a button handler, a sensor poller, a display updater, a little web server, a logger. Each becomes an actor, and a bug in one does not freeze the others.
  • Devices that must keep running unattended: supervisors restart what fails.
  • Learning and experimenting: the language, VM and network stack are small enough to study end to end, and every program also runs on your laptop.

What gogo is not good for

  • Existing Go code. gogo compiles a subset of Go: no channels or goroutines (actors instead), no generics, no recover, no unsafe or reflection, package-level variables are read-only, and pointers, maps and fmt behave a little differently (copied when sent, maps in key order, no reflection in fmt). Most Go libraries will not compile as they are. Chapter 3 lists exactly what is in and what is out.
  • Number crunching. The VM is an interpreter, and every float is a small heap object: float32 and float64 work, but heavy maths belongs in C drivers.
  • Hard real-time work. The scheduler is preemptive but not real-time, and storage calls stall all actors while they run (a flash erase takes tens of milliseconds).
  • General-purpose secure networking. There is an HTTPS client (TLS 1.2, one connection at a time), but no TLS server, no client certificates and no HTTP/2.
  • Big data on a board. Each actor is limited to 16 KB by default on a board, and a single actor can never use more than 256 KB there (on a computer, 1 MB by default and up to 1 GB).

Where gogo is going

This book describes what is on gogo’s main branch. The next steps, in order:

  1. Embed mode (in progress): embedding the VM in other programs, the way Lua is embedded, for desktop and server programs, phone apps and test tools. The C library and its API are in place (chapter 18); the network and the board peripherals are not in it yet.
  2. Language completeness: more of Go. Methods, interfaces, error, panic and type assertions are in; pointers, maps, arrays, sized integers and floats are next (chapter 3 lists what is missing today).
  3. Bluetooth (BLE): talking to a phone app, and setting up Wi-Fi from the phone.

HTTPS (TLS, chapter 17) is done. RP2040 boards are planned, with the board and the timing still open. The design document, docs/design.md, describes how the VM works.

How to read this book

The chapters build on each other:

ChapterWhat you learn
2. Getting StartedBuild the tools, run a program on your computer, flash a board
3. Language BasicsThe Go subset: what works, what does not, and what to do instead
4. ActorsSpawn, Send, Receive, Call/Reply, names, timers
5. Failure and RecoveryExit reasons, links, monitors, supervisors, “let it crash”
6. Memory ModelPer-actor heaps and limits, message copying, Bytes and Buffer
7. Ports and PeripheralsGPIO, events, owners, Handoff
8. Keyboard and PowerThe kbd and power ports
9. StorageFiles on /flash and /sd, kv, ringlog
10. NetworkingDHCP, DNS, UDP, TCP, HTTP client and server, the date (SNTP)
11. Configuration and Devicesgogo config, Wi-Fi, the two boards
12. Toolchain ReferenceEvery gogo command
13. AppendicesLibrary quick reference, exit reasons, error codes, limits, differences from Go, requirement names
14. Hot LoadingLoading code at run time: gogo/code, modules, supervised upgrades
15. IMU and IRThe motion sensor, infrared send and receive, streams
16. AudioThe microphone and speaker, WAV files
17. HTTPShttps:// URLs, the clock, keep-alive http.Client, streaming uploads, the "tls" port
18. Embedding gogoRunning images inside a C program: libgogo.a, the host table, stepping, posts, host ports

Actors need not share a board: chapter 20, Distributed Actors, lets a PID name an actor on another board or a server, with the same Send, Call, links and monitors.

Every example in the book is a complete program in docs/book/examples/, and the output shown is the real output of running it on the host VM. You can run all of them with docs/book/examples/run.sh, which also checks the output against the book. Where the behaviour is likely to surprise you, the book says so in a Note box; the open limitations are collected in docs/known-issues.md.

You should know some Go, or at least one C-like language. You do not need to know Erlang or embedded programming; the book explains the ideas as they come up.