TinyGo
- Go course
- Expert
- Lesson 29/34
- 4 min
Learn the TinyGo alternative compiler.
TinyGo
TinyGo is an alternative LLVM-based Go compiler. It is designed for microcontrollers (Arduino, ESP32, Raspberry Pi Pico) and small WASM modules; it produces KB-sized output instead of standard Go's MB-sized binaries.
tinygo flash -target=pico ./blink
tinygo build -o app.wasm -target=wasm ./webThe cost: parts of the standard library and reflect support are limited; the GC is simpler.
The machine package
TinyGo's machine package accesses pins directly. The classic "blink an LED" example:
led := machine.LED
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
for {
led.High()
time.Sleep(500 * time.Millisecond)
led.Low()
time.Sleep(500 * time.Millisecond)
}With little RAM (e.g. 264 KB), minimizing heap allocation and using fixed-size arrays matter.
Practice Blink pattern
Write the blink logic without hardware: type Pin struct{ acik bool } and func (p *Pin) Toggle() inverts the state. Toggle a pin 3 times and print the state at each step.
Starter code
package main
import "fmt"
func main() {
// buraya yaz
}Sample solution
package main
import "fmt"
type Pin struct{ acik bool }
func (p *Pin) Toggle() { p.acik = !p.acik }
func main() {
led := &Pin{}
for i := 0; i < 3; i++ {
led.Toggle()
fmt.Println(led.acik)
}
}Expected output
true false true
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which fits a microcontroller with no operating system?
- GOOS=linux go build
- GOOS=js go build
- cgo
- TinyGo Correct answer
Explanation: Standard Go expects an operating system; TinyGo supports bare-metal hardware targets directly.
2 Flash the board
tinygo ____ -target=pico ./blink
tinygo flash -target=pico ./blink
Explanation: tinygo flash compiles and writes the program directly to the board.
3 Compiler and target
| Term | Meaning |
|---|---|
go build |
Server, desktop |
tinygo flash |
Microcontroller |
tinygo -target=wasm |
Small WASM |
machine.LED |
On-board LED |