SlideShare a Scribd company logo
Beauty and
Power of Go
Frank Müller / Oldenburg / Germany
Frank Müller

Born 1965 in Oldenburg

Software Development
           since 1984

     Author since 1999

         Book about Go
                in 2011



  Introduction
© Tarandeep Sing



Let‘s talk about Go
• End of 2007 start by Rob Pike, Ken
 Thompson and Robert Griesemer

• Implementation started after design
 in mid 2008

• Going public in November 2009
• Release of Go 1 in March 2012

               History
• Rob Pike
 • Unix, Plan 9, Inferno, acme, Limbo,
   UTF-8

• Ken Thompson
 • Multics, Unix, B, Plan 9, ed, UTF-8
 • Turing Award

             Famous parents
“
Go aims to combine the safety
and performance of a statically
typed compiled language with the
expressiveness and
convenience of a dynamically
typed interpreted language.
It also aims to be suitable for
modern systems - large scale -
programming.
                         – Rob Pike
// Organized in packages.
package main

// Packages can be imported.
import “fmt“

// Simple function declaration.
func sayHello(to string) {
   // Package usage by prefix.
   fmt.Printf(“Hello, %s!n“, to)
}

// Main program is function main() in package main.
func main() {
   sayHello(“World“)
}




                  Hello, World!
• Roots in C and Plan 9
• Native binaries with garbage
 collection

• Static typing
• Concurreny
• Interfaces
• Powerful tools
• No fantastic new pur language
              Some quick facts
“
It is better to have a permanent
income than to be fascinating.
                   – Oscar Wilde
bool     rune      string      uintptr


byte      uint / uint8 - uint64      int / int8 - int64


 float32 / float64            complex64 / complex128




struct        array         slice        map    pointer

       interface               function

       channel



           Large set of standard types
// Declaration can be done this way:
var i int

i = myIntReturningFunction()

// More usual is:
i := myIntReturningFunction()

// Even for complex types:
s := []string{“Hello“, “World“}
m := map[string]string{
   “foo“: “bar“,
   “baz“, “yadda“,
}

// Or multiple values:
v, err := myFunctionWithPossibleError()




               Implicit declaration
// Alias for a simple type.
type Weight float64

func (w *Weight) String() string {
   return fmt.Sprintf(“%.3f kg“, w)
}

// Struct with hidden fields.
type Name struct {
   first string
   last string
}

func (n *Name) String() string {
   return n.first + “ “ + n.last
}

func (n *Name) SetLast(l string) {
   n.last = l
}




          Own types can have methods
// Two-dimensional object.
type BasePlate struct {
   width float64
   depth float64
}

func (bp *BasePlate) Area() float64 {
   return bp.width * bp.depth
}

// Use the base plate.
type Box struct {
   BasePlate
   height float64
}

func (b *Box) Volume() float64 {
   return b.Area() * b.height
}




         No inheritence, but embedding
// Create a base plate.
func NewBasePlate(w, d float64) *BasePlate {
   return &BasePlate{w, d}
}

// Create a box.
func NewBox(w, d, h float64) *Box {
   return &Box{BasePlate{w, d}, h}
}

// Create an illuminated box.
func NewIlluminatedBox() *IlluminatedBox {
   ib := &IlluminatedBox{
      box:           NewBox(1.0, 1.0, 1.0),
      lightOnHour:   22,
      lightDuration: 8 * time.Hour,
   }
   go ib.backend()
   return ib
}




        No contructors, simple functions
© www.freeimages.co.uk



Interfaces
• Important abstraction
• Duck typing à la Go
• Set of method signatures
• No explicit implementation


              Interfaces
// Interface declaration.
type Duck interface {
   Quack() string
}

// First duck implementation.
type SimpleDuck Name

func (sd *SimpleDuck) Quack() string {
   return fmt.Sprintf(“Quack, my name is %s“, sd)
}

// Second duck implementation.
type ComplexDuck struct {
   name Name
   box *IlluminatedBox
}

func (cd *ComplexDuck) Quack() string {
   return fmt.Sprintf(“Quack, my name is %s “ +
      “and my box has %.3f m3.“, cd.name,
      cd.box.Volume())
}



       Declare and implement interfaces
// A pond with ducks.
type Pond struct {
   ducks []Duck
}

// Add ducks to the pond.
func (p *Pond) Add(ds ...Duck) {
   p.ducks = append(p.ducks, ds...)
}

// Add ducks to the pond.
func main() {
   p := &Pond{}
   huey := NewSimpleDuck(...)
   dewey := NewComplexDuck(...)
   louie := NewAnyDuck(...)

    p.Add(huey, dewey, louie)

    for _, duck := range p.ducks {
       println(duck.Quack())
    }
}



                   Use interfaces
