I have been working with Go (also known as Golang) for some time now, and I must tell you that the more I work with it, the more I like it. And, that's the inspiration to set me on this path of sharing my learning about Go. That said, here is what we will be talking about in this article:
- Introduction to Go
- Environment setup
- Hello, World! - following the tradition
Let's get started!
Introduction to Go
Go is a general-purpose language, which was designed with systems programming in mind. At first Go feels more or less like C or C++, but it's certainly more than that. Three key aspects that make Go standout are, that it is compiled, garbage collected and above all it has explicit support for concurrent programming.
Compiled
Go is compiled. When we say so, it's important to understand that it is compiled down to the processor level. This means that when we compile our code, it is compiled into a code that runs directly on the processor.
Garbage Collected
In general, garbage collection means that the memory is managed for you. Go free up a developer from worrying about allocating or freeing memory manually. In Go, garbage collection is very fast as compared to other programming languages that have built in garbage collection, where it can create some latency in the application at an unpredictable time.
Concurrent Programming
Go is concurrent, and this concurrency is built right into the language. The <em>Go routines</em> allow us to handle concurrency by communicating through messages or passing the data instead of sharing it. This gives Go, the capability of taking advantage of modern processor where we have multiple cores and multiple processors almost everywhere.
Environment Setup
To setup the environment and get started with Go, the first thing we need is to install Go and there is no better place (with description) than
Go's Downloads page. Once we select the package based on our OS, we land on a page that describes the steps to setup Go in the
PATH environment variable, followed by setting the
GOPATH.
Go Installation Directory
When we install Go, it creates a directory c:\go on Windows and /usr/local/go in UNIX. This is where all the necessary binaries are kept by Go to make command line tools, compiler and standard library work. So, when we import a package from Go's standard library, Go looks for that package in this directory.
If we want to change the installation directory of Go, we can very well do so. However, to let Go know about the change, we are required to update the value of GOROOT variable which is by default set to default installation directory. On Windows, we can do so by updating the GOROOT environment variable, and on UNIX we can edit the ~/.bash_profile and set the GOROOT as,
- // edit the ~/.bash_profile file
- export GOROOT=/usr/local/custom-go
I would suggest, not to change the Go root, until it's really required and is your last resort.
Go Workspace
A workspace is a way to facilitate project management in Go. A workspace is simply a directory on our system, where Go looks for source code files, manages dependency packages and build distribution binary files. We can have as many workspaces as we want, however, we must ensure that the GOPATH environment variable must point to current working workspace directory.
To learn more about Go workspace and its directory structure, do check out
this documentation page. Any Go workspace directory must have three subdirectories viz. src, pkg and bin. Refer the below image,
src is a project directory containing the Go source code (.go files). Also, any packages installed using go get command or any dependency packages reside in this directory. In Go, all the source files are contained in a package. Therefore, when we work on a project, we create a directory inside the $GOPATH/src directory, and keep our source files in it.
pkg is a directory that contains the Go package objects. When we build our Go source code, they are archived here and therefore have the
.a file extension. These files are created by the Go
pack tool, when we build a package. These files contain source information, debug symbols, and compiled package binary code. Package objects are compiled and archived files for specific platforms, and therefore we can see a platform architecture directory inside pkg directory (refer to the above image).
bin directory contains the binary executable files, which can be created using the go install command. The go install command internally first builds the package using the go build command and then outputs the binary executable files in the bin directory. Go uses the GOBIN environment variable to place the executables, which is by default set to $GOPATH/bin and can be changed.
Once everything is set, we can run the go env command to see the environment variables set for Go. Here is what it looks like on my Mac,
Note the values for GOROOT and GOPATH, and if everything looks fine we are all set to say "Hello, World!".
Hello, World! in Go (Golang)
Let's now continue following the longstanding tradition of writing a program which prints Hello, World!. To start with, please ensure that you have setup the environment, and the output of go env command shows what you expect. Here is what we need to do:
- create a directory inside $GOPATH/src as hello-world, or name it whatever you like
- inside this directory, create a file as hello.go
- open the file in your favourite editor and copy-paste the code given below
- save the file
- being inside the directory created above, run the go run hello.go command
- it will first build our code, run it for us and print the output on console
- package main
-
- import "fmt"
-
-
-
- func main() {
-
- fmt.Println("Hello, World!\n")
- }
The image below shows the above steps in action.
If you can see, "Hello, World!" printed on your console, congratulations, you have completed your first milestone. We will not dive into the program structure right now, but the inline comments in the code should give an idea about what each line does.
Summary
Go, also know as Golang, is a general purpose programming language which is based on C and C++, but is not limited. What makes go stand out the most is it's built in support for concurrency and garbage collection. In this article, however, we covered a brief introduction to Go, its working environment, and how we can setup Go to start coding. There is certainly much more to come. Go is easy to learn. To make the best out of Go, don't code your way in Go, code the way Go suggests and solve your problems.