Platform-specific Go code uses operating-system or CPU-specific features (system calls, file paths, signals, assembly) and is compiled only for the targets that support it. Generic, or portable, Go code sticks to the standard library and interfaces, so the same source builds and behaves the same on every GOOS and GOARCH. Prefer portable code by default and isolate the platform-specific parts behind a small interface selected with build constraints.
This is different from Go generics (type parameters, added in Go 1.18). Generics are about reusing code across types; portability is about reusing code across operating systems and architectures. The two are often confused because both are called "generic".
Platform-specific code depends on something that exists, or behaves differently, on only some targets. Typical examples:
syscall or golang.org/x/sys/unix and golang.org/x/sys/windows.SIGUSR1, which do not exist on Windows.epoll, macOS kqueue, or file locking with flock.amd64 or arm64.Such code is faster or more capable on its target, but it will not compile, or will not behave correctly, elsewhere.
Portable code only uses packages whose behaviour the Go team keeps consistent across targets: fmt, io, net/http, encoding/json, os, path/filepath, time, sync and similar. The compiler and runtime hide the differences. A web service written this way can be built for Linux, macOS and Windows without a single line changing.
Portability still needs care in a few places:
path/filepath (not string concatenation with /) for file system paths.os.UserConfigDir, os.TempDir and os.UserHomeDir instead of hard-coded directories.int or uintptr; use int32, int64 and so on when the size matters.Go lets you keep both kinds of code in one package. The compiler picks files using file-name suffixes and //go:build lines.
File-name suffixes are implicit constraints: _linux.go, _windows.go, _darwin.go, _amd64.go, _linux_arm64.go. A file ending in _test.go is a test file; combine it as _linux_test.go when needed.
For anything more complex, use a //go:build line placed before the package clause:
Common expressions combine &&, || and !, and also accept unix (a convention for Unix-like systems, supported since Go 1.19) and cgo. The older // +build syntax is obsolete; gofmt and go vet keep the two forms in sync, and new code needs only //go:build.
Define the portable API once, then supply a file per platform. Here the package reports whether a process with a given PID is alive.
proc.go (all platforms):
proc_unix.go:
proc_windows.go:
Callers use proc.Alive(pid) everywhere and never see the difference. If you forget a platform, the build fails with an undefined alive, which is the behaviour you want. To add a clear fallback, add a file with //go:build !unix && !windows that returns false.
Because the Go toolchain is a cross compiler, you can build for another target by setting two environment variables:
Pure Go builds cross-compile without extra setup. Code that uses cgo needs a C cross-compiler, which is a strong reason to keep cgo out of portable packages. Run go vet for each target in CI so a missing implementation is caught before release:
At run time, runtime.GOOS and runtime.GOARCH expose the current target. They are fine for diagnostics or small choices, but build constraints are better for real divergence because unused code is not compiled into the binary.
| Aspect | Platform-specific | Portable |
|---|---|---|
| Reach | Only the targets you implement | Every supported target |
| Performance | Can use native features and assembly | Good, limited by the common API |
| Maintenance | One file per platform, more testing | One implementation |
| Cross-compiling | Needs care, cgo needs a C toolchain | Works out of the box |
| Typical use | Drivers, daemons, system tools, SIMD hot paths | Web services, CLIs, libraries |
No. Platform-specific code can be pure Go (for example syscall calls). cgo is a separate mechanism for calling C, and it is usually platform-specific as a side effect.
No. Type parameters let a function work over many types. Platform differences are handled with build constraints and interfaces.
Use build tags when implementations differ in the APIs they call, because the code will not compile on the other platform anyway. Use runtime.GOOS only for small runtime decisions that compile everywhere.
// +build line still needed?No. Since Go 1.17 the //go:build form is the standard, and the old form is only kept for compatibility with very old toolchains.
Write portable Go by default and use the standard library, path/filepath and interfaces. When you need a native feature, put it in small files selected by //go:build lines or file-name suffixes, expose one common function, and verify every target with cross-compilation and go vet in CI. Keep generics (type parameters) separate in your mind: they reuse code across types, not across platforms.