// An empty interface has no methods.
type Anything interface{}

// Type switch helps to get the real type.
func foo(any interface{}) error {
   switch v := any.(type) {
   case bool:
      fmt.Printf(“I‘m a bool: %v“, v)
   case ComplexType:
      fmt.Printf(“I‘m a complex type: %v“, v)
   default:
      return errors.New(“Oh, type is unexpected!“)
   }
   return nil
}

// Or even shorter with type assertion.
v, ok := any.(WantedType)
if !ok {
   return errors.New(“Expected a ‘WantedType‘!“)
}




                 Empty interface
Functions
• First-class functions
• Higher-order functions

• User-defined function types
• Function literals
• Closures

• Multiple return values

       Flexible world of functions
// Function type to do something with ducks.
type DuckAction func(d Duck) error

// Pond method to let the ducks do something.
func (p *Pond) LetDucksDo(a DuckAction) error {
   for _, d := range p.ducks {
      if err := a(d); err != nil {
         return err
      }
   }
}

// Use a duck action.
func CollectQuacks(p *Pond) ([]string, error) {
   quacks := []string{}
   if err := p.LetDucksDo(func(d Duck) error {
      quacks = append(quacks, d.Quack)
      return nil
   }); err != nil {
      return nil, err
   }
   return quacks, nil
}



        Full bandwidth of function usage
Concurrency
•   Lightweight goroutines running in a
    thread pool

•   Up to thousands of goroutines

• Synchronization and communication
    via channels

• Multi-way concurrent control via
    select statement



               Concurrency
• Concurrency is the composition of
  independently executing processes

• Parallelism is the simultaneous
  execution of computations in a
  context




      Concurrency is not parallelism
• Modern applications deal with lots of
 things at once

• Concurrency allows to structure
 software for these kinds of
 applications




 Structuring applications with concurrency
// A goroutine is just a function.
func fileWrite(filename, text string) {
   f, err := file.Open(filename)
   if err != nil {
      log.Printf(“fileWrite error: %v“, err)
   }
   defer f.Close()

    f.WriteString(text)
}

// It is started with the keyword ‘go‘.
func main() {
   f := “...“
   t := “...“

    // Write file in the background.
    go fileWrite(f, t)

    // Meanwhile do something else.
    ...
}




                  Simple goroutine
// Read strings and convert them to upper case.
func pipe(in <-chan string, out chan<- string) {
   for s := range in {
      out <- strings.ToUpper(s)
   }
}

// Use buffered channels for async. communication.
func main() {
   in := make(chan string, 50)
   out := make(chan string, 50)

    go pipe(in, out)

    in <- "lower-case string"
    s := <-out

    // Prints “LOWER-CASE STRING“.
    fmt.Println(s)
}




     Pipeline goroutine using range statement
// Job with four parts.
func job(s *Source, t *Target) {
   a := partA(s)
   b := partB(a)
   c := partC(b)
   d := partD(c)
   t.Set(d)
}

// Start job multiple times.
func parallel(s *Source, t *Target) {
   for i := 0; i < 4; i++ {
      go job(s, t)
   }
}




   Naive approach leads to syncing problems
// Start a pipeline of parts.
func concurrent(s *Source, t *Target) {
   abChan := make(chan *AResult, 5)
   bcChan := make(chan *BResult, 5)
   cdChan := make(chan *CResult, 5)
   waitChan := make(chan bool)

    go   partA(s, abChan)
    go   partB(abChan, bcChan)
    go   partC(bcChan, cdChan)
    go   partD(cdChan, t, waitChan)

    <-waitChan
}




                   Pipelined approach
// Type X managing some data.
type X struct { ... }

// Backend of type X.
func (x *X) backend() {
   for {
      select {
      case r := <-w.requestChan:
         // One request after another.
         w.handleRequest(r)
      case c := <-w.configChan:
         // A configuration change.
         w.handleConfiguration(c)
      case <-w.stopChan:
         // X instance shall stop working.
         return
      case <-time.After(5 * time.Second):
         // No request since 5 seconds.
         w.handleTimeout()
      }
   }
}




   Complex scenarios using select statement
// A possible request.
type Request struct {
   f            func() *Response
   responseChan chan string
}

// Public method to add an exclamation mark.
func (x *X) Exclamation(in string) string {
   // Also x could be modified here.
   f := func() { return in + “!“ }
   req := &Request{f, make(chan string)}
   x.requestChan <- req
   return <-req.responseChan
}

// Using the requests closure in the backend.
func (x *X) handleRequest(req *Request) {
   res := req.f()
   x.responses = append(x.responses, res)
   req.responseChan <- res
}




         Request and response handling
