Introduction to Golang

The Go programming language, also known as Golang, was designed in 2007 (and first released in 2012) at Google in order to improve backend code productivity and efficiency. Its creators were motivated by problems they faced using C++ for systems programming. Therefore, Golang's main goals as a language became:

  • Static typing and run-time efficiency (like C++).
  • Readability and usability (like newer languages such as Python or Javascript). One usability concept deeply embraced by Go is automatic dependency management, like NodeJS does using Node Package Manager (npm). Golang uses the `go get` tool to automatically solve dependencies.
  • High-performance networking and multiprocessing: The goal here is to take as much advantage of multiprocessor architectures as possible, and favor horizontal scaling (i.e. improve just by adding machines).

The blue gopher is Go's mascot

package main

import "fmt"

func main() {
    fmt.Println("hello world")
}

After installing Go, you can build with go build your_file.go, and run with go run your_go. Simple!

Golang has a C-like syntax, has pointers types, but also garbage collection (which can be fine tuned and also disabled). It also eschews classes and inheritance in favor of structs, interfaces and type embedding (which is much closer to composition). A struct can have associated functions, much like a class would methods; however, there is no inheritance and no override mechanism. A struct can implement an interface, but it doesn't need to declare it explicitly: just defining functions matching the interface is enough.

Another typical OOP concept which Go abandons are exceptions; instead, an instance of error can be returned. A function can have multiple return values, so there is no need to return data via the arguments.

There are no generics in Go either; the closest concept is interface{}, which would be like the System.Object class from Java, or C#'s object: a catch-all type behind which any concrete type can be used. To help with this, there are also reflection mechanisms.

Concurrency mechanisms are first class citizens in Go: lightweight threads are called "goroutines", and they can communicate between each other by using "channels", which can be thought of as bidirectional blocking message queues. When a goroutine sends via a channel, it will block until another goroutine reads that message from the channel (this is the default, unbuffered behavior for channels; a backlog amount can be set when creating the channel to reduce blocking). With goroutines and channels only, lots of concurrency patterns are possible from the get go. Of course, if there is concurrency, and the need for mutual exclusion arises, mechanisms are provided.

A good place to get started is the official tour. For a deeper dive, there is also Effective Go. For more specific and up-to-date information, there's the official blog. Just like C++ has Bjarne Stroustrup, Go has Rob Pike. Another great source of material are the GopherCon keynotes from the Gopher Academy channel (I learned about memory profiling here).

This guy (Rob Pike) knows a thing or two

Stay tuned for more in-depth posts about Go!

Comments

Popular posts from this blog

VB.NET: Raise base class events from a derived class

Apache Kafka - I - High level architecture and concepts

Upgrading Lodash from 3.x to 4.x