Google Go!
Seminar aus Informatik
Martin Aigner 0621270
Alexander Baumgartner 0620345
Department of Computer Science
University of Salzburg

May 28, 2010

Aigner, Baumgartner (Uni Salzburg)

Google Go!

May 28, 2010

1 / 22

Data Allocation and Runtime Representation

1Basic Types

2

Arrays and Strings

3Slices

4

Maps

5

New and Make

Aigner, Baumgartner (Uni Salzburg)

Google Go!

May 28, 2010

2 / 22

Basic Types

Basic Types

Figure: Memory Layout of basic types
Aigner, Baumgartner (Uni Salzburg)

Google Go!

May 28, 2010

3 / 22

Basic Types

Struct Type

Figure: Memory Layout of structs

Aigner, Baumgartner (Uni Salzburg)

Google Go!

May 28, 2010

4 / 22

Basic Types

Struct Type

Figure: Memory Layout of composite structs

Aigner, Baumgartner (Uni Salzburg)

Google Go!

May 28, 2010

5 / 22

Arrays and Strings

Arrays and Strings

Figure: Memory Layout of a string

Aigner, Baumgartner (Uni Salzburg)

Google Go!

May 28, 2010

6 / 22

Slices

SlicesFigure: Slicing an array of integers

Aigner, Baumgartner (Uni Salzburg)

Google Go!

May 28, 2010

7 / 22

Maps

Maps

Maps are...
... built-in data structures to associate values of different types. Keys canbe any type for which the equality operator is defined.
integers
floatsstrings
pointerinterfaces (if the dynamic type supports equality)

Aigner, Baumgartner (Uni Salzburg)

Google Go!

May 28, 2010

8 / 22Maps

Maps
Example// c o m p o s i t e l i t e r a l c o n s t r u c t i o nv a r t i m e Z o n e = map [ s t r i n g ] i n t {" UTC " :0∗60∗60 ," EST " : −5∗60∗60 ,// and s o on}// a c c e s s i n g map v a l u e so f f s e t := t i m e Z o n e [ " EST " ]// c h e c k i n g 0 v . s . non−e x i s t a n v evar seconds intv a r ok b o o ls e c o n d s , ok = t i m e Z o n e [ t z ] //comma ok i d i o m

Aigner, Baumgartner (Uni Salzburg)

Google Go!

May 28, 2010

9 / 22

New and Make

New and Make

Newnew(T) returns a *T, a pointer to zeroed storage
ready to use
works transitivelyMakemake(T, args) returns a value of type T, not a pointerused for slices, maps and channels only
initialized complex datastructure

Aigner, Baumgartner (Uni Salzburg)

Google Go!

May 28, 2010

10 / 22

New and Make

Examples for New

Figure: Allocation with new

Aigner, Baumgartner (Uni Salzburg)

Google Go!

May 28, 201011 / 22

New and Make

Examples for Make

Figure: Allocation with makeAigner, Baumgartner (Uni Salzburg)

Google Go!

May 28, 201012 / 22

Concurrency

6

Share by communicating

7

Goroutines

8Channels

9Parallelization

10Example

Aigner, Baumgartner (Uni Salzburg)

Google Go!

May 28, 201013 / 22

Share by communicating

Share by communicating

SloganDo not communicate by sharing memory; instead, share memory bycommunicating
Shared values are passed around on channels
Only one goroutine has access to the value at any given time
Using channels to control access makes it easier to write clear, correct
programs
It can also be seen as a type-safe generalization of Unix pipes
For reference counts there is no need to put a mutex around the integer
variable

Aigner, Baumgartner (Uni Salzburg)

Google Go!

May 28, 201014 / 22

Goroutines

Goroutines
Goroutines are...
...functions executing in parallel with other goroutines in the same addressspace
Prefix a function or method call with the go keyword to run the callin a new goroutine
Hides many of the complexities of thread creation and management
Goroutines are multiplexed onto multiple OS threads
When the call completes, the goroutine exits, silently
func main() {go expensiveComputation(x, y, z)anotherExpensiveComputation(a, b, c)}Aigner, Baumgartner (Uni Salzburg)

Google Go!

May 28, 201015 / 22

Channels

Channels I
Channels combine...
...communication with synchronization
Shared values are passed around on channels
Like maps, channels are a reference type and are allocated with make
Channels can be bufferedWith a channel you can make one goroutine wait for an otherReceivers always block until there is data to receive
If the channel is unbuffered, the sender blocks until the receiver hasreceived the value
If the channel has a buffer, the sender blocks if the buffer is fullci := make(chan int) // unbuffered channel of integers
cs := make(chan *os.File, 100) // buffered channel of pointers to Files
Aigner, Baumgartner (Uni Salzburg)

Google Go!

May 28, 201016 / 22

Channels

Channels IIChannels combine...
...communication with synchronizationc := make(chan int) // Allocate a channel.
// Start the sort in a goroutine; when it completes, signal on the channel.
go func() {list.Sort()c <- 1 // Send a signal; value does not matter.
}()
doSomethingForAWhile()<- c // Wait for sort to finish; discard sent value.

Aigner, Baumgartner (Uni Salzburg)

Google Go!

May 28, 201017 / 22

Parallelization

Parallelization
If the calculation can be broken into separate pieces,...
...it can be parallelized, with a channel to signal when each piececompletes.

Current compilerimplementations will not parallelize code by default
Environment variable GOMAXPROCS sets the number of cores to use
Or call runtime.GOMAXPROCS(NCPU) from your code
A good example for parallelization is a request-broker.
We handle a defined number of requests in parallel and block incoming
requests if the maximum number is reached.

Aigner, Baumgartner (Uni Salzburg)

Google Go!

May 28, 201018 / 22

Example

Semaphore using a channel I (Code)var sem = make(chan int, MaxOutstanding)func handle(r *Request) {sem <- 1; // Wait for active queue to drain.
process(r); // May take a long time.
<-sem; // Done; enable next request to run.
}func Serve(queue chan *Request) {for {req := <-queue;
go handle(req); // Don’t wait for handle to finish.
}}

Aigner, Baumgartner (Uni Salzburg)

Google Go!

May 28, 201019 / 22

Example

Semaphore using a channel II (Figure)

Aigner, Baumgartner (Uni Salzburg)

Google Go!

May 28, 2010

20 / 22

The End

Thank You!

Aigner, Baumgartner (Uni Salzburg)

Google Go!

May 28, 2010

21 / 22References[1] The go programming language, 2010. URL http://golang.org.
[2] Go l4, 2010. URL
http://www.technovelty.org/code/go-l4.html.
[3] The go programming language blog, 2010. URL http://blog.golang.org/2010/05/new-talk-and-tutorials.html.
[4] Russ Cox. research!rsc, 2010. URL http://research.swtch.com/2009/11/go-data-structures.html.
[5] golang-nuts, 2010. URL
http://groups.google.com/group/golang-nuts.

Aigner, Baumgartner (Uni Salzburg)

Google Go!

May 28, 201022 / 22