// Start 10 pipes to do the work.
func balancedWorker(ins []string) []string {
   in, out := make(chan string), make(chan string)
   outs := []string{}
   // Start worker goroutines.
   for i := 0; i < 10; i++ {
      go worker(in, out)
   }
   // Send input strings in background.
   go func() {
      for _, is := range ins {
         in <- is
      }
      close(in)
   }()
   // Collect output.
   for os := range out {
      outs = append(outs, os)
      if len(outs) == len(ins) {
         break
      }
   }
   return outs
}



              Simple load balancing
// Receiving data.
select {
case data, ok := <-dataChan:
   if ok {
      // Do something with data.
   } else {
      // Channel is closed.
   }
case <-time.After(5 * time.Seconds):
   // A timeout happened.
}

// Sending data.
select {
case dataChan <- data:
   // Everything fine.
case <-time.After(5 * time.Seconds):
   // A timeout happened.
}




               Adding some safety
No exceptions?
• error is just an interface
• Errors are return values
• defer helps to ensure cleanup tasks

• panic should be used with care
• recover can handle panics
• Leads to clear and immediate error
 handling


        Error, panic and recover
// Error for non-feedable ducks.
type NonFeedableDuckError struct {
   Duck Duck
   Food *Food
}

// Fullfil the error interface.
func (e *NonFeedableError) Error() string {
   return fmt.Sprintf(“It seems %v dislikes %v, “,
      “maybe a rubber duck?“, e.Duck, e.Food)
}

// Error as only return value.
func FeedDucks(p *Pond, f *Food) error {
   return p.LetDucksDo(func(d Duck) error {
      // Type assert for ‘FeedableDuck‘ interface.
      if fd, ok := d.(FeedableDuck); ok {
         return fd.Feed(f)
      }
      return &NonFeedableDuckError{d, f}
   })
}




    Definition and usage of own error type
// Retrieve data from a server.
func Retrieve(addr, name string) (*Data, error) {
   conn, err := net.Dial(“tcp“, addr)
   if err != nil {
      return nil, err
   }
   defer conn.Close()
   // Do the retrieving.
   ...
   return data, nil
}

// Somewhere else.
data, err := Retrieve(“localhost:80“, “/foo/bar“)
if err != nil {
   // Handle the error.
   ...
}




 Error in multi-value return, cleanup with defer
// Save execution of function ‘f‘.
func safeExec(f func()) (err error) {
   defer func() {
      if r := recover(); r != nil {
         err = fmt.Sprintf(“Ouch: %v!“, r)
      }
   }
   f()
   return nil
}

// Somewhere else.
var result float64

err := safeExec(func() {
   // Divide by zero.
   result = 1.0 / (1.0 - 1.0)
})
if err != nil {
   // Do the error management.
   ...
}




                panic and recover
Packages
• Network / HTTP / Template
• Encodings (JSON, XML, ...)

• I/O, Formatting
• Crypto
• Images

• Time
• Reflection
      Large set of useful packages
• Import of external packages
• Namespace based on SCM

  • BitBucket, GitHub
  • Google Code, Launchpad
• Example:

  • “code.google.com/p/tcgl/redis“

           External packages
The go tool
• build - build the software
• fmt - format the sources
• test - run unit tests

• install - install the package
• get - retrieve and install an external
  package

• doc - source documentation
     Most important go subcommands
• http://golang.org/
• http://tour.golang.org/
• http://blog.golang.org/
• http://godashboard.appspot.com/
• http://go-lang.cat-v.org/
• http://www.reddit.com/r/golang/
• #go-nuts on irc.freenode.net
• Google Group golang-nuts
                Some links
Thank you for listening.


       Questions?


And now some practice ...

More Related Content

PPTX
Golang iran - tutorial go programming language - Preliminary
PPT
Concurrency in go
PDF
Go Lang Tutorial
PPT
Inheritance compiler support
PDF
scala-gopher: async implementation of CSP for scala
PDF
Hey! There's OCaml in my Rust!
PDF
A deep dive into PEP-3156 and the new asyncio module
PDF
Objective-C Blocks and Grand Central Dispatch
Golang iran - tutorial go programming language - Preliminary
Concurrency in go
Go Lang Tutorial
Inheritance compiler support
scala-gopher: async implementation of CSP for scala
Hey! There's OCaml in my Rust!
A deep dive into PEP-3156 and the new asyncio module
Objective-C Blocks and Grand Central Dispatch

What's hot (20)

