1. 程式人生 > >How to install and setup golang > LinxLabs

How to install and setup golang > LinxLabs

In this post I’ll explain how you can setup golang on any 64bit Linux distribution (Fedora/Ubuntu/SuSE/CentOS/..you name it)

Make sure you are doing these steps from a non-root user ; I’m using a non-root user ‘linxlabs’ throughout this post

Download

Download ‘golang’ binary archive from https://golang.org/dl/


OR
Copy the URL of Linux binary from https://golang.org/dl/ to download it via ‘wget’ from command line

[email protected]:~> wget https://storage.googleapis.com/golang/go1.9.1.linux-amd64.tar.gz
--2017-10-09 23:52:00-- https://storage.googleapis.com/golang/go1.9.1.linux-amd64.tar.gz
Resolving storage.googleapis.com (storage.googleapis.com)... 74.125.68.128, 2404:6800:4003:c02::80
Connecting to storage.googleapis.com (storage.googleapis.com)|74.125.68.128|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 104197862 (99M) [application/x-gzip]
Saving to: ‘go1.9.1.linux-amd64.tar.gz’

100%[=======================================================================>] 104,197,862 9.90MB/s in 13s

2017-10-09 23:52:14 (7.48 MB/s) - ‘go1.9.1.linux-amd64.tar.gz’ saved [104197862/104197862]

[email protected]
:~>

Installation

Extract the contents using tar command.
You will see a directory called ‘go’ in your home directory

[email protected]:~> tar -xvf go1.9.1.linux-amd64.tar.gz
[email protected]:~> ls -lrt
total 101764
drwxr-xr-x 2 linxlabs users 4096 May 10 04:56 bin
drwxr-xr-x 11 linxlabs users 4096 Oct 5 00:24 go
-rw-r--r-- 1 linxlabs users 104197862 Oct 5 01:59 go1.9.1.linux-amd64.tar.gz
[email protected]
:~>

Environment Setup

To include go binaries in you path variable edit .bash_profile
(by the way this may vary based on user’s shell; I’m using default shell BASH – execute “echo ${SHELL}” to verify to know your shell )

[email protected]:~> vi .bash_profile
PATH=${PATH}:~/go/bin

To make the PATH variable set , either you may logout and login OR execute “source .bash_profile”

Verify Go setup

Execute “go version” to verify your installation

[email protected]:~> go version
go version go1.9.1 linux/amd64
[email protected]:~>
[email protected]:~> go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/linxlabs/go"
GORACE=""
GOROOT="/home/linxlabs/go"
GOTOOLDIR="/home/linxlabs/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
[email protected]:~>

Here you can see a set of default variables.
The ‘GOPATH’ environment variable specifies the location of your ‘workspace’.
The ‘GOROOT’ environment variable specifies the location of the ‘go installed directory’
You may set this if you installed “go” in a different path

I would recommend to set this path to different filesystem where you have redundancy on file system (Storage LUN , MD-RAID , LVM mirror )
This is to make sure you will not lose your hard work in case of a disk failure

So edit .bash_profile again and add your path . I’ll be setting it to a test directory called /home/linxlabs/go_workspace

[email protected]:~> echo "export GOPATH=/home/linxlabs/go_workspace" >>.bash_profile
[email protected]:~> source .bash_profile

Verify your work space again with “go env” command

[email protected]:~> go env |grep GOPATH
GOPATH="/home/linxlabs/go_workspace"
[email protected]:~>

Now create few mandatory directories in your workspace

Write your first golang program

So let’s write our first golang program

[email protected]:~> cd ${GOPATH}/src
[email protected]:~> vi hello.go
package main
import "fmt"
func main(){
fmt.Printf("hello, world\n")
}

Golang formating 

Above code looks ok , but its a bad coding style as there is no proper spacing or indentation
But go have an option to set the proper readable style formatting with ‘go fmt’

[email protected]:~/go_workspace/src> go fmt
hello.go

Now do a cat on hello.go , you will see beautiful coding style

[email protected]:~/go_workspace/src> cat hello.go
package main

import "fmt"

func main() {
       fmt.Printf("hello, world\n")
}

How to run your code

[email protected]:~/go_workspace/src> go run hello.go
hello, world

If you got a hello, world ; then congratulations , you made your first step to the world of ‘golang’

But , where is your binary which can be executed without running it from source code every time! ?

When you execute go run , go will create temporary files to build the code and it will be deleted after execution

Build binary

So to build the binary , execute below

[email protected]:~/go_workspace/src> go build hello.go
[email protected]:~/go_workspace/src> ls -lrt
total 1832
-rw-r--r-- 1 linxlabs users 74 Oct 10 00:34 hello.go
-rwxr-xr-x 1 linxlabs users 1868323 Oct 10 00:42 hello
[email protected]:~/go_workspace/src>

Now you can see a binary file called ‘hello’ which can be executed

[email protected]:~/go_workspace/src> ./hello
hello, world

Install binary to a path

we will see ,how we can ‘install’ it to the bin directory – (mimic of make install)

[email protected]:~/go_workspace/src> go install
go install: no install location for directory /home/linxlabs/go_workspace/src outside GOPATH
For more details see: 'go help gopath'

We got an error saying that our present directory is outside of GOPATH ; which is true . Now what we can do to over come this ?

Here we will export one more variable GOBIN to install binaries

[email protected]:~/go_workspace/src> export GOBIN=/home/linxlabs/go_workspace/bin
[email protected]:~/go_workspace/src> go install hello.go
[email protected]:~/go_workspace/src> ls ../bin/
hello

You have to make sure the GOBIN is persistent by appending it to .bash_profile

[email protected]:~/go_workspace/src> echo "export GOBIN=/home/linxlabs/go_workspace/bin" >>~/.bash_profile

In next post I’ll show you how to integrate your github account with go and how to commit to your repository with go