How To Install And Test Go On CentOS

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 CentOSClean up repositoriesyum clean allEnsure everything is up to dateyum updateChange directoriescd /usr/srcDownload 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.gzUncompress the archivetar xfvz go1.8.3.linux-amd64.tar.gzMove the binary and its applicable files to /usr/localmv go /usr/local/Export the followingexport GOROOT=/usr/local/goGOROOT defines the path you placed the Go package inexport GOPATH=$HOME/go-projectGOPATH tells go where your project is located, this can be anywhereexport PATH=$GOPATH/bin:$GOROOT/bin:$PATHThis 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 InstallationBy 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/amd64By 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 projectCreate the project directory you exported earlier:mkdir $HOME/go-projectChange to that directory:cd $HOME/go-projectCreate a new file:nano hello.goInsert the following lines:package main
import “fmt”
func main() {
fmt.Println(“hello world”)
}Execute the file:go run hello.goThe file should print the following# go run hello.go
hello worldThat’s it, you should now have a working Go installation.Aug 7, 2017LinuxAdmin.io

Latest articles

Related articles