Let's start with the Language Trend in Github.com. There is no doubt, Java tops the chart amongst all compiled languages. Java 1.0 was released in 1995 and we haven't had the need for another general-purpose programming language ever since.

Go, on the other hand recently announced its latest release. If the success stories of few early adopters are any indication, the language has a great future ahead.

You probably read and some of you probably tried the new language from Google, rightly named so – short and sweet. The language is getting popular by the day and for good reasons. Obviously for the mass, it is not the language of choice yet. Having said that, there is a huge community and momentum out there we cannot afford to ignore.

In this article, we're going to go through everything you need to know in the Go ecosystem. If you like starting from "Hello World", Start Here, play with Playground, read the FAQ and Effective Go.

Facts

  • Go offers a Playground for Developers to experiment and share code – gotta love that.
  • Go is Open Source and Small – The complete spec is less than 40 pages.
  • Go is a Compiled (Statically-Typed) language.
  • Go supports GC and Concurrency.
  • Go is Opinionated – leaves no room for ambiguity and interpretation. Checkout gofmtgolint and other productive commands.
  • Go is a Modern language – Designed for the web and teams. Supports networked and multi-core computing.
  • It is Simple and Fast – It has its origins in the C programming language.
  • Stay up-to-date with The Weekly Newsletter from Go.

Into the Weeds

In this section, we’ll deep-dive into some concepts that are important in the long run:

Zero Values

Zero values are the default values for various data types.

Function

Function is a

first-class

citizen in Go. A function in Go can take

varargs

, and can return multiple types. It can be

anonymous

and use

defer

to do things later.

Structure, Method and Receiver

Go is (technically) not Object-Oriented but gives you features that gets you close enough. You may have already guessed it by now. Yes, a Structure in Go allows you to capture both data and behavior.

The functions associated to a Structure are called

Methods

and the runtime structure object is called a

Receiver

. A receiver can be a pointer or a value object.

Interface

Read this thrice. Yes, three times.

An

Interface

in Go is used to define behavior (

Method

) and really

any type

can implement that behavior

implicitly

. There is no implements keyword. In other words, a type can satisfy many different interfaces simply by implementing the matching

Method

set for an interface.

Reflection & Type Conversion

Reflection is the ability to use runtime type information. Types and Interfaces are key here. It is very important to understand that all types are of type interface{} (an empty interface) implicitly. Go provides a powerful Reflection API and along with value.(type) you can accomplish a whole lot. Do checkout the Laws of Reflection.

Pointer vs. Value

There is a subtle (but huge) difference between these two and it is important to understand that distinction. Think about a link (URL) to a web-page as a Pointer and the content of the page as Value.

Functions (or Methods) interact with each other by (a) passing type/data and (b) receiving type/data. Either way it can be a Pointer(Pass by Reference) or a Value (Pass by Value). Although technically speaking, everything in Go is pass by value. The act of passing a pointer (an address) is still a pass by value. In case of a Pointer, the value you are passing is an address.

With a Pointer you can check against <nil> as well as change the original data. In contrast, with a Value you cannot check against <nil> and you cannot change the original data. So use Pointer to mutate and Value for immutability.

Let’s try the same example as before, but this time we’ll change the Receiver to a

Value

as opposed to a

Pointer

.

Goroutine and Channel

Goroutines are threads. Channels are a typed conduit through which you can send and receive values with the channel operator, <-. The direction of arrow determines the flow (to or from).

Vendoring

Vendoring in Go is what we know as dependency management. Go lets you use specific versions from third-party vendors go into a specific vendor folder. So here is how you can accomplish this:

  1. For Go 1.5 or earlier, export GO15VENDOREXPERIMENT=1
  2. Get the package you need using a dependency management tool

More here

Server Side

Server side programming has never been so easy. Yes, you can write a simple (or complicated) http server in Go with ease. No Web-Server or App-Server is needed.

Obviously you cannot run this example in Playground. So, save this example to a go file (say server.go) and run from command-line.

> go run <path>/server.go

This will start the server at port 8080. Now try

this

from your browser.

Dockerizing Go Programs

Again it is fairly simple. Use this Dockerfile and try the server we build earlier.

# Get image from https://hub.docker.com/_/golang/
FROM golang:1.6
# Setup Vendor (if needed)
#ENV GO15VENDOREXPERIMENT=1
# Copy your code to the image.
COPY . /go/src/github.com/my-repo/server
# Build & Install
RUN cd /go/src && go install github.com/my-repo/server
# Run the app
ENTRYPOINT /go/bin/server
# Expose port 8080.
EXPOSE 8080

Next Steps and Resources

And finally, do check us out at The Coral Project, our collaboration with Mozilla and The New York Times – we’re using Go heavily on the project. We’d love for you to learn, share and contribute Code there.