SlideShare une entreprise Scribd logo
1  sur  46
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 ...

Contenu connexe

Tendances

Hacking Go Compiler Internals / GoCon 2014 Autumn
Hacking Go Compiler Internals / GoCon 2014 AutumnHacking Go Compiler Internals / GoCon 2014 Autumn
Hacking Go Compiler Internals / GoCon 2014 AutumnMoriyoshi Koizumi
 
Using Flow-based programming to write tools and workflows for Scientific Comp...
Using Flow-based programming to write tools and workflows for Scientific Comp...Using Flow-based programming to write tools and workflows for Scientific Comp...
Using Flow-based programming to write tools and workflows for Scientific Comp...Samuel Lampa
 
7 Common Mistakes in Go (2015)
7 Common Mistakes in Go (2015)7 Common Mistakes in Go (2015)
7 Common Mistakes in Go (2015)Steven Francia
 
Demystifying the Go Scheduler
Demystifying the Go SchedulerDemystifying the Go Scheduler
Demystifying the Go Schedulermatthewrdale
 
Introduction to Go programming language
Introduction to Go programming languageIntroduction to Go programming language
Introduction to Go programming languageSlawomir Dorzak
 
Introduction to kotlin coroutines
Introduction to kotlin coroutinesIntroduction to kotlin coroutines
Introduction to kotlin coroutinesNAVER Engineering
 
O caml2014 leroy-slides
O caml2014 leroy-slidesO caml2014 leroy-slides
O caml2014 leroy-slidesOCaml
 
05 pig user defined functions (udfs)
05 pig user defined functions (udfs)05 pig user defined functions (udfs)
05 pig user defined functions (udfs)Subhas Kumar Ghosh
 
7 Common mistakes in Go and when to avoid them
7 Common mistakes in Go and when to avoid them7 Common mistakes in Go and when to avoid them
7 Common mistakes in Go and when to avoid themSteven Francia
 
Golang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / OverviewGolang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / OverviewMarkus Schneider
 
4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладка4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладкаDEVTYPE
 
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...Muhammad Ulhaque
 
Transmogrifier: Migrating to Plone with less pain
Transmogrifier: Migrating to Plone with less painTransmogrifier: Migrating to Plone with less pain
Transmogrifier: Migrating to Plone with less painLennart Regebro
 
Go Programming Language (Golang)
Go Programming Language (Golang)Go Programming Language (Golang)
Go Programming Language (Golang)Ishin Vin
 

Tendances (20)

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

Similaire à Beauty and Power of Go

golang_getting_started.pptx
golang_getting_started.pptxgolang_getting_started.pptx
golang_getting_started.pptxGuy Komari
 
Introduction to go
Introduction to goIntroduction to go
Introduction to goJaehue Jang
 
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...Francesco Casalegno
 
golang_refcard.pdf
golang_refcard.pdfgolang_refcard.pdf
golang_refcard.pdfSpam92
 
Go ahead, make my day
Go ahead, make my dayGo ahead, make my day
Go ahead, make my dayTor Ivry
 
Next Generation Language Go
Next Generation Language GoNext Generation Language Go
Next Generation Language GoYoichiro Shimizu
 
Go serving: Building server app with go
Go serving: Building server app with goGo serving: Building server app with go
Go serving: Building server app with goHean Hong Leong
 
Geeks Anonymes - Le langage Go
Geeks Anonymes - Le langage GoGeeks Anonymes - Le langage Go
Geeks Anonymes - Le langage GoGeeks Anonymes
 
От Java Threads к лямбдам, Андрей Родионов
От Java Threads к лямбдам, Андрей РодионовОт Java Threads к лямбдам, Андрей Родионов
От Java Threads к лямбдам, Андрей РодионовYandex
 
The GO Language : From Beginners to Gophers
The GO Language : From Beginners to GophersThe GO Language : From Beginners to Gophers
The GO Language : From Beginners to GophersAlessandro Sanino
 
Things about Functional JavaScript
Things about Functional JavaScriptThings about Functional JavaScript
Things about Functional JavaScriptChengHui Weng
 
Functions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupFunctions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupSyedHaroonShah4
 
От Java Threads к лямбдам, Андрей Родионов
От Java Threads к лямбдам, Андрей РодионовОт Java Threads к лямбдам, Андрей Родионов
От Java Threads к лямбдам, Андрей РодионовYandex
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)Pavlo Baron
 

