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
Go offers a
for Developers to experiment and share code – gotta love that.
– The complete spec
is less than 40 pages.
Go is a Compiled
Go supports GC
Go is Opinionated
– leaves no room for ambiguity and interpretation. Checkout gofmt
and other productive commands
Go is a Modern
language – Designed for the web and teams. Supports networked and multi-core computing.
– It has its origins
in the C programming language.
Stay up-to-date with The Weekly Newsletter
Into the Weeds
In this section, we’ll deep-dive into some concepts that are important in the long run:
Zero values are the default values for various data types.
Structure, Method and Receiver
Go is 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.
Read this thrice. Yes, three times.
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 take multiple forms by simply associating Method
(same signature) from different Interfaces.
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).
With a Pointer
you can check against
as well as change the original data .
In contrast with a Value
you cannot check against
and you cannot change the original data. So use Pointer
to mutate and Value
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 in Go is what we know as dependency management. Go lets you use specific versions from third-party vendors go into a specific
folder. So here is how you can accomplish this:
For Go 1.5 or less,
- Get the package you need using a dependency management tool
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 /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