gocompilationconditional-compilation

how do I exclude a go library from compilation on linux but not Mac/windows?


I use

import "github.com/dontpanic92/wxGo/wx"

in a program that can run with or without a GUI.

i.e. you might run it like:

./program --gui true

or

./program --gui false

When running on Linux with GUI false I'd like to avoid having to compile all of wxGo/wx.

How can I check in the same code to git so that it will compile on Mac or Linux just fine.

i.e. right now when I compile on Linux I have to comment out any reference to wxGo/wx


Solution

  • If you want to exclude specific Go files from building on certain operating systems, architectures, Go versions or if certain tags are specified during compilation, these are your options:

    1. If you want a file to only build on Linux, append _linux to the file name, e.g. gui_linux.go. It is also possible to specify an architecture, or both, e.g. gui_windows_amd64.go.

    2. Add a //+build ... comment at the top of your Go file, e.g. // +build linux,386.

    See https://golang.org/pkg/go/build/#hdr-Build_Constraints for details.