PDF
Csp scala wixmeetup2016
PDF
The future of async i/o in Python
PPTX
Hacking Go Compiler Internals / GoCon 2014 Autumn
PDF
Using Flow-based programming to write tools and workflows for Scientific Comp...
ODP
Rust言語紹介
PDF
7 Common Mistakes in Go (2015)
PDF
Demystifying the Go Scheduler
PDF
Introduction to Go programming language
PDF
Introduction to kotlin coroutines
PPTX
Python
PDF
O caml2014 leroy-slides
PPTX
05 pig user defined functions (udfs)
PDF
7 Common mistakes in Go and when to avoid them
PDF
Golang and Eco-System Introduction / Overview
PDF
4. Обработка ошибок, исключения, отладка
PDF
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
PDF
Transmogrifier: Migrating to Plone with less pain
ZIP
Intro to Pig UDF
PPTX
Go Programming Language (Golang)
Csp scala wixmeetup2016
The future of async i/o in Python
Hacking Go Compiler Internals / GoCon 2014 Autumn
Using Flow-based programming to write tools and workflows for Scientific Comp...
Rust言語紹介
7 Common Mistakes in Go (2015)
Demystifying the Go Scheduler
Introduction to Go programming language
Introduction to kotlin coroutines
Python
O caml2014 leroy-slides
05 pig user defined functions (udfs)
7 Common mistakes in Go and when to avoid them
Golang and Eco-System Introduction / Overview
4. Обработка ошибок, исключения, отладка
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
Transmogrifier: Migrating to Plone with less pain
Intro to Pig UDF
Go Programming Language (Golang)
Ad

Similar to Beauty and Power of Go (20)

PPTX
golang_getting_started.pptx
PDF
Introduction to go
PDF
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
PDF
golang_refcard.pdf
PDF
Go ahead, make my day
PDF
Ruby Programming Assignment Help
PDF
Ruby Programming Assignment Help
PPTX
Next Generation Language Go
PDF
Go serving: Building server app with go
PDF
Let's Go-lang
PDF
Geeks Anonymes - Le langage Go
PDF
От Java Threads к лямбдам, Андрей Родионов
KEY
PPTX
The GO Language : From Beginners to Gophers
PPTX
Things about Functional JavaScript
PDF
Functions And Header Files In C++ | Bjarne stroustrup
PDF
От Java Threads к лямбдам, Андрей Родионов
PDF
What can be done with Java, but should better be done with Erlang (@pavlobaron)
PDF
Wien15 java8
PPTX
ProgrammingwithGOLang
golang_getting_started.pptx
Introduction to go
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
golang_refcard.pdf
Go ahead, make my day
Ruby Programming Assignment Help
Ruby Programming Assignment Help
Next Generation Language Go
Go serving: Building server app with go
Let's Go-lang
Geeks Anonymes - Le langage Go
От Java Threads к лямбдам, Андрей Родионов
The GO Language : From Beginners to Gophers
Things about Functional JavaScript
Functions And Header Files In C++ | Bjarne stroustrup
От Java Threads к лямбдам, Андрей Родионов
What can be done with Java, but should better be done with Erlang (@pavlobaron)
Wien15 java8
ProgrammingwithGOLang
Ad

More from Frank Müller (20)

PDF
IT-Tage 2024: Philosophie in der Software-Architektur
PDF
JAX 2023 - Cloud Provider APIs
PDF
JAX 2023 - Generics in Go
PDF
Let The Computer Do It
PDF
Concurrency with Go
PDF
2021 OOP - Kubernetes Operatoren
PDF
DevOpsCon - Verteilte Entwicklung in Go
PDF
Devs@Home - Einführung in Go
PDF
Fun with functions
PDF
Ein Gopher im Netz
PDF
Blockchains - Mehr als nur digitale Währungen
PDF
Spaß an der Nebenläufigkeit
PDF
Go - Googles Sprache für skalierbare Systeme
PDF
Cloud Provisioning mit Juju
PDF
Juju - Scalable Software with Google Go
PDF
RESTful Web Applications with Google Go
PDF
Clouds, leicht beherrschbar
PDF
Skalierbare Anwendungen mit Google Go
PDF
WTC 2013 - Juju - Mit etwas Magie zur perfekten Cloud
PDF
Juju - Google Go in a scalable Environment
IT-Tage 2024: Philosophie in der Software-Architektur
JAX 2023 - Cloud Provider APIs
JAX 2023 - Generics in Go
Let The Computer Do It
Concurrency with Go
2021 OOP - Kubernetes Operatoren
DevOpsCon - Verteilte Entwicklung in Go
Devs@Home - Einführung in Go
Fun with functions
Ein Gopher im Netz
Blockchains - Mehr als nur digitale Währungen
Spaß an der Nebenläufigkeit
Go - Googles Sprache für skalierbare Systeme
Cloud Provisioning mit Juju
Juju - Scalable Software with Google Go
RESTful Web Applications with Google Go
Clouds, leicht beherrschbar
Skalierbare Anwendungen mit Google Go
WTC 2013 - Juju - Mit etwas Magie zur perfekten Cloud
Juju - Google Go in a scalable Environment

Recently uploaded (20)

