Guide to Installing GO GoLang with PATH Environment variable in OpenBSD

Go or Golang has lots of PATH variables. Golang environment variables are name/value pairs that exist outside the program but can be referenced at any given time. There are many predefined environment variables in the Golang repository, usually Golang applications installed with binary files.

Trying to run the Golang environment variable PATH will redirect all Golang binaries and libraries to a specific directory that you have specified in the Golang PATH. On each operating system, the PATH environment variable is implemented differently, such as FreeBSD using the csh command. On OpenBSD we will use .profile to direct all installation results from Golang.

You can set the Golang environment variable PATH at any time, however, we prefer to use the environment variable PATH permanently.

In this post, we will cover the basics around the PATH environment variable and some of the default and most used Golang environment variables. We will also learn how to set the environment configuration of the Golang program installed on OpenBSD 7.5.


1. Golang Default PATH Environment variables

As we know, there are many environment variables that Golang includes when you install it and can be configured to your liking. Some of the most used are

GOROOT
This is the path where the Go standard library resides in your local file system. Usually you don't need to change it unless you want to use some other version of Go as well. By default OpenBSD places it in /usr/local/go.

GOPAT
This variable defines the root of your workspace. This tells Go where to look for your code. It stores all the files required for program development. Usually this file consists of several files that are generated when you carry out the go build command. The results from GOPAT come from go.mod and go.sum.

To see other PATHs of Golang environment variables you can run the command below. The result of the command below will display all Golang PATH information.

Golang env
ns3# go env
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/var/cache/.cache/go-build'
GOENV='/var/cache/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='openbsd'
GOINSECURE=''
GOMODCACHE='/var/cache/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='openbsd'
GOPATH='/var/cache/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/var/cache/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.3.openbsd-amd64'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/var/cache/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.3.openbsd-amd64/pkg/tool/openbsd_amd64'
GOVCS=''
GOVERSION='go1.22.3'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/usr/local/dnsproxy/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build460608726=/tmp/go-build -gno-record-gcc-switches'


2. Install Golang on OpenBSD

Usually each Golang operating system consists of several versions. To get the latest version of Golang, you must update the OpenBSD PKG package. Here's how to update the PKG package.

Update package PKG
ns3# pkg_add -uvi
The update process will take quite a long time, once finished you can continue installing Golang with the pkg_add command. Like the example below.

Install Golang
ns3# pkg_add go
Golang is a bit different from other programs, Goalng does not have a main configuration file. After you have finished installing Golang, it means that the GO language is ready for you to use in OpenBSD.


3. Setup Golang PATH Environment variables

In this section we will configure Golang's default PATH. We will move the Golang PATH according to our wishes. In this article, we will put all Golang library files in the /var/cache directory. The method is quite easy, you just change a few /root/.profile scripts.

Please look at the example of the script that we have created in /root/.profile below.

Script /root/.profile
# $OpenBSD: dot.profile,v 1.10 2023/11/16 16:03:51 millert Exp $
## sh/ksh initialization
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/sbin:/usr/local/bin

export PATH
: ${HOME='/root'}
: ${GOVERSION='go1.22+'}
: ${GOCACHE='/var/cache/.cache/go-build'}
: ${GOENV='/var/cache/.config/go/env'}
: ${GOMODCACHE='/var/cache/go/pkg/mod'}
: ${GOPATH='/var/cache/go'}
: ${GOROOT='/usr/local/go'}
: ${GOTOOLDIR='/usr/local/go/pkg/tool/openbsd_amd64'}
export HOME
export GOCACHE
export GOENV
export GOMODCACHE
export GOPATH
export GOROOT
export GOTOOLDIR
export GOVERSION
umask 022

case "$-" in
*i*)    # interactive shell
	if [ -x /usr/bin/tset ]; then
		eval `/usr/bin/tset -IsQ '-munknown:?vt220' $TERM`
	fi
	;;
esac
In the script above, we place the PATH GOCACHE, GOENV, GOMODCACHE and GOPATH in /var/cache.

To see how the Golang environment variable PATH changes, we will demonstrate installing a Golang application that we downloaded from Github.

Download ddns
ns3# cd /usr/local
ns3# git clone https://github.com/pboehm/ddns.git
To see changes in the Golang PATH location, we run the go build command, as in the example below.

Install ddns
ns3# cd ddns
ns3# go build -v ./...
Now you see the results in the /var/cache directory. New folders have appeared called ".cache" and "go". Run the ls command to see whether the Golang PATH has moved to /var/cache or not.

/var/cache
ns3# cd /var/cache
ns3# ls
.cache      fontconfig  go          rpki-client
Setting the Golang environment variable PATH can influence the process and results of our development and compilation projects, so that we can manage every program that runs with Golang. This PATH function can also adjust the capacity of your hard disk.
Iwan Setiawan

I Like Adventure: Mahameru Mount, Rinjani Mount I Like Writer FreeBSD

Post a Comment

Previous Post Next Post