More on Packages

Last time I learned about writing and importing my own packages. This time, I want to learn about installing programs and packages.

I've been using the command:

go run program.go

To compile and run my programs. The
run command compiles the program and the GO packages that my program imports. After the program executes, the executable is thrown away. That is, you aren't left with a program that you can run in a standalone manner. In the first post I learned I can use the command

go build program.go

To create a true executable. Why have both? Well, one reason is that by using the
build command every time might result in executables where you don't want them. The go command keeps your directory environment clean of test, and temporary builds you might create. There is a third way to compile a program.

GO install




Last time I learned about the workspace, the directory named
go in your home directory that contains the packages to be imported. I also explained that it has the following subdirectories:

  • bin - executables
  • pkg - compiled package code
  • src - source code

By using the command:

go install program.go

GO will compile and install your binary executable into the
bin directory of your workspace. Here is an example of me compiling a program, I've called time.go:

Screen Shot 2021-03-08 at 8.34.46 AM

Ugh! Well that didn't go as planned. So what is GOBIN? My guess is it's an environment variable that hasn't been set. Also, I remember from last time that the workspace structure isn't created automatically. I don't have a bin subdirectory. What happens if I create it first? Another quick test and another quick failure. The phrase GOBIN not set should have been a big clue.


GOBIN



GOBIN turns out to be an environment variable, as I guessed. (Go with your first instinct.) How you set this variable depends on the system on which you run GO. I'm running GO on a Raspberry Pi (Linux), so I know the command to add this variable is:

export GOBIN="/home/pi/go/bin"

Where /home/pi/go/bin is the path to the bin subdirectory of my workspace. Time to try install again:



Screen Shot 2021-03-08 at 9.05.36 AM


It works! However, I now have to run my program by changing to the bin directory or running:

~/go/bin/time


To solve this, I can add the bin directory to my PATH variable. I also need to persist the GOBIN variable. I'll add this to my .profile file on the Pi.

GOPATH


There is another environment variable,
GOPATH which points to the top of your workspace, whereas GOBIN points to the bin directory. You set GOPATH the same way you do GOBIN.

You can change the
GOPATH variable to point to different workspaces you may have. This allows you to separate projects and different phases of a single project. One important thing to note is that GOBIN is overridden by GOPATH. That is, when you change GOPATH, the install command will use the bin directory pointed to by GOPATH. Why have both? Well some digging on the internet tells me most people just use GOPATH and don't worry about GOBIN. That's what I'll use.

This site does not track your information.