PDF
project resource management chapter-09.pdf
PDF
Hindi spoken digit analysis for native and non-native speakers
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Mushroom cultivation and it's methods.pdf
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
Enhancing emotion recognition model for a student engagement use case through...
PDF
A novel scalable deep ensemble learning framework for big data classification...
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PPTX
TLE Review Electricity (Electricity).pptx
PDF
Approach and Philosophy of On baking technology
PDF
Web App vs Mobile App What Should You Build First.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
Encapsulation theory and applications.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
project resource management chapter-09.pdf
Hindi spoken digit analysis for native and non-native speakers
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
A comparative analysis of optical character recognition models for extracting...
Mushroom cultivation and it's methods.pdf
Zenith AI: Advanced Artificial Intelligence
Enhancing emotion recognition model for a student engagement use case through...
A novel scalable deep ensemble learning framework for big data classification...
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
gpt5_lecture_notes_comprehensive_20250812015547.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Heart disease approach using modified random forest and particle swarm optimi...
TLE Review Electricity (Electricity).pptx
Approach and Philosophy of On baking technology
Web App vs Mobile App What Should You Build First.pdf
Assigned Numbers - 2025 - Bluetooth® Document
cloud_computing_Infrastucture_as_cloud_p
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
Encapsulation theory and applications.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx

