Highlights of Google Go

Preview:

Citation preview

HIGHLIGHTS OF GOOGLE GOBY RÉMON VAN DE KAMP

DomCode, April 2015

HISTORY OF GOOGLE GOCreated by Google, open source since November 2009Stable as of Go 1, early 2012Filled a gap between Python and JavaUsed by Google (dl.google.com, YouTube), Dropbox,Soundcloud, BBC, Docker, and more

LANGUAGE OVERVIEWProceduralCompiledGarbage collectedStrongly typedPointersNot (really) OOBuilt for concurrencyBuilt for developer ease

COMPILINGStatically linked binarySuper easy to deploy

Cross platformLinuxMacWindowsAndroid (beta, since 1.4)

Pre­compile imported packages

TYPING SYSTEMScalar types, e.g. bool, int, uint, float64, stringComposite types: maps, arrays, slicesInstead of objects there are structs

Or write a constructor like function

type Person struct Name string Age int

p := Person"Remon", 32

p := PersonName: "Remon", Age: 32

METHODSYou can add methods to structs

Not just to structs, to any type in the current package

func (p Person) Greet() string return "Hello!"

EMBEDDINGThere is no inheritance for structs, but there isembedding (composition)

Superhero now has all methods and fields Person has

type Person struct Name string Age int

type Superhero struct Person Superpower string

NO POLYMORPHISMYou can't pass a Superhero to a function that expects a

Person

INTERFACESInterfaces define a contract for functions on a typeApplied implicitly!

INTERFACES: EXAMPLEtype Greeter interface Greet() string

func hello(g Greeter) g.Greet()

INTERFACES APPLIED CROSS-PACKAGEAwesome for testing, e.g., to mock a package withexpensive callsHandy for pulling parts of third party libraries into yourown application

INTERFACE COMPOSITIONInterfaces are embeddable for composition too

So you can easily create "interface hierarchies"There is struct composition as well! (multiple inheritancewithout the inheritance part)

type SingGreeter interface Singer Greeter

INTERFACES MASK TYPESFor an interface parameter, only the methods defined in the

interface are availablefunc doStuff(g Greeter) g.UseSuperPower() // error: g.UseSuperPower undefined

INTERFACES MAKE INTENT CLEARAccept a network connection and make no promises

whatsoeverfunc foo(conn net.Conn)

or accept something we can write tofunc foo(writer io.Writer)

implies we won't try to close or read from it

WRITING IN GO1. Write some code2. Find common patterns within code3. Refactor/generalise patterns using interfaces

Great for testing as well!4. Goto 1

TYPE SYSTEM SUMMARYBasic scalar and composite typesInterfaces applied implictly, to other packages as wellInterfaces are a great alternative to inheritanceOpen up a number of great options not possible withinheritance

CONCURRENCYgoroutine: light­weight threadOnly 8KB stack initially

Separate schedulerCan use multiple CPUsBut defaults to 1 thread

Thousands of goroutines is nothing special

CONCURRENCYdoSomething()

go doSomething()

CONCURRENCY PRINCIPLEDo not communicate by sharing memory; instead, share

memory by communicating.

CHANNELSCommunication through channelswriting to full channel blocks executionreading from empty channel blocks executionsynchronous or buffered

Data races cannot occur, by design

CHANNELS: EXAMPLEfunc Add(c chan int, i, j int) c <­ i + j

c := make(chan int)

go Add(c, 1, 2)

result := <­c

SELECTselect is like switch, but for channelsListen to multiple channels at the same timeAct on incoming messages on channelsIf just one channel has a message, process itIf multiple channels have a message, pick one atrandom

CONCURRENCY SUMMARYVery lightweightEasy code with channels instead of complex code withmutexesUse select to listen to multiple channels

LESS GOODGo's garbage collector is not optimal for some problemsYou don't run into this easily, but beware for very large/ hard real time systemsThere are plans for improvement going forwardMax 10ms every 50msMax 25% of CPU cycles

Build cache sometimes gets in the wayCompile 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

SUMMARYStatically linked binaries, easy to deployInterfaces as a great alternative to inhertanceEasy and lightweight concurrency

MORE INFORMATIONGo website: A Tour of Go:

Go by example: An introduction to programming in Go, by Caleb Doxsey:

Golang Challenge: #golang on TwitterWhen 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 ALLrate my talk: twitter: github:

https://joind.in/14466@scallioxtxrpkamp