Similaire à Beauty and Power of Go (20)

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

Plus de Frank Müller

JAX 2023 - Cloud Provider APIs
JAX 2023 - Cloud Provider APIsJAX 2023 - Cloud Provider APIs
JAX 2023 - Cloud Provider APIsFrank Müller
 
JAX 2023 - Generics in Go
JAX 2023 - Generics in GoJAX 2023 - Generics in Go
JAX 2023 - Generics in GoFrank Müller
 
Let The Computer Do It
Let The Computer Do ItLet The Computer Do It
Let The Computer Do ItFrank Müller
 
2021 OOP - Kubernetes Operatoren
2021   OOP - Kubernetes Operatoren2021   OOP - Kubernetes Operatoren
2021 OOP - Kubernetes OperatorenFrank Müller
 
DevOpsCon - Verteilte Entwicklung in Go
DevOpsCon - Verteilte Entwicklung in GoDevOpsCon - Verteilte Entwicklung in Go
DevOpsCon - Verteilte Entwicklung in GoFrank Müller
 
Devs@Home - Einführung in Go
Devs@Home - Einführung in GoDevs@Home - Einführung in Go
Devs@Home - Einführung in GoFrank Müller
 
Blockchains - Mehr als nur digitale Währungen
Blockchains - Mehr als nur digitale WährungenBlockchains - Mehr als nur digitale Währungen
Blockchains - Mehr als nur digitale WährungenFrank Müller
 
Spaß an der Nebenläufigkeit
Spaß an der NebenläufigkeitSpaß an der Nebenläufigkeit
Spaß an der NebenläufigkeitFrank Müller
 
Go - Googles Sprache für skalierbare Systeme
Go - Googles Sprache für skalierbare SystemeGo - Googles Sprache für skalierbare Systeme
Go - Googles Sprache für skalierbare SystemeFrank Müller
 
Cloud Provisioning mit Juju
Cloud Provisioning mit JujuCloud Provisioning mit Juju
Cloud Provisioning mit JujuFrank Müller
 
Juju - Scalable Software with Google Go
Juju - Scalable Software with Google GoJuju - Scalable Software with Google Go
Juju - Scalable Software with Google GoFrank Müller
 
RESTful Web Applications with Google Go
RESTful Web Applications with Google GoRESTful Web Applications with Google Go
RESTful Web Applications with Google GoFrank Müller
 
Clouds, leicht beherrschbar
Clouds, leicht beherrschbarClouds, leicht beherrschbar
Clouds, leicht beherrschbarFrank Müller
 
Skalierbare Anwendungen mit Google Go
Skalierbare Anwendungen mit Google GoSkalierbare Anwendungen mit Google Go
Skalierbare Anwendungen mit Google GoFrank Müller
 
WTC 2013 - Juju - Mit etwas Magie zur perfekten Cloud
WTC 2013 - Juju - Mit etwas Magie zur perfekten CloudWTC 2013 - Juju - Mit etwas Magie zur perfekten Cloud
WTC 2013 - Juju - Mit etwas Magie zur perfekten CloudFrank Müller
 
Juju - Google Go in a scalable Environment
Juju - Google Go in a scalable EnvironmentJuju - Google Go in a scalable Environment
Juju - Google Go in a scalable EnvironmentFrank Müller
 
OOP 2013 - Weltweite Entwicklung von Open Source Software
OOP 2013 - Weltweite Entwicklung von Open Source SoftwareOOP 2013 - Weltweite Entwicklung von Open Source Software
OOP 2013 - Weltweite Entwicklung von Open Source SoftwareFrank Müller
 

Plus de Frank Müller (20)

JAX 2023 - Cloud Provider APIs
JAX 2023 - Cloud Provider APIsJAX 2023 - Cloud Provider APIs
JAX 2023 - Cloud Provider APIs
 
JAX 2023 - Generics in Go
JAX 2023 - Generics in GoJAX 2023 - Generics in Go
JAX 2023 - Generics in Go
 
Let The Computer Do It
Let The Computer Do ItLet The Computer Do It
Let The Computer Do It
 
Concurrency with Go
Concurrency with GoConcurrency with Go
Concurrency with Go
 
2021 OOP - Kubernetes Operatoren
2021   OOP - Kubernetes Operatoren2021   OOP - Kubernetes Operatoren
2021 OOP - Kubernetes Operatoren
 
