Getting started with Golang on macOS
Golang, or Go, is a programming language that is becoming massively popular for its ease of use for both developer and DevOps audiences. Let's set up Go on macOS to see how easy it is to start with Go.
Go was created at Google® with a purpose to resolve the problems and imperfections that persist in other languages. Golang was designed to be as efficient as C++, but be as intuitive as JavaScript and Python. However, if you are completely new to programming many people agree that Go might not be the best choice because it is considered a rather opinionated language. Anyways, if you want to get your feet wet with Golang, you need to first set it up and this article should help you with that.
Download Golang
First, download Go to your machine. The list of binaries and archives is available at https://golang.org/dl/. You need to run some of these commands as root.
Enable root user on your machine:
dsenableroot username = svetlana user password: root password: verify root password:
Example of system response
dsenableroot:: ***Successfully enabled root user.
Download the Go archive:
Example:
curl -o go1.12.1.darwin-amd64.tar.gz https://dl.google.com/go/go1.12.1.darwin-amd64.tar.gz
System response:
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 121M 100 121M 0 0 2292k 0 0:00:54 0:00:54 --:--:-- 1453k
Unarchive the Go tarball:
sudo tar -C /Users/svetlana -xzf go1.12.1.darwin-amd64.tar.gz
Verify that you have the contents of the Go tarball extracted:
ls /Users/svetlana/go
The go
directory must exist.
Configure PATH for Go
After downloading the Go archive file, you need to add go
to
your PATH
. By default, Go configures $HOME/go
as your
workspace directory. This is were all your Go tools and
third-party binaries are stored. If you want to use a different
directory, you can change it by configuring a GOPATH
variable
as needed. In this tutorial, we will use the default setting.
Open your
/usr/local/.profile
file for editing and add the following lines:export GOPATH=$HOME/goexport PATH=$GOPATH/bin:$PATH
Save and exit.
Verify Go is in the path:
which go
System response:
/Users/svetlana/go/bin/go
Verify Go version:
go version
Example of system response:
go version go1.11.1 darwin/amd64
Test the Go installation
To check that Go is running on your computer, you can run a simple script.
Go to the
$HOME/go/src/
directory.Create a directory called
yay
:mkdir yay
Create a simple Go script called
yay.go
:package main import "fmt" func main() { fmt.Printf("This is my first Go script. Yay!\n") }
Build the script:
go build
Run the script:
./yay
System response:
This is my first go script. Yay!
Congratulations! Go ahead and continue to play with Go. Do not forget to run "go build" every time you make changes to your script. Do not forget to check out the official Go website https://golang.org/.