An Introduction to Golang

The Go programming language is an open source project to make programmers more productive.

Go 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.

It combines the best of both statically typed and dynamically typed languages and gives you the right mixture of efficiency and ease of programming. It is primarily suited for building fast, efficient, and reliable server side applications.

Following are some of the most noted features of Go

  • Type safety and Memory safety.
  • Good support for Concurrency and communication.
  • Efficient and latency-free Garbage Collection
  • High speed compilation
  • Excellent Tooling support

Now 10 years in the wild, Google’s Go programming language has certainly made a name for itself. Lightweight and quick to compile, Go has stirred significant interest due to its generous libraries and abstractions that ease the development of concurrent and distributed (read: cloud) applications.

But the true measure of success of any programming language is the projects that developers create with it. Go has proven itself as a first choice for fast development of network services, software infrastructure projects, and compact and powerful tools of all kinds.

Here are 5 noteworthy projects written in Go, many of which have become more famous than the language they were written in. All of them have made a significant mark in their respective domains. All of the projects featured here are hosted on GitHub, so it’s easy for the Go-curious to take a peek at the Go code that makes them tick.

  • Docker
  • Kubernetes
  • Terraform
  • Fedora CoreOS
  • InfluxDB

Installing GO

As a Linux user for more than 17 years, installation and configuration explained here is for Linux and will work for almost all Linux distros. For installing go in Windows or Mac you can follow the GO official documentation here.

Go binary distributions are available for all major operating systems like Linux, Windows, and MacOS. It’s super simple to install Go from the binary distributions. The following link provides more information on the releases.

If a binary distribution is not available for your operating system, you can install Go from source.

We will be installing from the binary by downloading the tar ball,

Curl in action for download

Extracting the tar ball:

Extract using tar

Now we need to export the path so that our go binary will be available in your OS environment PATH variable.

Exporting PATH variable

If all the above steps are completed successfully we will be able to execute go from the command line and can be tested to print the version details.

GO Tool or Go command

Go is a tool for managing Go source code.

$ go [arguments]

Commands:

bug - start a bug report
build - compile packages and dependencies
clean - remove object files and cached files
doc - show documentation for package or symbol
env - print Go environment information
fix - update packages to use new APIs
fmt - gofmt (reformat) package sources
generate - generate Go files by processing source
get - add dependencies to current module and install them
install - compile and install packages and dependencies
list - list packages or modules
mod - module maintenance
run - compile and run Go program
test - test packages
tool - run specified go tool
version - print Go version
vet - report likely mistakes in packages

We can execute “go help ” for more information about a command.

GOPATH

Go requires you to organize your code in a specific way. All your Go code and the code you import, must reside in a single workspace. A workspace is a directory in your file system whose path is stored in the environment variable GOPATH. A Go Workspace is how Go manages our source files, compiled binaries, and cached objects used for faster compilation later. It is typical, and also advised, to have only one Go Workspace, though it is possible to have multiple spaces. The GOPATH acts as the root folder of a workspace. In Linux and other UNIX derivatives GOPATH defaults to $HOME/go, but this can be changed if needed by exporting appropriate path in the GOPATH variable.

We will now set the GOPATH variable and test it by printing the environment variable.

Variable GOPATH exported

Anatomy of Go workspace (GOPATH)

Inside of a Go Workspace, or GOPATH, there are three directories: bin, pkg, and src. Each of these directories has special meaning to the Go tool chain.

.
├── bin
├── pkg
└── src
. └── github.com/foo/bar
└── bar.go

Let’s take a look at each of these directories:

src: contains Go source files.

The src directory typically contains many version control repositories containing one or more Go packages. Every Go source file belongs to a package. You generally create a new subdirectory inside your repository for every separate Go package.

bin: contains the binary executables.

The Go tool builds and installs binary executables to this directory. All Go programs that are meant to be executables must contain a source file with a special package called main and define the entry point of the program in a special function called main().

pkg: contains Go package archives

All the non-executable packages (shared libraries) are stored in this directory. You cannot run these packages directly as they are not binary files. They are typically imported and used inside other executable packages.

Start Coding with a Hello World

First, make sure that you have created the Go workspace directory or GOPATH at $HOME/go or any other directory you wish and GOPATH variable exported. Next, create a new directory src/hello inside your workspace.

Creating $GOPATH/src/hello directory

Now we can create a file named hello.go with the following code

package main
import "fmt"
func main() {
fmt.Printf("Hello, World\n")
}

creating hello.go and listing it

The easiest way to run the above program is using the go run command as below.

run the hello.go program

The go run command compiles and runs your program at one go. If however, you want to produce a binary from your Go source that can be run as a standalone executable without using the Go tool, then use the go build command. The go build command creates an executable binary with the same name as the name of your immediate package (hello). You can run the binary file like so

Building hello app from hello.go

Learning GO

An interactive introduction to Go in three sections. The first section covers basic syntax and data structures; the second discusses methods and interfaces; and the third introduces Go’s concurrency primitives. Each section concludes with a few exercises that you can practice what you’ve learned.

You can take the tour online:

Or can install locally:

$ go get golang.org/x/tour

That’s all for now folks! I hope you’re all set to deep dive and learn more about the Go programming language.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.