What is Go? Go or Golang is a programming language created by Google in 2009 by Robert Griesemer, Rob Pike, and Ken Thompson.
Go language is designed for building simple, fast and reliable software.
In this tutorial i’ll show how to install Go on MacOS, Ubuntu, CentOS and how to create a simple “Hello world!” program on Golang.
Install Go on MacOS, Ubuntu, CentOS
Download an archive with the latest version of Golang for your platform from the official download page (the current version of Go is 1.11.3).
$ curl -O https://dl.google.com/go/go1.11.3.linux-amd64.tar.gz
Extract the tar archive files into /usr/local
(default Go installation directory):
$ sudo tar -C /usr/local -xvzf go1.11.3.linux-amd64.tar.gz
Configure GoLang Environment
For a system-wide installation of Go it needs to add the following line at the bottom of /etc/profile
file:
export PATH=$PATH:/usr/local/go/bin
To specify a workspace (location where you store your Go code) it needs to set Go environment variable GOPATH
.
By default, GOPATH
is $HOME/go
, but you can change it to any location you want (except Go installation directory).
I also recommend to set GOBIN
environment variable that specifies location where to install compiled Go programs and add it to PATH
.
Set GOPATH
, GOBIN
and PATH
environment variables by adding the following lines at the bottom of your $HOME/.profile
file:
export GOPATH=$HOME/go export GOBIN=$GOPATH/bin export PATH=$PATH:/usr/local/go/bin:$GOBIN
Refresh profile:
$ source $HOME/.profile
Verify Golang installation by checking the Go version and environment:
$ go version $ go env
GoLang: Hello World! – Example
Create $GOPATH/src
directory:
$ mkdir -p $GOPATH/src
Create $GOPATH/src/hello.go
file with the following contents:
package main import "fmt" func main() { fmt.Printf("Hello World!\n") }
Run go install
to compile and install our “Hello World!” program on Go:
$ go install $GOPATH/src/hello.go
Execute hello
to run the program:
$ hello Hello World!
If the program returns “Hello World!”, then Go is successfully installed and functional.
Now, you’ll need to decide what platforms and architectures you want build executables for. In this tutorial, we’ll build executables for Windows 64-bit, Windows 32-bit, and 64-bit macOS. We will put these targets in an array with the format