29
HIGHLIGHTS OF GOOGLE GO BY RÉMON VAN DE KAMP DomCode, April 2015

Highlights of Google Go

Embed Size (px)

Citation preview

Page 1: Highlights of Google Go

HIGHLIGHTS OF GOOGLE GOBY RÉMON VAN DE KAMP

DomCode, April 2015

Page 2: Highlights of Google Go

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

Page 3: Highlights of Google Go

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

Page 4: Highlights of Google Go

COMPILINGStatically linked binarySuper easy to deploy

Cross platformLinuxMacWindowsAndroid (beta, since 1.4)

Pre­compile imported packages

Page 5: Highlights of Google Go

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

Page 6: Highlights of Google Go

METHODSYou can add methods to structs

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

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

Page 7: Highlights of Google Go

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

Page 8: Highlights of Google Go

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

Person

Page 9: Highlights of Google Go

INTERFACESInterfaces define a contract for functions on a typeApplied implicitly!

Page 10: Highlights of Google Go

INTERFACES: EXAMPLEtype Greeter interface Greet() string

func hello(g Greeter) g.Greet()

Page 11: Highlights of Google Go

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

Page 12: Highlights of Google Go

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

Page 13: Highlights of Google Go

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

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

Page 14: Highlights of Google Go

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

Page 15: Highlights of Google Go

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

Great for testing as well!4. Goto 1

Page 16: Highlights of Google Go

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

Page 17: Highlights of Google Go

CONCURRENCYgoroutine: light­weight threadOnly 8KB stack initially

Separate schedulerCan use multiple CPUsBut defaults to 1 thread

Thousands of goroutines is nothing special

Page 18: Highlights of Google Go

CONCURRENCYdoSomething()

go doSomething()

Page 19: Highlights of Google Go

CONCURRENCY PRINCIPLEDo not communicate by sharing memory; instead, share

memory by communicating.

Page 20: Highlights of Google Go

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

Data races cannot occur, by design

Page 21: Highlights of Google Go

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

c := make(chan int)

go Add(c, 1, 2)

result := <­c

Page 22: Highlights of Google Go

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

Page 23: Highlights of Google Go

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

Page 24: Highlights of Google Go

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

Page 25: Highlights of Google Go

LESS GOOD (2)Error handling can become very verbose

Workarounds possible but not always obvious/trivial

if err != nil return 0, err

Page 26: Highlights of Google Go

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

Page 27: Highlights of Google Go

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/

Page 28: Highlights of Google Go

INSTALLATION IS EASY!Just download, extract, add the bin directory to your $PATH

and you're good to go!(pun intended)

Page 29: Highlights of Google Go

THAT'S ALLrate my talk: twitter: github:

https://joind.in/14466@scallioxtxrpkamp