Go's standard library handles most of what an IoT gateway or edge service needs: net and net/http for communication, encoding/json for telemetry, os for local storage, context and goroutines for concurrent sensors, and cross-compilation for ARM and MIPS devices. It does not include MQTT, Bluetooth or GPIO support, and standard Go does not run on small microcontrollers, which is where TinyGo comes in.
Go fits devices that run Linux, such as a Raspberry Pi, a router or an industrial gateway. It produces one static binary, so deployment is a file copy.
Go works best on the gateway and cloud side of an IoT system, and on embedded Linux boards with tens of megabytes of RAM or more.
| Target | Go option | Notes |
|---|---|---|
| Raspberry Pi, embedded Linux, gateways | Standard Go | Cross-compile with GOOS=linux GOARCH=arm64 |
| Microcontrollers (ARM Cortex-M, ESP32 and similar) | TinyGo | Separate compiler with a subset of the standard library |
| Cloud ingestion and device APIs | Standard Go | Servers handling thousands of device connections |
| Hardware access (I2C, SPI, GPIO) | Third-party libraries or cgo | Not in the standard library |
Building for a device is one command from your laptop:
CGO_ENABLED=0 gives a fully static binary, and -s -w strips debug symbols to shrink it.
Devices commonly report over TCP, UDP or HTTP. The TCP server below reads newline-delimited messages, applies a read deadline so dead devices do not hold goroutines forever, and acknowledges each message.
For HTTP ingestion, net/http with the Go 1.22 method-aware router keeps the handler short:
http.MaxBytesReader caps the body size, which matters when untrusted devices send data.
MQTT is the most common IoT messaging protocol, but Go's standard library has no MQTT client. The widely used choice is the Eclipse Paho client (github.com/eclipse/paho.mqtt.golang). The sketch below shows its shape, and it should use TLS in production rather than a plain tcp:// broker address.
If you only need request and response, plain HTTPS from the standard library is often simpler to operate than running a broker.
Goroutines make it easy to poll several sensors without blocking each other. Use context for shutdown and a sync.WaitGroup to wait for the workers.
Pressing Ctrl+C cancels the context, the pollers return, and the channel closes. Since Go 1.22 each loop iteration has its own i, so the closure captures the right value.
Devices lose connectivity, so buffer readings on disk and upload later. Write to a temporary file and rename it, so a power cut never leaves a half-written file.
Flash storage wears out, so batch writes instead of writing every reading. For larger buffers, SQLite through a third-party driver is a common step up.
Calling a vendor C library works through cgo, but it disables easy cross-compilation because you need a C cross-compiler for the target.
Where possible use pure-Go libraries for GPIO, I2C and SPI, which keep CGO_ENABLED=0 builds possible. For bare-metal microcontrollers, use TinyGo, which compiles a subset of Go and supports many boards, but not every standard library package.
Standard Go needs an operating system and several megabytes of memory. Use TinyGo for microcontrollers, and expect a reduced standard library.
No. Use a third-party client such as Eclipse Paho, or send data over HTTPS or gRPC if you control both ends.
Cross-compile with GOOS=linux GOARCH=arm64 (or arm with GOARM=7 for older boards), copy the binary over, and run it under systemd.
For hard real-time loops it can be. Keep timing-critical control in firmware or a microcontroller and use Go for coordination, networking and data handling.
Go suits IoT gateways and edge devices thanks to static binaries, easy cross-compilation, strong networking and cheap concurrency. The standard library covers TCP, HTTP, JSON, files and context. MQTT and hardware buses need third-party libraries, and microcontrollers need TinyGo.