Google Go
Web Technology Talks Meeting - 24.8.2010
          Moritz Haarmann
It includes a http-server
     thats why it‘s a web-technology.
Moritz Haarmann
• GTUG/NA Founding Member
• Interests: Android, iPhone, Web and Open
  Technologies
• 25y, writing my Bachelor Thesis
  ( CompScience ), HdM Stuttgart
• @derwildemomo
Thinks to talk about
Thinks to talk about
Why Google decided to go. ( Speculation )
Thinks to talk about
Why Google decided to go. ( Speculation )




Go Basics
Thinks to talk about
Why Google decided to go. ( Speculation )




Go Basics      Cool Ideas
Thinks to talk about
Why Google decided to go. ( Speculation )




                               Get you
Go Basics      Cool Ideas
                                going
Why Go?
C is still second-most used
   but almost 40 years old and notoriously unsafe
Java is not always an option
        read: System programming
Python etc. also rock
Same problem: Though they are cool, they cannot be
             applied to any problem
38 years after the
 invention of C
No real Alternative
Go
Go Basics
The Basics
The Basics
• Compiled, no Bytecode/Interpreter
The Basics
• Compiled, no Bytecode/Interpreter
• Static Typing
The Basics
• Compiled, no Bytecode/Interpreter
• Static Typing
• Concurrent, Functional, Imperative
  Paradigms satisfied
The Basics
• Compiled, no Bytecode/Interpreter
• Static Typing
• Concurrent, Functional, Imperative
  Paradigms satisfied
• Clean design, familiar C-like Syntax
The Basics
• Compiled, no Bytecode/Interpreter
• Static Typing
• Concurrent, Functional, Imperative
  Paradigms satisfied
• Clean design, familiar C-like Syntax
• Garbage Collector: its problem is not
  yours.
Always ask!
Hello World
package main

import "fmt"

func main() {
    fmt.Println("Hello, World")
}
Functions
with super-cow-powers.
Simple Function
  func simpleFunction() {
      (...)
  }

  func caller() {
      simpleFunction()
  }
Better Functions
Better Functions
func Function(an int) (int) {
    return an + 1
}
Better Functions
func Function(an int) (int) {
    return an + 1
}



func Function(an int) (returnValue int, another int)
{
    returnValue = an + in
    another = returnValue * 5
    return
}
Better Functions
func Function(an int) (int) {
    return an + 1
}



func Function(an int) (returnValue int, another int)
{
    returnValue = an + in
    another = returnValue * 5
    return
}
a,_ = Function(1)
Functionals

func execFunc(aFunc func()) {
    aFunc()
}
Functionals
func execFunc(aFunc func()) {
    aFunc()
    var anotherF = func(){
        fmt.Println("Thats another function")
    }
    execFunc(anotherF)
}
Packages
  in a short
Packages
• Form the core organizational unit
• one package - one file
• entry point: Package main with function
  main
• Function visibility: First letter decides!
  ( func invisible() vs. func Visible() )
• many packages shipped for common tasks,
  e.g. http
Package Example
 package main

 import "fmt"

 func main() {
     fmt.Println("Hello, World")
 }
a parallel world
Sharing Memory
 information a a information a
   information information a
 information a a information a
   information information a
 information a a information a
   information information a
 information a a information a
   information information a
 information a a information a
   information information a
 information a a information a
   information information a
Sharing by
Communicating

   information a information b
   information b information b
   information b information b
   information b information a
   information a information a
   information a information a
Do not communicate by
sharing memory; instead, share
  memory by communicating
Goroutines & Channels
Goroutine
func showGo() {
    go func(){
        time.Sleep(20)
        fmt.Println("parallel.")
    }
}
Channels
Channels
• First-Class Value Object
Channels
• First-Class Value Object
• Typed, e.g. chan int is a channel transporting
  ints.
Channels
• First-Class Value Object
• Typed, e.g. chan int is a channel transporting
  ints.
• buffered, that is async, or unbuffered and
  therefore synchronous channel
Channels
• First-Class Value Object
• Typed, e.g. chan int is a channel transporting
  ints.
• buffered, that is async, or unbuffered and
  therefore synchronous channel
• Brainfuck ( for now ): channels of channels!
Sorting in background
      c := make(chan int)
      go func() {
          list.Sort()
          c <- 1
      }()
      doSomethingForAWhile()
      <-c
WTF?
WTF?

• Goroutines and Channels hide the
  complexity of threads, mutexes and other
  hard-to-master stuff effectively
WTF?

• Goroutines and Channels hide the
  complexity of threads, mutexes and other
  hard-to-master stuff effectively
• Race conditions, deadlocks etc are
  impossible.
WTF?

• Goroutines and Channels hide the
  complexity of threads, mutexes and other
  hard-to-master stuff effectively
• Race conditions, deadlocks etc are
  impossible.
• Another underlying thought model
A word on types
A word on types
• Known types are known ( int, unsigned )
A word on types
• Known types are known ( int, unsigned )
• First-Class UTF-8 Strings
A word on types
• Known types are known ( int, unsigned )
• First-Class UTF-8 Strings
• Arrays, Maps
A word on types
• Known types are known ( int, unsigned )
• First-Class UTF-8 Strings
• Arrays, Maps
• Custom Types
A word on types
• Known types are known ( int, unsigned )
• First-Class UTF-8 Strings
• Arrays, Maps
• Custom Types
• Arrays++: Slices
A word on types
• Known types are known ( int, unsigned )
• First-Class UTF-8 Strings
• Arrays, Maps
• Custom Types
• Arrays++: Slices
• Pointers but no arithmetic ( guess why )
Slices
Slices

• Type- and Boundsafe Array Wrappers, that
  can be easily generated and passed around
Slices

• Type- and Boundsafe Array Wrappers, that
  can be easily generated and passed around
• Reference to underlying data
Slices

• Type- and Boundsafe Array Wrappers, that
  can be easily generated and passed around
• Reference to underlying data
• Safe, fast and easy to use
Interfaces
Interfaces


• Duck Typing-Style
Interfaces


• Duck Typing-Style
• Quite Useful
A bit OO


• Functions operating on a type
...
type Momo struct {
    a int
    b int
}

func (m *Momo) wakeUp {
    m.shakeAlmostToDeath()
}

var m = new(Momo)
m.wakeUp()
Not discussed today
Not discussed today
• A lot
Not discussed today
• A lot
• Allocation, Memory Handling
Not discussed today
• A lot
• Allocation, Memory Handling
• Built-In gimmicks ( Do.once, init )
Not discussed today
• A lot
• Allocation, Memory Handling
• Built-In gimmicks ( Do.once, init )
• Error Handling
Not discussed today
• A lot
• Allocation, Memory Handling
• Built-In gimmicks ( Do.once, init )
• Error Handling
• The bitchy compiler
Not discussed today
• A lot
• Allocation, Memory Handling
• Built-In gimmicks ( Do.once, init )
• Error Handling
• The bitchy compiler
• Embedding
Questions?
http://tinyurl.com/gotalk2010
            und danke!

Google Go Overview