Difference between revisions of "Golang/Install"

From Ever changing code
Jump to navigation Jump to search
 
(32 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Ubuntu Install =
= <code>goenv</code> - version manager =
Ubuntu LTS 16.04, this steps allow to install more modern version of Golang than comes from official Ubuntu repositories. There was major changes between in 1.7 but default version seems to be 1.6.2. At the time of writing this I installed version go1.10.2.
[https://github.com/syndbg/goenv/blob/master/INSTALL.md Install]
<source lang="bash">
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"
</source>
 
 
[https://github.com/syndbg/goenv/blob/master/HOW_IT_WORKS.md#choosing-the-go-version Usage]
<source lang="bash">
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
</source>
 
= Manual install on Ubuntu =
{{Note|Update to reflect Ubuntu 20.04}}
 
 
;Ubuntu 18.04, 20.04 inc. WSL, WSL2
<source lang="bash">
# 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
</source>


Download from https://golang.org/dl/ and extract it into <code>/usr/local</code>, creating a Go tree in <code>/usr/local/go</code>
 
<source>
Official way, download from https://golang.org/dl/ and extract it into <code>/usr/local</code>, creating a Go tree in <code>/usr/local/go</code>
sudo tar -C /usr/local -xzvf go$VERSION.$OS-$ARCH.tar.gz
<source lang="bash">
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
</source>
</source>
Add /usr/local/go/bin to the PATH environment variable. You can do this by adding this line to your <tt>/etc/profile</tt> (for a system-wide installation) or <tt>$HOME/.profile</tt>:  
 
<source>
 
sudo update-alternatives --install "/usr/bin/go" "go" "/usr/local/go/bin/go" 2 #adds to the $PATH
Add <code>/usr/local/go/bin</code> to the <code>PATH</code> environment variable. You can do this by adding this line to your <tt>/etc/profile</tt> (for a system-wide installation) or <tt>$HOME/.profile</tt>:  
export PATH=$PATH:/usr/local/go/bin #optional
<source lang="bash">
# 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
</source>
</source>


= Multiple versions =
== Multiple versions using <code>alternatives</code> ==
You can have multiple versions fo Golang installed in your system. The way to manage the current version in use is to use <code>update-alternatives</code>
You can have multiple versions fo Golang installed in your system. The way to manage the current version in use is to use <code>update-alternatives</code>


Display best options
;Display best options
<source lang="bash">
<source lang="bash">
sudo update-alternatives --display go
sudo update-alternatives --display go
Line 33: Line 85:
   2            /usr/bin/golang-go    10        manual mode
   2            /usr/bin/golang-go    10        manual mode
</source>
</source>
Action: Installed manually unzipped GoLang into <code>/usr/local/go</code> but was not added to alternatives. Use command below to add it on:
 
 
;Installed manually unzipped GoLang into <code>/usr/local/go</code> but was not added to alternatives. Use command below to add it on:
<source lang="bash">
<source lang="bash">
sudo update-alternatives --install "/usr/bin/go" "go" "/usr/local/go/bin/go" 2
sudo update-alternatives --install "/usr/bin/go" "go" "/usr/local/go/bin/go" 2
# | update-alternatives: --install needs <link> <name> <path> <priority>
</source>
</source>
After
After
Line 54: Line 109:
$ go version #the version of ''go'' will be the one from <tt>/usr/local/go</tt> location.
$ go version #the version of ''go'' will be the one from <tt>/usr/local/go</tt> location.
</source>
</source>
= [https://pkg.go.dev/github.com/posener/complete 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 [https://pkg.go.dev/github.com/posener/complete details in the example].
Supported shells: bash, zsh, fish
;Install
<source lang=bash>
go get -u github.com/posener/complete/gocomplete
gocomplete -install # Uninstall by gocomplete -uninstall
</source>
= Configuration and env variables =
== <code>GOROOT</code> and <code>GOPATH</code> ==
;<code>GOPATH</code> is discussed in the [http://golang.org/cmd/go/#hdr-GOPATH_environment_variable cmd/go] documentation:
The <code>GOPATH</code> 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 <code>GOPATH</code> is <code>$HOME/go</code> where <code>bin/</code> and <code>src/</code> directories exists. Each time you execute <code>go get -u <url></code>, the binaries will be placed in <code>$HOME/go/bin</code> and code in <code>$HOME/go/src</code>
* therefore <code>$HOME/go/bin</code> needs to be in the <code>$PATH</code>
<source lang=bash>
/usr/local/go    # default go directory is called $GOROOT
/usr/local/go/bin # default go bin directory needs to be in $PATH
</source>
{{Note|<code>GOPATH</code> must be set to get, build and install packages outside the standard Go tree.}}
;<code>GOROOT</code> is discussed in the [http://golang.org/doc/install#tarball_non_standard installation instructions]:
The Go binary distributions assume they will be installed in <code>/usr/local/go</code> (or <code>c:\Go</code> under Windows), but it is possible to install the Go tools to a different location. In this case you must set the <code>GOROOT</code> 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:
<source lang=bash>
export GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/bin
</source>
{{Note|GOROOT must be set only when installing to a custom location.}}
== <code>sudo go</code> ==
Allow 'sudo go <subcommand>'
<source>
sudo visudo # then add to'secure_path' '.../go/bin' directory
Defaults secure_path=".../bin:/usr/local/go/bin"
</source>
== Environment variables ==
Get go environment variables
<source>
$ 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"
</source>
= References =
* [https://dev.to/eternaldev/go-learning-for-loop-statement-in-depth-5bkc for loop] dev.to
Tutorials
* [https://golangbyexample.com/golang-comprehensive-tutorial/ GoLang by example] contains for-range loop
[[Category:Golang]]

Latest revision as of 18:32, 4 November 2022

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