Golang/Install

From Ever changing code
< Golang
Revision as of 17:32, 4 November 2022 by Pio2pio (talk | contribs) (→‎Install on Ubuntu)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

goenv - version manager

Install

git clone https://github.com/syndbg/goenv.git ~/.goenv
cd ~/.goenv && git fetch --all && git pull # upgrade

# In Ubuntu, add to ~/.bashrc
export GOENV_ROOT="$HOME/.goenv"
export PATH="$GOENV_ROOT/bin:$PATH"
eval "$(goenv init -)"
export PATH="$GOROOT/bin:$PATH"
export PATH="$PATH:$GOPATH/bin"


Usage

goenv install 1.19.3
goenv versions
  1.19.3
goenv global
system # it means by default uses `go` system binary as if would goenv were not installed
goenv global 1.19.3

Manual install on Ubuntu

Note: Update to reflect Ubuntu 20.04


Ubuntu 18.04, 20.04 inc. WSL, WSL2
# List available versions
apt-cache policy golang-1.1?
sudo apt install golang-go    # default version

# Specific version
sudo apt install golang-1.16                     # requires adding to PATH or via update-alternatives where PATH get update automatically
locate -b "\go" | grep bin/go                    # ==> /usr/lib/go-1.16/bin/go
sudo update-alternatives --install "/usr/bin/go" "go" "/usr/lib/go-1.16/bin/go" 2


Official way, download from https://golang.org/dl/ and extract it into /usr/local, creating a Go tree in /usr/local/go

LATEST=$(curl -sL https://golang.org/VERSION?m=text); echo $LATEST
VERSION=$LATEST
VERSION=1.17 # 1.16.7
curl -O https://storage.googleapis.com/golang/go${VERSION}.linux-amd64.tar.gz
sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzvf go${VERSION}.linux-amd64.tar.gz


Add /usr/local/go/bin to the PATH environment variable. You can do this by adding this line to your /etc/profile (for a system-wide installation) or $HOME/.profile:

# Official
export PATH=$PATH:/usr/local/go/bin

# Optional using alternatives
sudo update-alternatives --install "/usr/bin/go" "go" "/usr/local/go/bin/go" 2 # updates PATH

# Set $GOPATH
export GOPATH=$HOME/go 
export PATH=$PATH:$GOPATH/bin

Multiple versions using alternatives

You can have multiple versions fo Golang installed in your system. The way to manage the current version in use is to use update-alternatives

Display best options
sudo update-alternatives --display go
go - auto mode
  link best version is /usr/local/go/bin/go
  link currently points to /usr/local/go/bin/go
  link go is /usr/bin/go
/usr/local/go/bin/go - priority 2

Before

Selection    Path                  Priority   Status
 ------------------------------------------------------------
   0            /usr/bin/golang-go     10        auto mode
   1            /usr/bin/gccgo-go      5         manual mode
   2            /usr/bin/golang-go     10        manual mode


Installed manually unzipped GoLang into /usr/local/go but was not added to alternatives. Use command below to add it on
sudo update-alternatives --install "/usr/bin/go" "go" "/usr/local/go/bin/go" 2
# | update-alternatives: --install needs <link> <name> <path> <priority>

After

Selection    Path                  Priority   Status
 ------------------------------------------------------------
 * 0            /usr/bin/golang-go     10        auto mode
   1            /usr/bin/gccgo-go      5         manual mode
   2            /usr/bin/golang-go     10        manual mode
   3            /usr/local/go/bin/go   2         manual mode

Then run command to change go binary available in your shell

sudo update-alternatives --config go  #then select ''3''

When you run

$ go version #the version of ''go'' will be the one from <tt>/usr/local/go</tt> location.

Shell autocompletion

Features

  • Complete go command, including sub commands and all flags.
  • Complete packages names or .go files when necessary.
  • Complete test names after -run flag.
  • it can add any Go program autocomplete to your shell, check for details in the example.

Supported shells: bash, zsh, fish

Install
go get -u github.com/posener/complete/gocomplete 
gocomplete -install # Uninstall by gocomplete -uninstall

Configuration and env variables

GOROOT and GOPATH

GOPATH is discussed in the cmd/go documentation

The GOPATH environment variable lists places to look for Go code. On Unix, the value is a colon-separated string. On Windows, the value is a semicolon-separated string. On Plan 9, the value is a list.

  • usually GOPATH is $HOME/go where bin/ and src/ directories exists. Each time you execute go get -u <url>, the binaries will be placed in $HOME/go/bin and code in $HOME/go/src
  • therefore $HOME/go/bin needs to be in the $PATH
/usr/local/go     # default go directory is called $GOROOT
/usr/local/go/bin # default go bin directory needs to be in $PATH

Note: GOPATH must be set to get, build and install packages outside the standard Go tree.


GOROOT is discussed in the installation instructions

The Go binary distributions assume they will be installed in /usr/local/go (or c:\Go under Windows), but it is possible to install the Go tools to a different location. In this case you must set the GOROOT environment variable to point to the directory in which it was installed.

For example, if you installed Go to your home directory you should add the following commands to $HOME/.profile:

export GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/bin

Note: GOROOT must be set only when installing to a custom location.

sudo go

Allow 'sudo go <subcommand>'

sudo visudo # then add to'secure_path' '.../go/bin' directory
Defaults secure_path=".../bin:/usr/local/go/bin"

Environment variables

Get go environment variables

$ go version 
go version go1.17 linux/amd64
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN="" # <-- if not set, defaults to $GOPATH/bin/ eg. /$HOME/piotr/go/bin/
GOCACHE="/home/piotr/.cache/go-build"
GOENV="/home/piotr/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/piotr/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/piotr/go" # <--
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go" # <--
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1651520096=/tmp/go-build -gno-record-gcc-switches"

References


Tutorials