Go suits IoT gateways and embedded Linux devices such as Raspberry Pi, industrial controllers and edge servers. The standard library covers networking (net, net/http, crypto/tls), data encoding (encoding/json, encoding/binary), timing (time.Ticker), concurrency, structured logging and signal handling, and the Go toolchain cross-compiles to ARM with one command. Hardware access such as GPIO, I2C and serial ports and protocols like MQTT need third-party modules.
Microcontrollers with kilobytes of memory cannot run standard Go. For those, use TinyGo, a separate compiler that supports a large subset of the language and the common boards.
| Target | Example hardware | Go option |
|---|---|---|
| Edge servers and gateways | x86 mini PCs, cloud edge nodes | Standard Go |
| Embedded Linux | Raspberry Pi, BeagleBone, industrial ARM boards | Standard Go, cross-compiled |
| Microcontrollers | Arduino-class boards, ESP32, RP2040 | TinyGo |
| Cloud backend for devices | Ingestion APIs, device registries | Standard Go |
On embedded Linux, a Go program is a single static binary with no runtime to install, which makes over-the-air updates and container deployment simple.
Build on your laptop, copy one file to the device. Set GOOS and GOARCH, and disable cgo so the result is fully static.
The -s -w flags strip symbol and debug information to shrink the binary. Typical Go binaries are a few megabytes, so check the flash space of your device before choosing Go.
IoT agents spend most of their time reading a sensor on a schedule. Use time.Ticker with a context so the loop stops cleanly on shutdown. On Linux, many sensors appear as files, for example the CPU temperature in millidegrees Celsius under /sys/class/thermal.
For real GPIO, I2C and SPI, use a library such as periph.io/x/conn/v3 or gobot.io/x/gobot/v2. For serial devices, go.bug.st/serial is widely used.
Networks on devices are unreliable, so every upload needs a timeout, a retry with backoff, and a way to stop. JSON over HTTPS is the simplest telemetry format and works with most cloud platforms.
This needs the imports bytes, context, encoding/json, fmt, net/http and time. For many devices, retry only idempotent requests, or include a reading ID so the server can discard duplicates.
JSON is readable but wasteful on cellular, LoRa or serial links. encoding/binary packs fixed-size fields into a few bytes.
binary.Write requires fixed-size types, so use int16 and uint8 rather than int. Fixed byte order (here big-endian) keeps devices of different architectures compatible. If you need schemas that evolve, Protocol Buffers is a common choice.
Devices are physically reachable and often deployed for years, so encrypt all traffic and authenticate both sides. crypto/tls supports mutual TLS, where each device presents its own client certificate.
Give each device a unique certificate so you can revoke one without touching the rest, and never ship a shared password or API key baked into the firmware image.
Most IoT platforms speak MQTT, a lightweight publish and subscribe protocol. The standard library has no MQTT client, so use a maintained module such as github.com/eclipse/paho.mqtt.golang or github.com/eclipse/paho.golang. Publish small messages to topics like devices/<id>/telemetry over TLS, and use a last-will message so the broker can tell consumers when a device drops offline.
Standard Go cannot, because the runtime and garbage collector need more memory than most microcontrollers have. TinyGo compiles a subset of Go for many boards, including ESP32 and RP2040.
Yes. It cross-compiles in seconds, produces a single binary, and handles concurrent sensors and network calls well. Use libraries such as periph.io or gobot for GPIO.
For gateways and Linux devices, rarely. Go's collector has short pauses, and you can lower memory use with GOMEMLIMIT. Hard real-time control loops belong in a microcontroller or a real-time OS.
Replace the single binary and restart the service with systemd, or ship it in a container. Sign update packages and verify the signature on the device before applying them.
Use standard Go for IoT gateways and embedded Linux, and TinyGo for microcontrollers. Cross-compile with GOOS and GOARCH, drive sensor loops with time.Ticker and context, send telemetry with timeouts and backoff, pack data with encoding/binary when bandwidth is tight, secure links with mutual TLS, and add third-party modules for GPIO and MQTT.