Beauty and Power of Go

  • 1. Beauty and Power of Go Frank Müller / Oldenburg / Germany
  • 2. Frank Müller Born 1965 in Oldenburg Software Development since 1984 Author since 1999 Book about Go in 2011 Introduction
  • 4. • End of 2007 start by Rob Pike, Ken Thompson and Robert Griesemer • Implementation started after design in mid 2008 • Going public in November 2009 • Release of Go 1 in March 2012 History
  • 5. • Rob Pike • Unix, Plan 9, Inferno, acme, Limbo, UTF-8 • Ken Thompson • Multics, Unix, B, Plan 9, ed, UTF-8 • Turing Award Famous parents
  • 6. “ Go aims to combine the safety and performance of a statically typed compiled language with the expressiveness and convenience of a dynamically typed interpreted language. It also aims to be suitable for modern systems - large scale - programming. – Rob Pike
  • 7. // Organized in packages. package main // Packages can be imported. import “fmt“ // Simple function declaration. func sayHello(to string) { // Package usage by prefix. fmt.Printf(“Hello, %s!n“, to) } // Main program is function main() in package main. func main() { sayHello(“World“) } Hello, World!
  • 8. • Roots in C and Plan 9 • Native binaries with garbage collection • Static typing • Concurreny • Interfaces • Powerful tools • No fantastic new pur language Some quick facts
  • 9. “ It is better to have a permanent income than to be fascinating. – Oscar Wilde
  • 10. bool rune string uintptr byte uint / uint8 - uint64 int / int8 - int64 float32 / float64 complex64 / complex128 struct array slice map pointer interface function channel Large set of standard types
  • 11. // Declaration can be done this way: var i int i = myIntReturningFunction() // More usual is: i := myIntReturningFunction() // Even for complex types: s := []string{“Hello“, “World“} m := map[string]string{ “foo“: “bar“, “baz“, “yadda“, } // Or multiple values: v, err := myFunctionWithPossibleError() Implicit declaration
  • 12. // Alias for a simple type. type Weight float64 func (w *Weight) String() string { return fmt.Sprintf(“%.3f kg“, w) } // Struct with hidden fields. type Name struct { first string last string } func (n *Name) String() string { return n.first + “ “ + n.last } func (n *Name) SetLast(l string) { n.last = l } Own types can have methods
  • 13. // Two-dimensional object. type BasePlate struct { width float64 depth float64 } func (bp *BasePlate) Area() float64 { return bp.width * bp.depth } // Use the base plate. type Box struct { BasePlate height float64 } func (b *Box) Volume() float64 { return b.Area() * b.height } No inheritence, but embedding
  • 14. // Create a base plate. func NewBasePlate(w, d float64) *BasePlate { return &BasePlate{w, d} } // Create a box. func NewBox(w, d, h float64) *Box { return &Box{BasePlate{w, d}, h} } // Create an illuminated box. func NewIlluminatedBox() *IlluminatedBox { ib := &IlluminatedBox{ box: NewBox(1.0, 1.0, 1.0), lightOnHour: 22, lightDuration: 8 * time.Hour, } go ib.backend() return ib } No contructors, simple functions
  • 16. • Important abstraction • Duck typing à la Go • Set of method signatures • No explicit implementation Interfaces
  • 17. // Interface declaration. type Duck interface { Quack() string } // First duck implementation. type SimpleDuck Name func (sd *SimpleDuck) Quack() string { return fmt.Sprintf(“Quack, my name is %s“, sd) } // Second duck implementation. type ComplexDuck struct { name Name box *IlluminatedBox } func (cd *ComplexDuck) Quack() string { return fmt.Sprintf(“Quack, my name is %s “ + “and my box has %.3f m3.“, cd.name, cd.box.Volume()) } Declare and implement interfaces
  • 18. // A pond with ducks. type Pond struct { ducks []Duck } // Add ducks to the pond. func (p *Pond) Add(ds ...Duck) { p.ducks = append(p.ducks, ds...) } // Add ducks to the pond. func main() { p := &Pond{} huey := NewSimpleDuck(...) dewey := NewComplexDuck(...) louie := NewAnyDuck(...) p.Add(huey, dewey, louie) for _, duck := range p.ducks { println(duck.Quack()) } } Use interfaces
  • 19. // An empty interface has no methods. type Anything interface{} // Type switch helps to get the real type. func foo(any interface{}) error { switch v := any.(type) { case bool: fmt.Printf(“I‘m a bool: %v“, v) case ComplexType: fmt.Printf(“I‘m a complex type: %v“, v) default: return errors.New(“Oh, type is unexpected!“) } return nil } // Or even shorter with type assertion. v, ok := any.(WantedType) if !ok { return errors.New(“Expected a ‘WantedType‘!“) } Empty interface
  • 21. • First-class functions • Higher-order functions • User-defined function types • Function literals • Closures • Multiple return values Flexible world of functions
  • 22. // Function type to do something with ducks. type DuckAction func(d Duck) error // Pond method to let the ducks do something. func (p *Pond) LetDucksDo(a DuckAction) error { for _, d := range p.ducks { if err := a(d); err != nil { return err } } } // Use a duck action. func CollectQuacks(p *Pond) ([]string, error) { quacks := []string{} if err := p.LetDucksDo(func(d Duck) error { quacks = append(quacks, d.Quack) return nil }); err != nil { return nil, err } return quacks, nil } Full bandwidth of function usage
  • 24. Lightweight goroutines running in a thread pool • Up to thousands of goroutines • Synchronization and communication via channels • Multi-way concurrent control via select statement Concurrency
  • 25. • Concurrency is the composition of independently executing processes • Parallelism is the simultaneous execution of computations in a context Concurrency is not parallelism
  • 26. • Modern applications deal with lots of things at once • Concurrency allows to structure software for these kinds of applications Structuring applications with concurrency
  • 27. // A goroutine is just a function. func fileWrite(filename, text string) { f, err := file.Open(filename) if err != nil { log.Printf(“fileWrite error: %v“, err) } defer f.Close() f.WriteString(text) } // It is started with the keyword ‘go‘. func main() { f := “...“ t := “...“ // Write file in the background. go fileWrite(f, t) // Meanwhile do something else. ... } Simple goroutine
  • 28. // Read strings and convert them to upper case. func pipe(in <-chan string, out chan<- string) { for s := range in { out <- strings.ToUpper(s) } } // Use buffered channels for async. communication. func main() { in := make(chan string, 50) out := make(chan string, 50) go pipe(in, out) in <- "lower-case string" s := <-out // Prints “LOWER-CASE STRING“. fmt.Println(s) } Pipeline goroutine using range statement
  • 29. // Job with four parts. func job(s *Source, t *Target) { a := partA(s) b := partB(a) c := partC(b) d := partD(c) t.Set(d) } // Start job multiple times. func parallel(s *Source, t *Target) { for i := 0; i < 4; i++ { go job(s, t) } } Naive approach leads to syncing problems
  • 30. // Start a pipeline of parts. func concurrent(s *Source, t *Target) { abChan := make(chan *AResult, 5) bcChan := make(chan *BResult, 5) cdChan := make(chan *CResult, 5) waitChan := make(chan bool) go partA(s, abChan) go partB(abChan, bcChan) go partC(bcChan, cdChan) go partD(cdChan, t, waitChan) <-waitChan } Pipelined approach
  • 31. // Type X managing some data. type X struct { ... } // Backend of type X. func (x *X) backend() { for { select { case r := <-w.requestChan: // One request after another. w.handleRequest(r) case c := <-w.configChan: // A configuration change. w.handleConfiguration(c) case <-w.stopChan: // X instance shall stop working. return case <-time.After(5 * time.Second): // No request since 5 seconds. w.handleTimeout() } } } Complex scenarios using select statement
  • 32. // A possible request. type Request struct { f func() *Response responseChan chan string } // Public method to add an exclamation mark. func (x *X) Exclamation(in string) string { // Also x could be modified here. f := func() { return in + “!“ } req := &Request{f, make(chan string)} x.requestChan <- req return <-req.responseChan } // Using the requests closure in the backend. func (x *X) handleRequest(req *Request) { res := req.f() x.responses = append(x.responses, res) req.responseChan <- res } Request and response handling
  • 33. // Start 10 pipes to do the work. func balancedWorker(ins []string) []string { in, out := make(chan string), make(chan string) outs := []string{} // Start worker goroutines. for i := 0; i < 10; i++ { go worker(in, out) } // Send input strings in background. go func() { for _, is := range ins { in <- is } close(in) }() // Collect output. for os := range out { outs = append(outs, os) if len(outs) == len(ins) { break } } return outs } Simple load balancing
  • 34. // Receiving data. select { case data, ok := <-dataChan: if ok { // Do something with data. } else { // Channel is closed. } case <-time.After(5 * time.Seconds): // A timeout happened. } // Sending data. select { case dataChan <- data: // Everything fine. case <-time.After(5 * time.Seconds): // A timeout happened. } Adding some safety
  • 36. • error is just an interface • Errors are return values • defer helps to ensure cleanup tasks • panic should be used with care • recover can handle panics • Leads to clear and immediate error handling Error, panic and recover
  • 37. // Error for non-feedable ducks. type NonFeedableDuckError struct { Duck Duck Food *Food } // Fullfil the error interface. func (e *NonFeedableError) Error() string { return fmt.Sprintf(“It seems %v dislikes %v, “, “maybe a rubber duck?“, e.Duck, e.Food) } // Error as only return value. func FeedDucks(p *Pond, f *Food) error { return p.LetDucksDo(func(d Duck) error { // Type assert for ‘FeedableDuck‘ interface. if fd, ok := d.(FeedableDuck); ok { return fd.Feed(f) } return &NonFeedableDuckError{d, f} }) } Definition and usage of own error type
  • 38. // Retrieve data from a server. func Retrieve(addr, name string) (*Data, error) { conn, err := net.Dial(“tcp“, addr) if err != nil { return nil, err } defer conn.Close() // Do the retrieving. ... return data, nil } // Somewhere else. data, err := Retrieve(“localhost:80“, “/foo/bar“) if err != nil { // Handle the error. ... } Error in multi-value return, cleanup with defer
  • 39. // Save execution of function ‘f‘. func safeExec(f func()) (err error) { defer func() { if r := recover(); r != nil { err = fmt.Sprintf(“Ouch: %v!“, r) } } f() return nil } // Somewhere else. var result float64 err := safeExec(func() { // Divide by zero. result = 1.0 / (1.0 - 1.0) }) if err != nil { // Do the error management. ... } panic and recover
  • 41. • Network / HTTP / Template • Encodings (JSON, XML, ...) • I/O, Formatting • Crypto • Images • Time • Reflection Large set of useful packages
  • 42. • Import of external packages • Namespace based on SCM • BitBucket, GitHub • Google Code, Launchpad • Example: • “code.google.com/p/tcgl/redis“ External packages
  • 44. • build - build the software • fmt - format the sources • test - run unit tests • install - install the package • get - retrieve and install an external package • doc - source documentation Most important go subcommands
  • 45. • http://golang.org/ • http://tour.golang.org/ • http://blog.golang.org/ • http://godashboard.appspot.com/ • http://go-lang.cat-v.org/ • http://www.reddit.com/r/golang/ • #go-nuts on irc.freenode.net • Google Group golang-nuts Some links
  • 46. Thank you for listening. Questions? And now some practice ...

