Control files to build in Golang project

Background

We are developing a project that needs to run on Windows and Linux. The project is written in Golang. There are a lot of codes that can be shared between these OS, so we decided to make it in the same project. By this way we can speed up the development process very much.

In the project, there are some code files that run on Windows only, some run on Linux only, and some run on both Windows and Linux. And we want to separating them on build. It means:

Benefit

By separating files on build, I can see that we have some benefits:

How can we do it

Firstly we need to divide the code for Linux and for Windows into different files. After that we can use one of the following ways.

Name the file

We can tell go what OS and what architecture we want to build a file by naming it. The followings are examples about file names and build behaviors:

The file name patterns are:

We can do this way if the build condition is only about OS and architecture.

Use // +build command

This way gives us more control on the environment. We can specify multiple OS and architectures, compiler, go version, -tags

// file name: hello.go
// +build linux windows
// +build amd64

package hello

The comment // +build above will tell go to build the file on Windows 64bit or Linux 64 bit. So when we build the project in freebsd, the package hello will be excluded from the files to be built.

If we have a test for this package, it will be something like this:

// file name: hello_test.go
// +build linux windows
// +build amd64

package hello

The comment // +build above will tell go to exclude this file from test on environments that are not Linux 64bit or Windows 64bit for us. We don't have to check the OS then skip the test.

For complex usage of // +build, please check golang documentation here: https://golang.org/cmd/go/#hdr-Build_constraints

Reference

Recommended Posts

Control files to build in Golang project
How to reference static files in a Django project
Bring files in Windows to WSL
Method to build Python environment in Xcode 6
How to read CSV files in Pandas
Convert FBX files to ASCII <-> BINARY in Python
Summary of how to import files in Python 3
How to multi-process exclusive control in C language
Convert UTF-8 CSV files to read in Excel
How to check / extract files in RPM package
How to get the files in the [Python] folder
Sample API server to receive JSON in Golang
Batch convert PSD files in directory to PDF
[TF] How to build Tensorflow in Proxy environment
How to load files in Google Drive with Google Colaboratory
Preparing to script control Rhino objects in Grasshopper / Python
How to make an interactive CLI tool in Golang
How to use variables in systemd Unit definition files
How to download files from Selenium in Python in Chrome
How to add page numbers to PDF files (in Python)
2 ways to read all csv files in a folder
[Golang] Command to check the supported GOOS and GOARCH in a list (Check the supported platforms of the build)
Zero padding in Golang
Batch convert all xlsx files in the folder to CSV files
Allow Python to select strings in input files from folders
Command to list all files in order of file name