HIGHLIGHTS OF GOOGLE GO
BY RÉMON VAN DE KAMP
DomCode, April 2015
HISTORY OF GOOGLE GO
Created by Google, open source since November 2009
Stable as of Go 1, early 2012
Filled a gap between Python and Java
Used by Google (dl.google.com, YouTube), Dropbox,
Soundcloud, BBC, Docker, and more
LANGUAGE OVERVIEW
Procedural
Compiled
Garbage collected
Strongly typed
Pointers
Not (really) OO
Built for concurrency
Built for developer ease
COMPILING
Statically linked binary
Super easy to deploy
Cross platform
Linux
Mac
Windows
Android (beta, since 1.4)
Pre­compile imported packages
TYPING SYSTEM
Scalar types, e.g. bool, int, uint, float64, string
Composite types: maps, arrays, slices
Instead of objects there are structs
Or write a constructor like function
type Person struct {
    Name string
    Age  int
}
p := Person{"Remon", 32}
p := Person{Name: "Remon", Age: 32}
METHODS
You can add methods to structs
Not just to structs, to any type in the current package
func (p Person) Greet() string {
  return "Hello!"
}
EMBEDDING
There is no inheritance for structs, but there is
embedding (composition)
Superhero now has all methods and fields Person has
type Person struct {
    Name string
    Age  int
}
type Superhero struct {
    Person
    Superpower string
}
NO POLYMORPHISM
You can't pass a Superhero to a function that expects a
Person
INTERFACES
Interfaces define a contract for functions on a type
Applied implicitly!
INTERFACES: EXAMPLE
type Greeter interface {
    Greet() string
}
func hello(g Greeter) {
    g.Greet()
}
INTERFACES APPLIED CROSS-PACKAGE
Awesome for testing, e.g., to mock a package with
expensive calls
Handy for pulling parts of third party libraries into your
own application
INTERFACE COMPOSITION
Interfaces are embeddable for composition too
So you can easily create "interface hierarchies"
There is struct composition as well! (multiple inheritance
without the inheritance part)
type SingGreeter interface {
    Singer
    Greeter
}
INTERFACES MASK TYPES
For an interface parameter, only the methods defined in the
interface are available
func doStuff(g Greeter) {
    g.UseSuperPower() // error: g.UseSuperPower undefined
}
INTERFACES MAKE INTENT CLEAR
Accept a network connection and make no promises
whatsoever
func foo(conn net.Conn) {
}
or accept something we can write to
func foo(writer io.Writer) {
}
implies we won't try to close or read from it
WRITING IN GO
1.  Write some code
2.  Find common patterns within code
3.  Refactor/generalise patterns using interfaces
Great for testing as well!
4.  Goto 1
TYPE SYSTEM SUMMARY
Basic scalar and composite types
Interfaces applied implictly, to other packages as well
Interfaces are a great alternative to inheritance
Open up a number of great options not possible with
inheritance
CONCURRENCY
goroutine: light­weight thread
Only 8KB stack initially
Separate scheduler
Can use multiple CPUs
But defaults to 1 thread
Thousands of goroutines is nothing special
CONCURRENCY
doSomething()
go doSomething()
CONCURRENCY PRINCIPLE
Do not communicate by sharing memory; instead, share
memory by communicating.
CHANNELS
Communication through channels
writing to full channel blocks execution
reading from empty channel blocks execution
synchronous or buffered
Data races cannot occur, by design
CHANNELS: EXAMPLE
func Add(c chan int, i, j int) {
    c <­ i + j
}
c := make(chan int)
go Add(c, 1, 2)
result := <­c
SELECT
select is like switch, but for channels
Listen to multiple channels at the same time
Act on incoming messages on channels
If just one channel has a message, process it
If multiple channels have a message, pick one at
random
CONCURRENCY SUMMARY
Very lightweight
Easy code with channels instead of complex code with
mutexes
Use select to listen to multiple channels
LESS GOOD
Go's garbage collector is not optimal for some problems
You don't run into this easily, but beware for very large
/ hard real time systems
There are plans for improvement going forward
Max 10ms every 50ms
Max 25% of CPU cycles
Build cache sometimes gets in the way
Compile errors based on cached information
LESS GOOD (2)
Error handling can become very verbose
Workarounds possible but not always obvious/trivial
if err != nil {
    return 0, err
}
SUMMARY
Statically linked binaries, easy to deploy
Interfaces as a great alternative to inhertance
Easy and lightweight concurrency
MORE INFORMATION
Go website: 
A Tour of Go: 
Go by example: 
An introduction to programming in Go, by Caleb Doxsey:
Golang Challenge: 
#golang on Twitter
When searching, search for "golang"
https://www.golang.org/
https://tour.golang.org/
http://www.gobyexample.com/
http://www.golang­book.com/
http://golang­challenge.com/
INSTALLATION IS EASY!
Just download, extract, add the bin directory to your $PATH
and you're good to go!
(pun intended)
THAT'S ALL
rate my talk: 
twitter: 
github: 
https://joind.in/14466
@scallioxtx
rpkamp

Highlights of Google Go