Editor's Notes

  • #2: \n
  • #3: Tideland is the name of the ecosystem of the German north-sea cost near to Oldenburg\n\nWay to Google Go mostly influenced by Pascal, C, ReXX, Java, Python, Smalltalk and Erlang\n\nArticles and reviews about programming languages and development processes for the German iX magazine\n
  • #4: Time is too short for a full tutorial\n\nConcentration on most important features\n\nExample code for some practice later\n
  • #5: Design decisions had to be made unanimous\n\nSometimes this forced longer discussions\n\nAs a result the core language has been quite stable when going public\n
  • #6: Multics, Unix, Plan 9, Inferno: operating systems\n\nB, Limbo: programming languages\n\nacme, ed: editors\n
  • #7: \n
  • #8: Still too complex example of a main program (smile)\n\nmain is the name of the program package, main() is the main function\n\nArguments and return values are handled via packages\n\nDirectly using println(&amp;#x201C;Hello, World!&amp;#x201C;) in main is the shortest way\n\n\n
  • #9: Very fast compilation has been a design goal\n\nStatic typing is strict\n\nConcurreny originates in Tony Hoare&apos;s Communicating Sequential Processes (CSP)\n\nShare by communication\n
  • #10: \n
  • #11: Upper half: predeclared types\n\nLower half: composite types\n
  • #12: Explicit declaration of variable and type with var is allowed\n\nMostly the short declaration and initializing with := is used\n
  • #13: String() is the only method of the Stringer interface allowing types to return a natural representation as a string\n\nImplicit export: lower-case is package-private, upper-case is exported\n\nWhen setter and getter are needed, the convention is SetFoo() and Foo()\n\nAsterisk in front of type passes variable as reference instead of a copy (important for settings)\n
  • #14: Embedding may cause naming troubles with fields or methods, e.g. Box defines Area() itself\n\nHere the embedded field or method has to be addressed full qualified: b.BasePlate.Area()\n\nWhen the embedded type is from external package no access to private fields or methods is possible\n\nUsing of embedding type by embedded type is possible by interface implementation and passing to embedded type (tricky)\n
  • #15: In simple cases often directly creations like &amp;BasePlate{} or &amp;Box{} are used\n\nWhen using the short initialization of a struct all fields in order or some fields with name has to be passed\n\n
  • #16: \n
  • #17: Duck test: If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck\n\nNo &quot;type foo struct implements bar, baz { ... }&quot;\n\nJust provide the methods of the interface\n
  • #18: Interface Duck only defines one the method Quack()\n\nVery often interfaces only contain few methods describing what the implementor is able to do\n
  • #19: Focus on what, not how\n\nPond.Add() shows usage of variadic final parameter, which internally is a slice of the given type\n\nThe build-in functions append() and copy() support the work with slices\n
  • #20: The empty interface may look strange but shows exactly what it is: an interface without methods\n\nEmpty interface, type switch and type assertion allow a transparent control flow for generic data\n
  • #21: \n
  • #22: \n
  • #23: LetDucksDo() uses the user-defined function type DuckAction as higher-order function\n\nFirst-class function CollectQuacks() passes a function literal to LetDucksDo()\n\nAs a closure it has access to the variable quacks of the surrounding function\n
  • #24: \n
  • #25: Memory overhead and context switching very small compared to threads (like Erlang)\n\nIndependent of the number of cores (reason will get more clear later)\n\nSessions with more than a million goroutines have been tested\n\nGoroutines may share multiple channels, not just one mailbox per goroutine (unlike Erlang)\n
  • #26: Concurreny is the base for parallelism\n
  • #27: Multicore computers\n\nCluster and clouds of CPUs\n\nNetworks\n\nUser sessions\n\n\n
  • #28: The keyword go starts a function in the background\n\nNo id or handle returned\n\nfileWrite() introduces defer which executes functions when the surrounding function will be left\n\nHere when file opening shows no error the closing gets automated\n
  • #29: Channels have to be created with make()\n\nBy default unbuffered, but can have a buffer\n
  • #30: Reading of data from source has to be managed\n\nWriting into target has to be managed\n\nOrder has to be kept\n\nparallel() returns after start of goroutines, but jobs aren&amp;#x2018;t yet done\n
  • #31: More like a production line\n\nNo syncing in source and target needed\n\nEach part signals when everything is done\n\nLast part signals via waitChan that work has been finished\n\n\n
  • #32: Many different channels are possible (check exactly if needed due to complexity)\n\nTimeout not needed, but may be used for safety aspects\n\nselect also knows default branch, taken if no other data received\n
  • #33: Function passed in a request allows to serialize access to state (see x.responses)\n
  • #34: Only two channels for input and output\n\nMultiple worker process the data independently\n\nInput is written in the input channel in background too\n\nReceived results may have a different order, depending on individual processing duration\n
  • #35: While receiving channel could be closed or it may last too long\n\nWhile sending the receiver(s) may have a deadlock so data can&amp;#x2018;t be sent and the program hangs\n\nTimeouts may allow graceful termination\n
  • #36: \n
  • #37: error only contains the method Error() string\n\nPackages errors allows to create a standard error with New()\n\nPackage fmt has Errorf() to return errors containing variable text information\n\npanic() not for control flow but for real logical failures\n
  • #38: If Duck and Food not needed the generic fmt.Errorf() could be used\n\nOwn types and fields allow the error handling to use type switching and get more informations about the error reason\n
  • #39: Linear control flow with error checks is typical\n\nSymetric operations like Open()/Close() or Dial()/Close() often use defer (fire and forget)\n\ndefer statements are function or method calls executed in LIFO order\n\nThis way work is done step by step, no need for cleanup later\n
  • #40: Unhandled panics lead to a program abort printing a stack trace (typically readable enough to get a good hint about the error)\n\nDirect divide by zero isn&amp;#x2018;t possible, compiler detects it (smile)\n
  • #41: \n
  • #42: Packages are, like the language specification, very good documented at http://golang.org\n
  • #43: Retrieving of external packages uses labels/tags matching the local Go version\n
  • #44: \n
  • #45: \n
  • #46: \n
  • #47: \n