Mac OS X: Install Go Programming Language
H
ow do I install Go language on Apple Mac OS X? How to setup GO lanuage on Mac OS X? How do I install Go language version 1.2+ on OS X?
Go or golang, is a programming language initially developed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. Just like Linux kernel it has many contributors from the open source community. From the official project site:
Go is an open source programming language that makes it easy to build simple, reliable, and efficient software. Go is distributed under a BSD-style license. It is expressive, concise, clean, and efficient. Its concurrency mechanisms make it easy to write programs that get the most out of multicore and networked machines, while its novel type system enables flexible and modular program construction. Go compiles quickly to machine code yet has the convenience of garbage collection and the power of run-time reflection. It’s a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language.
Install Xcode and gcc
Fig.01: Installing Xcode on OS X
First, you need to use the gcc that comes with Xcode. See how to install and use Xcode on Apple Mac OS X for more information.
Installing Golang on Mac OS X
Google provides the package file for OS X. All you have to do is visit this page and grab the latest version. Once downloaded, open it, and follow the prompts to install the Go tools. The package installs the Go distribution to /usr/local/go directory:
Golang installing on OS X using wizard
Golang install # 2
Golang install # 3
Golang – enter admin password
Golang install # 5
Golang install Completed
Verify your installation
Open the Terminal and type the following commands:
$ ls -l /usr/local/go
Sample outputs:
total 120
-rw-r–r– 1 root wheel 13577 Nov 29 03:13 AUTHORS
-rw-r–r– 1 root wheel 19578 Nov 29 03:13 CONTRIBUTORS
-rw-r–r– 1 root wheel 1479 Nov 29 03:13 LICENSE
-rw-r–r– 1 root wheel 1303 Nov 29 03:13 PATENTS
-rw-r–r– 1 root wheel 1112 Nov 29 03:13 README
-rw-r–r– 1 root wheel 5 Nov 29 03:16 VERSION
drwxr-xr-x 2 root wheel 272 Nov 29 03:13 api
drwxr-xr-x 2 root wheel 170 Nov 29 03:15 bin
drwxr-xr-x 4 root wheel 136 Nov 29 03:15 blog
drwxr-xr-x 8 root wheel 1292 Nov 29 03:13 doc
-rw-r–r– 1 root wheel 1150 Nov 29 03:13 favicon.ico
drwxr-xr-x 3 root wheel 510 Nov 29 03:13 include
drwxr-xr-x 3 root wheel 102 Nov 29 03:13 lib
drwxr-xr-x 22 root wheel 816 Nov 29 03:16 misc
drwxr-xr-x 6 root wheel 204 Nov 29 03:14 pkg
-rw-r–r– 1 root wheel 26 Nov 29 03:13 robots.txt
drwxr-xr-x 7 root wheel 782 Nov 29 03:13 src
drwxr-xr-x 16 root wheel 6800 Nov 29 03:13 test
Type the following command to display Go environment information:
$ go env
Sample outputs:
GOARCH= amd64
GOBIN=
GOCHAR= 6
GOEXE=
GOHOSTARCH= amd64
GOHOSTOS= darwin
GOOS= darwin
GOPATH=
GORACE=
GOROOT= /usr/local/go
GOTOOLDIR= /usr/local/go/pkg/tool/darwin_amd64
TERM= dumb
CC= gcc
GOGCCFLAGS= -g -O2 -fPIC -m64 -pthread -fno-common
CXX= g++
CGO_ENABLED= 1
Writing your first Go program
Create a text file called hello.go using a text editor such as vi/vim:
$ vi hello.go
Append the following code:
/* hello.go – My first Golang program */
package main
import fmt
func main() {
fmt.Printf( Hello, world\n )
}
Test and run hello.go, enter:
$ go run hello.go
Sample outputs:
Hello, world
To build an executable i.e. compile packages and dependencies into an executable file, enter:
$ go build hello.go
You will get an executable file called hello in the current directory:
$ ls -l hello
-rwxr-xr-x 1 vivek wheel 2188368 Dec 2 16:07 hello
$ file hello
hello: Mach-O 64-bit executable x86_64
You can run hello program as follows:
$ ./hello
Getting help
The go command is a tool for managing Go source code. To see basic help, type:
$ go help
Sample outputs:
Go is a tool for managing Go source code.
Usage:
go command [arguments]
The commands are:
build compile packages and dependencies
clean remove object files
env print Go environment information
fix run go tool fix on packages
fmt run gofmt on package sources
get download and install packages and dependencies
install compile and install packages and dependencies
list list packages
run compile and run Go program
test test packages
tool run specified go tool
version print Go version
vet run go tool vet on packages
Use go help [command] for more information about a command.
Additional help topics:
c calling between Go and C
gopath GOPATH environment variable
importpath import path syntax
packages description of package lists
testflag description of testing flags
testfunc description of testing functions
Use go help [topic] for more information about that topic.
To get more information on specific topic, type:
go help topicHere
go help build
go help build | less
References:
See the Golang home page and documenation for more information.