Go Developing for Multiple Platforms with Conditional Compiling

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Introduction

Platform based conditional compiling comes in two forms in Go, one is with file suffixes and the other is with build tags.

Syntax

  • After "// +build", a single platform or a list can follow
  • Platform can be reverted by preceding it by ! sign
  • List of space separated platforms are ORed together

Remarks

Caveats for build tags:

  • The // +build constraint must be placed at the top of the file, even before package clause.
  • It must be followed by one blank line to separate from package comments.
List of valid platforms for both build tags and file suffixes
android
darwin
dragonfly
freebsd
linux
netbsd
openbsd
plan9
solaris
windows

Refer to $GOOS list in https://golang.org/doc/install/source#environment for the most up-to-date platform list.



Got any Go Question?