42.6 Memory Constraints and Optimization for Small Devices

Right, let’s talk about memory. On your laptop, it’s an abundant, lazy river of resources you barely think about. On a microchip like an ATtiny85 with 8KB of RAM, it’s a thimble of water you’re trying to cross the desert with. You will learn to be miserly. You will learn to hate waste. And Go, for all its wonderful abstractions, can be a bit of a spendthrift if you’re not careful. That’s where TinyGo and a new mindset come in.

42.5 GPIO, I2C, SPI, and UART in TinyGo

Right, so you want to talk to hardware. You’ve got a shiny microcontroller, a board full of mysterious pins, and a burning desire to make an LED blink or a sensor read. This is where the rubber meets the road, or more accurately, where the electrons meet the silicon. Forget HTTP handlers and JSON marshaling for a moment; we’re about to get physical with GPIO, I2C, SPI, and UART. TinyGo is our tour guide here, and it does a remarkably good job of translating Go’s elegance into the sometimes-brutal world of electrical signals.

42.4 Using TinyGo for WebAssembly Targets

Right, so you want to run Go on a web page. Not a server, not a CLI tool, but in the browser. You’ve heard WebAssembly (WASM) is the magic that makes it happen, and you’ve heard TinyGo is the key to doing it without shipping the entire Go runtime stuffed inside a clown car. You’re right on both counts. Let’s get into it. The core idea is brilliantly simple: you write your Go code, TinyGo compiles it to a .wasm file, and you load that into your browser with a bit of JavaScript hand-holding. The magic, and the friction, lives in the interaction between your Go code and the JavaScript world it’s now imprisoned in.

42.3 Differences Between TinyGo and Full Go

Alright, let’s get this out of the way: TinyGo is not just “Go, but smaller.” It’s a reimagining of the Go toolchain for a different class of hardware, and with that comes a set of trade-offs, quirks, and frankly, some brilliant engineering hacks. Think of it as Go’s pragmatic, slightly rebellious cousin who lives in a tiny house and is obsessed with efficiency. You’re going to use 95% of the language you know and love, but that missing 5% will keep you on your toes.

42.2 Supported Boards: Arduino, Raspberry Pi Pico, and More

Right, so you want to use Go, a language built for concurrency and web servers the size of small planets, on a microcontroller with less RAM than your keyboard buffer. I love it. This is where TinyGo comes in—it’s our savior, our magic wand that shrinks Go programs down to fit on these tiny, delightful chunks of silicon. But like any good magic trick, it has its limits. Not every board is created equal, and neither is TinyGo’s support for them. Let’s talk about which boards will be your new best friends and which might give you a bit of a fight.

42.1 TinyGo: A Go Compiler for Microcontrollers and WebAssembly

Right, so you’ve heard Go is for the cloud and servers, and you’re looking at that 8-bit microcontroller with 32KB of flash and thinking, “Yeah, no.” I get it. The standard Go runtime and its sprawling, beautiful, heap-hungry ways were not made for this world. But that’s where TinyGo comes in. Think of it as Go on a serious diet, one that trades the luxury of a multi-gigabyte heap for the discipline of living within 128KB of RAM. It’s a reimagining of the Go toolchain specifically for microcontrollers (think ARM Cortex-M), WebAssembly, and other places where “small” is the primary feature.

— joke —

...