Go also known as golang, is a compiled computer programming language developed by google. It is loosely based on the C language but designed to overcome some of its short comings. It is a general purpose language and can be used from server-side development to games and streaming media. It can easily be installed on a CentOS system. This guide assumes you have at least a basic working installation of a Linux system.
Install Go on CentOS
Clean up repositories
yum clean all
Ensure everything is up to date
yum update
Change directories
cd /usr/src
Download the compiled package, you can find the most recent release on the downloads page.
wget https://storage.googleapis.com/golang/go1.8.3.linux-amd64.tar.gz
Uncompress the archive
tar xfvz go1.8.3.linux-amd64.tar.gz
Move the binary and its applicable files to /usr/local
mv go /usr/local/
Export the following
export GOROOT=/usr/local/go
GOROOT defines the path you placed the Go package in
export GOPATH=$HOME/go-project
GOPATH tells go where your project is located, this can be anywhere
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
This appends both the GOPATH and GOROOT to your $PATH. You can also add this to your .bash_profile to automatically export these variables upon logging in.
Verify Go Installation
By typing ‘go version’ it will give you a print out of the version you currently have running
# go version
go version go1.8.3 linux/amd64
By typing ‘go env’ it will show the environment variables that are currently in use.
# go env
GOARCH=”amd64″
GOBIN=””
GOEXE=””
GOHOSTARCH=”amd64″
GOHOSTOS=”linux”
GOOS=”linux”
GOPATH=”/root/go-project”
GORACE=””
GOROOT=”/usr/local/go”
GOTOOLDIR=”/usr/local/go/pkg/tool/linux_amd64″
GCCGO=”gccgo”
CC=”gcc”
GOGCCFLAGS=”-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build167939199=/tmp/go-build -gno-record-gcc-switches”
CXX=”g++”
CGO_ENABLED=”1″
PKG_CONFIG=”pkg-config”
CGO_CFLAGS=”-g -O2″
CGO_CPPFLAGS=””
CGO_CXXFLAGS=”-g -O2″
CGO_FFLAGS=”-g -O2″
CGO_LDFLAGS=”-g -O2″
Test a Go project
Create the project directory you exported earlier:
mkdir $HOME/go-project
Change to that directory:
cd $HOME/go-project
Create a new file:
nano hello.go
Insert the following lines:
package main
import “fmt”
func main() {
fmt.Println(“hello world”)
}
Execute the file:
go run hello.go
The file should print the following
# go run hello.go
hello world
That’s it, you should now have a working Go installation.
Aug 7, 2017LinuxAdmin.io