Introduction to Golang

"Go is a programming language designed by Google to help solve Google's problems, and Google has big problems.”

Created inside Google by 3 Googlers
Rob Pike ; Ken Thompson ; Robert Griesemer

Started in Set 2007 ; Open-sourced in Nov 2009

 Golang History - Origins

Born out of frustration with existing languages and environments for systems programming

 

One had to choose either efficient compilation, efficient execution, or ease of programming

all three were not available in the same mainstream language

 

Programmers who could were choosing ease over safety and efficiency by moving to dynamically typed languages such as Python and JavaScript rather than C++ or, to a lesser extent, Java.

Rob Spike: "Golang was built during C++ compilation time"

 Golang History - Origins

Go is an attempt to combine the ease of programming of an interpreted, dynamically typed language with the efficiency and safety of a statically typed, compiled language

 

  • Aims to be modern, with support for networked and multicore computing ( focus on concurrency )

 

  • Intended to be fast: it should take at most a few seconds to build a large executable on a single computer.

Golang History - Motivation

Golang History - Ancestors

Mostly C family (basic syntax)

 

Input from the Pascal/Modula/Oberon family (declarations, packages)

 

Inspired by Hoare's CSP, such as Newsqueak and Limbo (concurrency)

Minimalistic

Reserved keywords

Java 8 language spec - 780 page PDF

Scala language spec - 191 page PDF

Go language spec - 51 page PDF

 Golang - Concurrency is not Parallelism

Concurrency
Programming as the composition of independently executing processes.

 

 
Parallelism
Programming as the simultaneous execution of (possibly related) computations.

Golang - Concurrency is not Parallelism

Concurrency is about dealing with lots of things at once.
Parallelism is about doing lots of things at once.
Not the same, but related.

 


Concurrency is about structure

Parallelism is about execution

 


Concurrency provides a way to structure a solution to solve a problem that may (but not necessarily) be parallelizable.

Single Process Execution

Concurrent Solution #1

Concurrent Solution #2

 Golang - Concurrency

Parallelization of the previous concurrent solution

 Golang - Concurrency

 Golang - Concurrency

Concurrency is a way to structure a program by breaking it into pieces that can be executed independently

 


In concurrent development we need a way to coordinate the independent executions

Shared memory with explicit synchronization

mutex ; pthreads ; monitors ; locks ( C, Java, C# )

 

Transactional Memory

memory shared between threads in atomic transactions. thread may make accesses to shared mutable variables and have guaranteed isolation ( Concurrent Haskell ; Clojure )

 

Message Passing

Threads no longer share mutable variables, but communicate by explicitly sending messages ( Golang ; Erlang )

 Golang - Concurrency coordination models

 Golang - Features

Compiled

 

Statically linked

 

Garbage Collected

 

Object Oriented ( sort of )

 

Concurrency at language level

Is

Functions as first class citizens

 

Structs and Interfaces

---- Default Zero Values

 

All code is UTF-8

 

Functions can return multiple named value

Pointers ( * and & )

 

Concurrency ( goroutines, channels, select )

 

Have

 Golang - Features

Don't Have

Exceptions

Method or operator overloading

Pointer arithmetic 

Generics

Circular dependencies among packages

Type inheritance ( hierarchy )

Type inference

 

 Golang - NO Features

Goroutines

It's an independently executing function, launched by a go statement.

 

It has its own call stack, which grows and shrinks as required.

 

It's very cheap. It's practical to have thousands, even hundreds of thousands of goroutines.

 

There might be only one thread in a program with thousands of goroutines.

 

Goroutines are multiplexed dynamically onto threads as needed to keep all the goroutines running.

Channels

Channels provide a way for goroutines to communicate and synchronize their execution

Select

A control structure unique to concurrency.

The reason channels and goroutines are built into the language.

Do not communicate by sharing memory. Share memory by communicating

Timeout fetch URLs

GOPATH

Use Cases

Systems tasks

  • Background jobs
  • I/O ops
  • "Heavy processing" that will improve with concurrency ( + parallelism )

APIs

  • Usually when need to scale

Who's using it?

 

  • SendGrid
  • Soundcloud
  • Cloudflare
  • Iron.io
  • Splice
  • Docker
  • Dropbox
  • Google ( surprise! )

Hello, Web

on top of net/http

  • Gorilla Toolkit
  • Martini
  • Beego
  • Goji
  • Revel

on top of database/sql

  • gorp
  • hood
  • qbs
  • beedb
  • gorm
  • xorm

How fast?

 

  • Iron.io gone from 30 servers to 2
  • Splice simple benchmark Go API vs Ruby API ( 50x faster ) 
  • PHP Laravel simple JSON ( 100x faster )

Web Apps

 

https://github.com/rif/lov3lyme

https://github.com/thraxil/reticulum/

http://stevenyue.com/2014/02/10/from-rubys-grape-to-martini-in-go-for-building-web-api-server/

 

Can mix with Rails ( auth ) https://github.com/mattetti/goRailsYourself

  • go fmt / go imports
  • go build
  • go run
  • go install
  • go get
  • go vet
  • godoc 

FINAL NOTES

Private / Public

  • type Public struct
  • type private struct

Really good documentation

go build -> single binary

Q&A?

Introduction to Golang

By José Duarte

Introduction to Golang

  • 843