Scripting to compile for multiple platforms
We’ve learned several different ways to compile for operating systems using the GOOS
and GOARCH
environment variables and using build tags. The Makefile
can fill up rather quickly with all the different combinations of GOOS
/GOARCH
pairs and scripting may provide a better solution if you want to generate builds for many more specific architectures.
Creating a bash script to compile in Darwin or Linux
Let’s start by creating a bash script. Let’s name it build.sh
. To create the file, I simply type the following:
touch build.sh
The preceding command creates the file when it does not exist. The file extension is .sh
, which, while unnecessary to add, clearly indicates that the file is a bash script type. Next, we want to edit it. If using vi
, use the following command:
vi build.sh
Otherwise, edit the file using the editor of your choice.
Adding the shebang
The first line of a bash script is called the...