DevOpsCon - Verteilte Entwicklung in Go
DevOpsCon - Verteilte Entwicklung in GoDevOpsCon - Verteilte Entwicklung in Go
DevOpsCon - Verteilte Entwicklung in Go
 
Devs@Home - Einführung in Go
Devs@Home - Einführung in GoDevs@Home - Einführung in Go
Devs@Home - Einführung in Go
 
Fun with functions
Fun with functionsFun with functions
Fun with functions
 
Ein Gopher im Netz
Ein Gopher im NetzEin Gopher im Netz
Ein Gopher im Netz
 
Blockchains - Mehr als nur digitale Währungen
Blockchains - Mehr als nur digitale WährungenBlockchains - Mehr als nur digitale Währungen
Blockchains - Mehr als nur digitale Währungen
 
Spaß an der Nebenläufigkeit
Spaß an der NebenläufigkeitSpaß an der Nebenläufigkeit
Spaß an der Nebenläufigkeit
 
Go - Googles Sprache für skalierbare Systeme
Go - Googles Sprache für skalierbare SystemeGo - Googles Sprache für skalierbare Systeme
Go - Googles Sprache für skalierbare Systeme
 
Cloud Provisioning mit Juju
Cloud Provisioning mit JujuCloud Provisioning mit Juju
Cloud Provisioning mit Juju
 
Juju - Scalable Software with Google Go
Juju - Scalable Software with Google GoJuju - Scalable Software with Google Go
Juju - Scalable Software with Google Go
 
RESTful Web Applications with Google Go
RESTful Web Applications with Google GoRESTful Web Applications with Google Go
RESTful Web Applications with Google Go
 
Clouds, leicht beherrschbar
Clouds, leicht beherrschbarClouds, leicht beherrschbar
Clouds, leicht beherrschbar
 
Skalierbare Anwendungen mit Google Go
Skalierbare Anwendungen mit Google GoSkalierbare Anwendungen mit Google Go
Skalierbare Anwendungen mit Google Go
 
WTC 2013 - Juju - Mit etwas Magie zur perfekten Cloud
WTC 2013 - Juju - Mit etwas Magie zur perfekten CloudWTC 2013 - Juju - Mit etwas Magie zur perfekten Cloud
WTC 2013 - Juju - Mit etwas Magie zur perfekten Cloud
 
Juju - Google Go in a scalable Environment
Juju - Google Go in a scalable EnvironmentJuju - Google Go in a scalable Environment
Juju - Google Go in a scalable Environment
 
OOP 2013 - Weltweite Entwicklung von Open Source Software
OOP 2013 - Weltweite Entwicklung von Open Source SoftwareOOP 2013 - Weltweite Entwicklung von Open Source Software
OOP 2013 - Weltweite Entwicklung von Open Source Software
 

Dernier

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024The Digital Insurer
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 

Dernier (20)

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 

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 ...

Notes de l'éditeur

  1. \n
  2. 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
  3. Time is too short for a full tutorial\n\nConcentration on most important features\n\nExample code for some practice later\n
  4. 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
  5. Multics, Unix, Plan 9, Inferno: operating systems\n\nB, Limbo: programming languages\n\nacme, ed: editors\n
  6. \n
  7. 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
  8. 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
  9. \n
  10. Upper half: predeclared types\n\nLower half: composite types\n
  11. Explicit declaration of variable and type with var is allowed\n\nMostly the short declaration and initializing with := is used\n
  12. 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
  13. 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
  14. 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
  15. \n
  16. 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
  17. 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
  18. 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
  19. 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
  20. \n
  21. \n
  22. 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
  23. \n
  24. 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
  25. Concurreny is the base for parallelism\n
  26. Multicore computers\n\nCluster and clouds of CPUs\n\nNetworks\n\nUser sessions\n\n\n
  27. 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
  28. Channels have to be created with make()\n\nBy default unbuffered, but can have a buffer\n
  29. 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
  30. 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
  31. 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
  32. Function passed in a request allows to serialize access to state (see x.responses)\n
  33. 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
  34. 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
  35. \n
  36. 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
  37. 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
  38. 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
  39. 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
  40. \n
  41. Packages are, like the language specification, very good documented at http://golang.org\n
  42. Retrieving of external packages uses labels/tags matching the local Go version\n
  43. \n
  44. \n
  45. \n
  46. \n