SlideShare une entreprise Scribd logo
1  sur  47
Télécharger pour lire hors ligne
The
                                Go
                        Programming Language

                                Part 3

                                Rob Pike
                              r@google.com
                           (updated June 2011)


Friday, June 10, 2011                            1
Today’s Outline

                        Exercise
                         any questions?

                        Concurrency and communication
                         goroutines
                         channels
                         concurrency issues




Friday, June 10, 2011                                   2
Exercise

       Any questions?

       For a trivial HTTP server with various generators, see

            http://golang.org/src/pkg/http/triv.go




Friday, June 10, 2011                                           3
Concurrency and communication:
                          Goroutines




Friday, June 10, 2011                            4
Goroutines

            Terminology:

            There are many terms for "things that run
            concurrently" - process, thread, coroutine, POSIX
            thread, NPTL thread, lightweight process, ..., but
            these all mean slightly different things. None
            means exactly how Go does concurrency.

            So we introduce a new term: goroutine.




Friday, June 10, 2011                                            5
Definition
          A goroutine is a Go function or method executing
          concurrently in the same address space as other
          goroutines. A running program consists of one or
          more goroutines.

          It's not the same as a thread, coroutine, process,
          etc. It's a goroutine.

          Note: Concurrency and parallelism are different
          concepts. Look them up if you don't understand
          the difference.

          There are many concurrency questions. They will
          be addressed later; for now just assume it all
          works as advertised.
Friday, June 10, 2011                                          6
Starting a goroutine
         Invoke a function or method and say go:

              func IsReady(what string, minutes int64) {
                  time.Sleep(minutes * 60*1e9) // Unit is nanosecs.
                  fmt.Println(what, "is ready")
              }

              go IsReady("tea", 6)
              go IsReady("coffee", 2)
              fmt.Println("I'm waiting...")

         Prints:
              I'm waiting...    (right away)
              coffee is ready   (2 minutes later)
              tea is ready      (6 minutes later)

Friday, June 10, 2011                                                 7
Some simple facts

               Goroutines are cheap.

               Goroutines exit by returning from their top-level
               function, or just falling off the end.

               Goroutines can run concurrently on different
               processors, sharing memory.

               You don't have to worry about stack size.




Friday, June 10, 2011                                              8
Stacks

       In gccgo, at least for now, goroutines are pthreads. In
       6g they're multiplexed onto threads, so they are much
       cheaper.

       In both worlds, stacks are small (a few kB) and grow
       as needed. Thus goroutines use little memory, you
       can have lots of them, and they can dynamically have
       huge stacks.

       The programmer shouldn't have to think about the
       stack size issue, and in Go, it doesn't even come up.



Friday, June 10, 2011                                            9
Scheduling
                Goroutines are multiplexed as needed onto
                system threads. When a goroutine executes a
                blocking system call, no other goroutine is
                blocked.
                Plan to do the same for CPU-bound goroutines
                at some point, but for now, if you want user-
                level parallelism in 6g* you must set shell var.
                GOMAXPROCS or call runtime.GOMAXPROCS(n).
                GOMAXPROCS tells the runtime scheduler how
                many user-space-executing goroutines to run
                at once, ideally on different CPU cores.
                *
                    gccgo always uses one thread per goroutine.


Friday, June 10, 2011                                              10
Concurrency and communication:
                            Channels




Friday, June 10, 2011                             11
Channels in Go
          Unless two goroutines can communicate, they
          can't coordinate.

          Go has a type called a channel that provides
          communication and synchronization capabilities.

          It also has special control structures that build on
          channels to make concurrent programming easy.

          Note: I gave a talk in 2006 about predecessor
          stuff in an older language.
               http://www.youtube.com/watch?v=HmxnCEa8Ctw




Friday, June 10, 2011                                            12
The channel type
                  In its simplest form the type looks like this:

                        chan elementType


                  With a value of this type, you can send and
                  receive items of elementType.

                  Channels are a reference type, which means if
                  you assign one chan variable to another, both
                  variables access the same channel. It also
                  means you use make to allocate one:

                        var c = make(chan int)



Friday, June 10, 2011                                              13
The communication operator: <-
       The arrow points in the direction of data flow.

       As a binary operator, <- sends the value on the right
       to the channel on the left:
            c := make(chan int)
            c <- 1   // send 1 on c (flowing into c)

       As a prefix unary operator, <- receives from a
       channel:
            v = <-c // receive value from c, assign to v
            <-c      // receive value, throw it away
            i := <-c // receive value, initialize i




Friday, June 10, 2011                                          14
Semantics

      By default, communication is synchronous. (We'll
      talk about asynchronous communication later.)
      This means:

           1) A send operation on a channel blocks until a
           receiver is available for the same channel.
           2) A receive operation for a channel blocks until a
           sender is available for the same channel.

      Communication is therefore a form of
      synchronization: two goroutines exchanging data
      through a channel synchronize at the moment of
      communication.

Friday, June 10, 2011                                            15
Let's pump some data
              func pump(ch chan int) {
                  for i := 0; ; i++ { ch <- i }
              }

              ch1 := make(chan int)
              go pump(ch1)        // pump hangs; we run
              fmt.Println(<-ch1) // prints 0

         Now we start a looping receiver.
              func suck(ch chan int) {
                  for { fmt.Println(<-ch) }
              }
              go suck(ch1) // tons of numbers appear

         You can still sneak in and grab a value:
              fmt.Println(<-ch1)   // Prints 314159

Friday, June 10, 2011                                     16
Functions returning channels
      In the previous example, pump was like a generator
      spewing out values. But there was a lot of fuss
      allocating channels etc. Let's package it up into a
      function returning the channel of values.
           func pump() chan int {
               ch := make(chan int)
               go func() {
                   for i := 0; ; i++ { ch <- i }
               }()
               return ch
           }

           stream := pump()
           fmt.Println(<-stream)   // prints 0

      "Function returning channel" is an important idiom.

Friday, June 10, 2011                                       17
Channel functions everywhere

       I am avoiding repeating famous examples you can
       find elsewhere. Here are a couple to look up:

       1) The prime sieve; in the language specification and
       also in the tutorial.

       2) Doug McIlroy's power series paper:
           http://plan9.bell-labs.com/who/rsc/thread/squint.pdf

       A Go version of this amazing program is in available
       in the test suite as:
           http://golang.org/test/chan/powser1.go



Friday, June 10, 2011                                             18
Range and channels

            The range clause on for loops accepts a channel
            as an operand, in which case the for loops over
            the values received from the channel. We
            rewrote pump; here's the rewrite for suck, making
            it launch the goroutine as well:

                 func suck(ch chan int) {
                     go func() {
                         for v := range ch { fmt.Println(v) }
                     }()
                 }

                 suck(pump())   // doesn't block now



Friday, June 10, 2011                                           19
Closing a channel
            How does range know when the channel is done
            delivering data? The sender calls the built-in
            function close:
                 close(ch)

            Receiver tests if the sender has closed the
            channel using "comma ok":
                 val, ok := <-ch

            Result is (value, true) while values are available;
            once channel is closed and drained, result is
            (zero, false).



Friday, June 10, 2011                                             20
Range on a channel, by hand
       A for range on a channel such as:
           for value := range <-ch {
               use(value)
           }

       is equivalent to:
           for {
               value, ok := <-ch
               if !ok {
                   break
               }
               use(value)
           }




Friday, June 10, 2011                                 21
Close

       Key points:
       Only the sender should call close.
       Only the receiver can ask if channel has been closed.
       Can only ask while getting a value (avoids races).

       Call close only when it's necessary to signal to the
       receiver that no more values will arrive.

       Most of the time, close isn't needed; it's not
       analogous to closing a file.

       Channels are garbage-collected regardless.

Friday, June 10, 2011                                          22
Channel directionality

    In its simplest form a channel variable is an
    unbuffered (synchronous) value that can be used to
    send and receive.

    A channel type may be annotated to specify that it
    may only send or only receive:

         var recvOnly <-chan int
         var sendOnly chan<- int




Friday, June 10, 2011                                    23
Channel directionality (II)
          All channels are created bidirectional, but we can
          assign them to directional channel variables.
          Useful for instance in functions, for (type) safety:

          func sink(ch <-chan int) {
              for { <-ch }
          }
          func source(ch chan<- int) {
              for { ch <- 1 }
          }

          c := make(chan int)    // bidirectional
          go source(c)
          go sink(c)



Friday, June 10, 2011                                            24
Synchronous channels
       Synchronous channels are unbuffered. Sends do not
       complete until a receiver has accepted the value.
            c := make(chan int)
            go func() {
                time.Sleep(60*1e9)
                x := <-c
                fmt.Println("received", x)
            }()

            fmt.Println("sending", 10)
            c <- 10
            fmt.Println("sent", 10)

       Output:
            sending 10 (happens immediately)
            sent 10     (60s later, these 2 lines appear)
            received 10
Friday, June 10, 2011                                       25
Asynchronous channels
       A buffered, asynchronous channel is created by
       telling make the number of elements in the buffer.
            c := make(chan int, 50)
            go func() {
                time.Sleep(60*1e9)
                x := <-c
                fmt.Println("received", x)
            }()

            fmt.Println("sending", 10)
            c <- 10
            fmt.Println("sent", 10)

       Output:
            sending 10 (happens immediately)
            sent 10     (now)
            received 10 (60s later)
Friday, June 10, 2011                                       26
Buffer is not part of the type



        Note that the buffer's size, or even its existence, is
        not part of the channel's type, only of the value.
        This code is therefore legal, although dangerous:
             buf         =   make(chan int, 1)
             unbuf       =   make(chan int)
             buf         =   unbuf
             unbuf       =   buf

        Buffering is a property of the value, not of the type.



Friday, June 10, 2011                                            27
Select
        Select is a control structure in Go analogous to a
        communications switch statement. Each case must
        be a communication, either send or receive.
             ci, cs := make(chan int), make(chan string)

             select {
             case v := <-ci:
                 fmt.Printf("received %d from cin", v)
             case v := <-cs:
                 fmt.Printf("received %s from csn", v)
             }


        Select executes one runnable case at random. If
        no case is runnable, it blocks until one is. A
        default clause is always runnable.
Friday, June 10, 2011                                        28
Select semantics
        Quick summary:

        - Every case must be a (possibly :=) communication
        - All channel expressions are evaluated
        - All expressions to be sent are evaluated
        - If any communication can proceed, it does; others
          are ignored
        - If multiple cases are ready, one is selected at
          random, fairly. Others do not execute.
        - Otherwise:
          - If there is a default clause, that runs
          - If there is no default, select statement blocks
            until one communication can proceed; there is
            no re-evaluation of channels or values
Friday, June 10, 2011                                         29
Random bit generator
        Silly but illustrative example.
             c := make(chan int)
             go func() {
                 for {
                     fmt.Println(<-c)
                 }
             }()

             for {
                 select {
                 case c <- 0:   // no statement, no fall through
                 case c <- 1:
                 }
             }


        Prints 0 1 1 0 0 1 1 1 0 1 ...
Friday, June 10, 2011                                              30
Testing for communicability
          Can a communication proceed without blocking?
          Select with a default clause can tell us:

               select {
               case v := <-ch:
                   fmt.Println("received", v)
               default:
                   fmt.Println("ch not ready for receive")
               }

          The default clause executes if no other case can
          proceed, so this is the idiom for non-blocking
          receive; non-blocking send is the obvious variant.



Friday, June 10, 2011                                          31
Timeout
          Can a communication succeed in a given interval?
          Package time contains the After function:
               func After(ns int64) <-chan int64

          It delivers a value (the current time) on the
          returned channel after the specified interval.

          Use it in select to implement timeout:
               select {
               case v := <-ch:
                   fmt.Println("received", v)
               case <-time.After(30*1e9):
                   fmt.Println("timed out after 30 seconds")
               }


Friday, June 10, 2011                                          32
Multiplexing
          Channels are first-class values, which means they
          can be sent over channels. This property makes it
          easy to write a service multiplexer since the client
          can supply, along with its request, the channel on
          which to reply.

               chanOfChans := make(chan chan int)

          Or more typically

               type Reply struct { ... }
               type Request struct {
                   arg1, arg2 someType
                   replyc      chan *Reply
               }


Friday, June 10, 2011                                            33
Multiplexing server
               type request struct {
                   a, b    int
                   replyc chan int
               }

               type binOp func(a, b int) int

               func run(op binOp, req *request) {
                   req.replyc <- op(req.a, req.b)
               }

               func server(op binOp, service <-chan *request) {
                   for {
                       req := <-service // requests arrive here
                       go run(op, req) // don't wait for op
                   }
               }
Friday, June 10, 2011                                             34
Starting the server
   Use the "function returning channel" idiom to create
   a channel to a new server:

        func startServer(op binOp) chan<- *request {
            service := make(chan *request)
            go server(op, req)
            return service
        }

   adderChan := startServer(
         func(a, b int) int { return a + b }
       )




Friday, June 10, 2011                                     35
The client
    A similar example is worked in more detail in the
    tutorial, but here's a variant:
         func (r *request) String() string {
             return fmt.Sprintf("%d+%d=%d",
                                r.a, r.b, <-r.replyc)
         }

         req1 := &request{7, 8, make(chan int)}
         req2 := &request{17, 18, make(chan int)}

    Requests are ready; send them:
         adderChan <- req1
         adderChan <- req2

    Can retrieve results in any order; r.replyc demuxes:
         fmt.Println(req2, req1)

Friday, June 10, 2011                                      36
Teardown
    In the multiplex example, the server runs forever.
    To tear it down cleanly, signal with a channel. This
    server has the same functionality but with a quit
    channel:
         func server(op binOp, service <-chan *request,
                               quit <-chan bool) {
             for {
                 select {
                 case req := <-service:
                     go run(op, req) // don't wait for it
                 case <-quit:
                     return
                 }
             }
         }


Friday, June 10, 2011                                       37
Starting the server
    The rest of the code is mostly the same, with an extra
    channel:

         func startServer(op binOp) (service chan<- *request,
                                     quit chan<- bool) {
             service = make(chan *request)
             quit = make(chan bool)
             go server(op, service, quit)
             return service, quit
         }

         adderChan, quitChan := startServer(
             func(a, b int) int { return a + b }
         )




Friday, June 10, 2011                                           38
Teardown: the client
   The client is unaffected until it's ready to shut down
   the server:

        req1 := &request{7, 8, make(chan int)}
        req2 := &request{17, 18, make(chan int)}

        adderChan <- req1
        adderChan <- req2

        fmt.Println(req2, req1)

   All done, signal server to exit:

         quitChan <- true



Friday, June 10, 2011                                       39
Chaining
      package main

      import ("flag"; "fmt")

      var nGoroutine = flag.Int("n", 100000, "how many")

      func f(left, right chan int) { left <- 1 + <-right }

      func main() {
         flag.Parse()
         leftmost := make(chan int)
         var left, right chan int = nil, leftmost
         for i := 0; i  *nGoroutine; i++ {
            left, right = right, make(chan int)
            go f(left, right)
         }
         right - 0 // bang!
         x := -leftmost // wait for completion
          fmt.Println(x)   // 100000
      }
Friday, June 10, 2011                                        40
Example: Channel as Buffer cache
            var freeList = make(chan *Buffer, 100)
            var serverChan = make(chan *Buffer)
            func server() {
                for {
                    b := -serverChan    // Wait for work to do.
                    process(b)           // Process the request in buffer.
                    select {
                    case freeList - b: // Reuse buffer if room.
                    default:             // Otherwise drop it.
                    }
            }   }
            func client() {
                for {
                    var b *Buffer
                    select {
                    case b = -freeList:     // Grab one if available.
                    default: b = new(Buffer) // Allocate if not.
                    }
                    load(b)                  // Read next request into b.
                    serverChan - b          // Send request to server.
                }
            }
Friday, June 10, 2011                                                        41
Concurrency




Friday, June 10, 2011                 42
Concurrency issues
      Many issues, of course, but Go tries to take care of
      them. Channel send and receive are atomic. The
      select statement is very carefully defined and
      implemented, etc.

      But goroutines run in shared memory,
      communication networks can deadlock,
      multithreaded debuggers suck, and so on.

      What to do?




Friday, June 10, 2011                                        43
Go gives you the primitives
      Don't program the way you would in C or C++ or
      even Java.

      Channels give you synchronization and
      communication both, and that makes them powerful
      but also easy to reason about if you use them well.

      The rule is:

            Do not communicate by sharing memory.
            Instead, share memory by communicating.

      The very act of communication guarantees
      synchronization!
Friday, June 10, 2011                                       44
The model

      For instance, use a channel to send data to a
      dedicated server goroutine. If only one goroutine at
      a time has a pointer to the data, there are no
      concurrency issues.

      This is the model we advocate for programming
      servers, at least. It's the old one thread per client
      approach, generalized - and in use since the 1980s.
      It works very well.

      My old talk (mentioned above) goes into this idea in
      more depth.


Friday, June 10, 2011                                           45
The memory model

      The nasty details about synchronization and shared
      memory are written down at:

            http://golang.org/doc/go_mem.html

      But you rarely need to understand them if you follow
      our approach.




Friday, June 10, 2011                                        46
The
                                Go
                        Programming Language

                                Part 3

                                Rob Pike
                              r@google.com
                           (updated June 2011)


Friday, June 10, 2011                            47

Contenu connexe

Tendances

Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.Niraj Bharambe
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to PythonNowell Strite
 
Learn python – for beginners
Learn python – for beginnersLearn python – for beginners
Learn python – for beginnersRajKumar Rampelli
 
Python Programming Language
Python Programming LanguagePython Programming Language
Python Programming LanguageDr.YNM
 
Introduction to go language programming
Introduction to go language programmingIntroduction to go language programming
Introduction to go language programmingMahmoud Masih Tehrani
 
Python intro
Python introPython intro
Python introrik0
 
Python Seminar PPT
Python Seminar PPTPython Seminar PPT
Python Seminar PPTShivam Gupta
 
Overview of python 2019
Overview of python 2019Overview of python 2019
Overview of python 2019Samir Mohanty
 
Phython Programming Language
Phython Programming LanguagePhython Programming Language
Phython Programming LanguageR.h. Himel
 
Programming with Python - Adv.
Programming with Python - Adv.Programming with Python - Adv.
Programming with Python - Adv.Mosky Liu
 
Introduction To Python | Edureka
Introduction To Python | EdurekaIntroduction To Python | Edureka
Introduction To Python | EdurekaEdureka!
 
Basic Concepts in Python
Basic Concepts in PythonBasic Concepts in Python
Basic Concepts in PythonSumit Satam
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsRuth Marvin
 

Tendances (19)

Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
 
Python by Rj
Python by RjPython by Rj
Python by Rj
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 
Learn python – for beginners
Learn python – for beginnersLearn python – for beginners
Learn python – for beginners
 
Python Programming Language
Python Programming LanguagePython Programming Language
Python Programming Language
 
Introduction to go language programming
Introduction to go language programmingIntroduction to go language programming
Introduction to go language programming
 
Python basics
Python basicsPython basics
Python basics
 
Python intro
Python introPython intro
Python intro
 
Python Seminar PPT
Python Seminar PPTPython Seminar PPT
Python Seminar PPT
 
Python_in_Detail
Python_in_DetailPython_in_Detail
Python_in_Detail
 
Overview of python 2019
Overview of python 2019Overview of python 2019
Overview of python 2019
 
Python Workshop
Python WorkshopPython Workshop
Python Workshop
 
Phython Programming Language
Phython Programming LanguagePhython Programming Language
Phython Programming Language
 
Programming with Python - Adv.
Programming with Python - Adv.Programming with Python - Adv.
Programming with Python - Adv.
 
Python Workshop
Python WorkshopPython Workshop
Python Workshop
 
Python training
Python trainingPython training
Python training
 
Introduction To Python | Edureka
Introduction To Python | EdurekaIntroduction To Python | Edureka
Introduction To Python | Edureka
 
Basic Concepts in Python
Basic Concepts in PythonBasic Concepts in Python
Basic Concepts in Python
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_arguments
 

En vedette

Assignment 16
Assignment 16Assignment 16
Assignment 16rfasil22
 
Sylva workshop.gt that camp.2012
Sylva workshop.gt that camp.2012Sylva workshop.gt that camp.2012
Sylva workshop.gt that camp.2012CameliaN
 
Assignment 11 draft 4
Assignment 11 draft 4Assignment 11 draft 4
Assignment 11 draft 4rfasil22
 
Listening to the Earth: An Environmental Audit For Benedictine Communities
Listening to the Earth: An Environmental Audit For Benedictine Communities  Listening to the Earth: An Environmental Audit For Benedictine Communities
Listening to the Earth: An Environmental Audit For Benedictine Communities Z2P
 
01s0401 go,互联网时代的c语言 许式伟
01s0401 go,互联网时代的c语言   许式伟01s0401 go,互联网时代的c语言   许式伟
01s0401 go,互联网时代的c语言 许式伟Zoom Quiet
 

En vedette (6)

Assignment 16
Assignment 16Assignment 16
Assignment 16
 
Sylva workshop.gt that camp.2012
Sylva workshop.gt that camp.2012Sylva workshop.gt that camp.2012
Sylva workshop.gt that camp.2012
 
Go courseday1
Go courseday1Go courseday1
Go courseday1
 
Assignment 11 draft 4
Assignment 11 draft 4Assignment 11 draft 4
Assignment 11 draft 4
 
Listening to the Earth: An Environmental Audit For Benedictine Communities
Listening to the Earth: An Environmental Audit For Benedictine Communities  Listening to the Earth: An Environmental Audit For Benedictine Communities
Listening to the Earth: An Environmental Audit For Benedictine Communities
 
01s0401 go,互联网时代的c语言 许式伟
01s0401 go,互联网时代的c语言   许式伟01s0401 go,互联网时代的c语言   许式伟
01s0401 go,互联网时代的c语言 许式伟
 

Similaire à Go courseday3

Fundamental concurrent programming
Fundamental concurrent programmingFundamental concurrent programming
Fundamental concurrent programmingDimas Prawira
 
Asynchronous Python A Gentle Introduction
Asynchronous Python A Gentle IntroductionAsynchronous Python A Gentle Introduction
Asynchronous Python A Gentle IntroductionPyData
 
NodeJS, CoffeeScript & Real-time Web
NodeJS, CoffeeScript & Real-time WebNodeJS, CoffeeScript & Real-time Web
NodeJS, CoffeeScript & Real-time WebJakub Nesetril
 
Golang Channels use cases
Golang Channels use casesGolang Channels use cases
Golang Channels use casesErhan Yakut
 
Errors and handling them. YOW nights Sydney 2011
Errors and handling them. YOW nights Sydney 2011Errors and handling them. YOW nights Sydney 2011
Errors and handling them. YOW nights Sydney 2011Michael Neale
 
Error Handling Done Differently
Error Handling Done DifferentlyError Handling Done Differently
Error Handling Done DifferentlyCloudBees
 
Continuations in scala (incomplete version)
Continuations in scala (incomplete version)Continuations in scala (incomplete version)
Continuations in scala (incomplete version)Fuqiang Wang
 
YOU WILL REGRET THIS
YOU WILL REGRET THISYOU WILL REGRET THIS
YOU WILL REGRET THISMononcQc
 
WTF Is Messaging And Why You Should Use It?
WTF Is Messaging And Why You Should Use It?WTF Is Messaging And Why You Should Use It?
WTF Is Messaging And Why You Should Use It?James Russell
 
GT Logiciel Libre - Convention Systematic 2011
GT Logiciel Libre - Convention Systematic 2011GT Logiciel Libre - Convention Systematic 2011
GT Logiciel Libre - Convention Systematic 2011Stefane Fermigier
 
Develop Gwt application in TDD
Develop Gwt application in TDDDevelop Gwt application in TDD
Develop Gwt application in TDDUberto Barbini
 
Considerations about Eye Gaze interfaces for people with disabilities: from t...
Considerations about Eye Gaze interfaces for people with disabilities: from t...Considerations about Eye Gaze interfaces for people with disabilities: from t...
Considerations about Eye Gaze interfaces for people with disabilities: from t...Jacques Chueke
 
Yahoo Release Management
Yahoo Release ManagementYahoo Release Management
Yahoo Release ManagementPhil Hollenback
 
Introduction to Google's Go programming language
Introduction to Google's Go programming languageIntroduction to Google's Go programming language
Introduction to Google's Go programming languageMario Castro Contreras
 
Deep-learning based Language Understanding and Emotion extractions
Deep-learning based Language Understanding and Emotion extractionsDeep-learning based Language Understanding and Emotion extractions
Deep-learning based Language Understanding and Emotion extractionsJeongkyu Shin
 
Carton CPAN dependency manager
Carton CPAN dependency managerCarton CPAN dependency manager
Carton CPAN dependency managerTatsuhiko Miyagawa
 
Inroduction to golang
Inroduction to golangInroduction to golang
Inroduction to golangYoni Davidson
 

Similaire à Go courseday3 (20)

Fundamental concurrent programming
Fundamental concurrent programmingFundamental concurrent programming
Fundamental concurrent programming
 
Asynchronous Python A Gentle Introduction
Asynchronous Python A Gentle IntroductionAsynchronous Python A Gentle Introduction
Asynchronous Python A Gentle Introduction
 
NodeJS, CoffeeScript & Real-time Web
NodeJS, CoffeeScript & Real-time WebNodeJS, CoffeeScript & Real-time Web
NodeJS, CoffeeScript & Real-time Web
 
Golang Channels use cases
Golang Channels use casesGolang Channels use cases
Golang Channels use cases
 
Errors and handling them. YOW nights Sydney 2011
Errors and handling them. YOW nights Sydney 2011Errors and handling them. YOW nights Sydney 2011
Errors and handling them. YOW nights Sydney 2011
 
Error Handling Done Differently
Error Handling Done DifferentlyError Handling Done Differently
Error Handling Done Differently
 
Continuations in scala (incomplete version)
Continuations in scala (incomplete version)Continuations in scala (incomplete version)
Continuations in scala (incomplete version)
 
YOU WILL REGRET THIS
YOU WILL REGRET THISYOU WILL REGRET THIS
YOU WILL REGRET THIS
 
Secrets of the GWT
Secrets of the GWTSecrets of the GWT
Secrets of the GWT
 
WTF Is Messaging And Why You Should Use It?
WTF Is Messaging And Why You Should Use It?WTF Is Messaging And Why You Should Use It?
WTF Is Messaging And Why You Should Use It?
 
GT Logiciel Libre - Convention Systematic 2011
GT Logiciel Libre - Convention Systematic 2011GT Logiciel Libre - Convention Systematic 2011
GT Logiciel Libre - Convention Systematic 2011
 
Develop Gwt application in TDD
Develop Gwt application in TDDDevelop Gwt application in TDD
Develop Gwt application in TDD
 
Considerations about Eye Gaze interfaces for people with disabilities: from t...
Considerations about Eye Gaze interfaces for people with disabilities: from t...Considerations about Eye Gaze interfaces for people with disabilities: from t...
Considerations about Eye Gaze interfaces for people with disabilities: from t...
 
What is Python?
What is Python?What is Python?
What is Python?
 
Yahoo Release Management
Yahoo Release ManagementYahoo Release Management
Yahoo Release Management
 
Introduction to Google's Go programming language
Introduction to Google's Go programming languageIntroduction to Google's Go programming language
Introduction to Google's Go programming language
 
Deep-learning based Language Understanding and Emotion extractions
Deep-learning based Language Understanding and Emotion extractionsDeep-learning based Language Understanding and Emotion extractions
Deep-learning based Language Understanding and Emotion extractions
 
RunDeck
RunDeckRunDeck
RunDeck
 
Carton CPAN dependency manager
Carton CPAN dependency managerCarton CPAN dependency manager
Carton CPAN dependency manager
 
Inroduction to golang
Inroduction to golangInroduction to golang
Inroduction to golang
 

Plus de Zoom Quiet

产品信息收集系统Infoc的演变
产品信息收集系统Infoc的演变产品信息收集系统Infoc的演变
产品信息收集系统Infoc的演变Zoom Quiet
 
Zoz pwned-by-the-owner-表惹程序猿
Zoz pwned-by-the-owner-表惹程序猿Zoz pwned-by-the-owner-表惹程序猿
Zoz pwned-by-the-owner-表惹程序猿Zoom Quiet
 
金山云查询系统改进之路1
金山云查询系统改进之路1金山云查询系统改进之路1
金山云查询系统改进之路1Zoom Quiet
 
Zh120226techparty zd-story
Zh120226techparty zd-storyZh120226techparty zd-story
Zh120226techparty zd-storyZoom Quiet
 
Zh120226techparty velocity2011-review
Zh120226techparty velocity2011-reviewZh120226techparty velocity2011-review
Zh120226techparty velocity2011-reviewZoom Quiet
 
Zh120226techparty jeff kit-ios-toolbox
Zh120226techparty jeff kit-ios-toolboxZh120226techparty jeff kit-ios-toolbox
Zh120226techparty jeff kit-ios-toolboxZoom Quiet
 
Velocity2011分享
Velocity2011分享Velocity2011分享
Velocity2011分享Zoom Quiet
 
陈正 Introduction to-sae_python
陈正   Introduction to-sae_python陈正   Introduction to-sae_python
陈正 Introduction to-sae_pythonZoom Quiet
 
111218 zhtechparty-panda讲稿
111218 zhtechparty-panda讲稿111218 zhtechparty-panda讲稿
111218 zhtechparty-panda讲稿Zoom Quiet
 
111218 zhtechparty-移动互联网产品需求分析
111218 zhtechparty-移动互联网产品需求分析111218 zhtechparty-移动互联网产品需求分析
111218 zhtechparty-移动互联网产品需求分析Zoom Quiet
 
111218 zhtechparty-zd-浅谈symbian开发
111218 zhtechparty-zd-浅谈symbian开发111218 zhtechparty-zd-浅谈symbian开发
111218 zhtechparty-zd-浅谈symbian开发Zoom Quiet
 
ImpoImport this, that, and the other thing: custom importersrt not for_y
ImpoImport this, that, and the other thing: custom importersrt not for_yImpoImport this, that, and the other thing: custom importersrt not for_y
ImpoImport this, that, and the other thing: custom importersrt not for_yZoom Quiet
 
Import this, that, and the other thing: custom importers
Import this, that, and the other thing: custom importersImport this, that, and the other thing: custom importers
Import this, that, and the other thing: custom importersZoom Quiet
 
金山卫士界面框架
金山卫士界面框架金山卫士界面框架
金山卫士界面框架Zoom Quiet
 
111030 gztechparty-小路-云时代的mysql
111030 gztechparty-小路-云时代的mysql111030 gztechparty-小路-云时代的mysql
111030 gztechparty-小路-云时代的mysqlZoom Quiet
 
111030 gztechparty-小路-sophia
111030 gztechparty-小路-sophia111030 gztechparty-小路-sophia
111030 gztechparty-小路-sophiaZoom Quiet
 
080328 linux2 bsd
080328 linux2 bsd080328 linux2 bsd
080328 linux2 bsdZoom Quiet
 
110929 kn-手机软件测试
110929 kn-手机软件测试110929 kn-手机软件测试
110929 kn-手机软件测试Zoom Quiet
 

Plus de Zoom Quiet (20)

42qu thrift1
42qu thrift142qu thrift1
42qu thrift1
 
产品信息收集系统Infoc的演变
产品信息收集系统Infoc的演变产品信息收集系统Infoc的演变
产品信息收集系统Infoc的演变
 
Go courseday2
Go courseday2Go courseday2
Go courseday2
 
Zoz pwned-by-the-owner-表惹程序猿
Zoz pwned-by-the-owner-表惹程序猿Zoz pwned-by-the-owner-表惹程序猿
Zoz pwned-by-the-owner-表惹程序猿
 
金山云查询系统改进之路1
金山云查询系统改进之路1金山云查询系统改进之路1
金山云查询系统改进之路1
 
Zh120226techparty zd-story
Zh120226techparty zd-storyZh120226techparty zd-story
Zh120226techparty zd-story
 
Zh120226techparty velocity2011-review
Zh120226techparty velocity2011-reviewZh120226techparty velocity2011-review
Zh120226techparty velocity2011-review
 
Zh120226techparty jeff kit-ios-toolbox
Zh120226techparty jeff kit-ios-toolboxZh120226techparty jeff kit-ios-toolbox
Zh120226techparty jeff kit-ios-toolbox
 
Velocity2011分享
Velocity2011分享Velocity2011分享
Velocity2011分享
 
陈正 Introduction to-sae_python
陈正   Introduction to-sae_python陈正   Introduction to-sae_python
陈正 Introduction to-sae_python
 
111218 zhtechparty-panda讲稿
111218 zhtechparty-panda讲稿111218 zhtechparty-panda讲稿
111218 zhtechparty-panda讲稿
 
111218 zhtechparty-移动互联网产品需求分析
111218 zhtechparty-移动互联网产品需求分析111218 zhtechparty-移动互联网产品需求分析
111218 zhtechparty-移动互联网产品需求分析
 
111218 zhtechparty-zd-浅谈symbian开发
111218 zhtechparty-zd-浅谈symbian开发111218 zhtechparty-zd-浅谈symbian开发
111218 zhtechparty-zd-浅谈symbian开发
 
ImpoImport this, that, and the other thing: custom importersrt not for_y
ImpoImport this, that, and the other thing: custom importersrt not for_yImpoImport this, that, and the other thing: custom importersrt not for_y
ImpoImport this, that, and the other thing: custom importersrt not for_y
 
Import this, that, and the other thing: custom importers
Import this, that, and the other thing: custom importersImport this, that, and the other thing: custom importers
Import this, that, and the other thing: custom importers
 
金山卫士界面框架
金山卫士界面框架金山卫士界面框架
金山卫士界面框架
 
111030 gztechparty-小路-云时代的mysql
111030 gztechparty-小路-云时代的mysql111030 gztechparty-小路-云时代的mysql
111030 gztechparty-小路-云时代的mysql
 
111030 gztechparty-小路-sophia
111030 gztechparty-小路-sophia111030 gztechparty-小路-sophia
111030 gztechparty-小路-sophia
 
080328 linux2 bsd
080328 linux2 bsd080328 linux2 bsd
080328 linux2 bsd
 
110929 kn-手机软件测试
110929 kn-手机软件测试110929 kn-手机软件测试
110929 kn-手机软件测试
 

Dernier

Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
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
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 

Dernier (20)

Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 

Go courseday3

  • 1. The Go Programming Language Part 3 Rob Pike r@google.com (updated June 2011) Friday, June 10, 2011 1
  • 2. Today’s Outline Exercise any questions? Concurrency and communication goroutines channels concurrency issues Friday, June 10, 2011 2
  • 3. Exercise Any questions? For a trivial HTTP server with various generators, see http://golang.org/src/pkg/http/triv.go Friday, June 10, 2011 3
  • 4. Concurrency and communication: Goroutines Friday, June 10, 2011 4
  • 5. Goroutines Terminology: There are many terms for "things that run concurrently" - process, thread, coroutine, POSIX thread, NPTL thread, lightweight process, ..., but these all mean slightly different things. None means exactly how Go does concurrency. So we introduce a new term: goroutine. Friday, June 10, 2011 5
  • 6. Definition A goroutine is a Go function or method executing concurrently in the same address space as other goroutines. A running program consists of one or more goroutines. It's not the same as a thread, coroutine, process, etc. It's a goroutine. Note: Concurrency and parallelism are different concepts. Look them up if you don't understand the difference. There are many concurrency questions. They will be addressed later; for now just assume it all works as advertised. Friday, June 10, 2011 6
  • 7. Starting a goroutine Invoke a function or method and say go: func IsReady(what string, minutes int64) { time.Sleep(minutes * 60*1e9) // Unit is nanosecs. fmt.Println(what, "is ready") } go IsReady("tea", 6) go IsReady("coffee", 2) fmt.Println("I'm waiting...") Prints: I'm waiting... (right away) coffee is ready (2 minutes later) tea is ready (6 minutes later) Friday, June 10, 2011 7
  • 8. Some simple facts Goroutines are cheap. Goroutines exit by returning from their top-level function, or just falling off the end. Goroutines can run concurrently on different processors, sharing memory. You don't have to worry about stack size. Friday, June 10, 2011 8
  • 9. Stacks In gccgo, at least for now, goroutines are pthreads. In 6g they're multiplexed onto threads, so they are much cheaper. In both worlds, stacks are small (a few kB) and grow as needed. Thus goroutines use little memory, you can have lots of them, and they can dynamically have huge stacks. The programmer shouldn't have to think about the stack size issue, and in Go, it doesn't even come up. Friday, June 10, 2011 9
  • 10. Scheduling Goroutines are multiplexed as needed onto system threads. When a goroutine executes a blocking system call, no other goroutine is blocked. Plan to do the same for CPU-bound goroutines at some point, but for now, if you want user- level parallelism in 6g* you must set shell var. GOMAXPROCS or call runtime.GOMAXPROCS(n). GOMAXPROCS tells the runtime scheduler how many user-space-executing goroutines to run at once, ideally on different CPU cores. * gccgo always uses one thread per goroutine. Friday, June 10, 2011 10
  • 11. Concurrency and communication: Channels Friday, June 10, 2011 11
  • 12. Channels in Go Unless two goroutines can communicate, they can't coordinate. Go has a type called a channel that provides communication and synchronization capabilities. It also has special control structures that build on channels to make concurrent programming easy. Note: I gave a talk in 2006 about predecessor stuff in an older language. http://www.youtube.com/watch?v=HmxnCEa8Ctw Friday, June 10, 2011 12
  • 13. The channel type In its simplest form the type looks like this: chan elementType With a value of this type, you can send and receive items of elementType. Channels are a reference type, which means if you assign one chan variable to another, both variables access the same channel. It also means you use make to allocate one: var c = make(chan int) Friday, June 10, 2011 13
  • 14. The communication operator: <- The arrow points in the direction of data flow. As a binary operator, <- sends the value on the right to the channel on the left: c := make(chan int) c <- 1 // send 1 on c (flowing into c) As a prefix unary operator, <- receives from a channel: v = <-c // receive value from c, assign to v <-c // receive value, throw it away i := <-c // receive value, initialize i Friday, June 10, 2011 14
  • 15. Semantics By default, communication is synchronous. (We'll talk about asynchronous communication later.) This means: 1) A send operation on a channel blocks until a receiver is available for the same channel. 2) A receive operation for a channel blocks until a sender is available for the same channel. Communication is therefore a form of synchronization: two goroutines exchanging data through a channel synchronize at the moment of communication. Friday, June 10, 2011 15
  • 16. Let's pump some data func pump(ch chan int) { for i := 0; ; i++ { ch <- i } } ch1 := make(chan int) go pump(ch1) // pump hangs; we run fmt.Println(<-ch1) // prints 0 Now we start a looping receiver. func suck(ch chan int) { for { fmt.Println(<-ch) } } go suck(ch1) // tons of numbers appear You can still sneak in and grab a value: fmt.Println(<-ch1) // Prints 314159 Friday, June 10, 2011 16
  • 17. Functions returning channels In the previous example, pump was like a generator spewing out values. But there was a lot of fuss allocating channels etc. Let's package it up into a function returning the channel of values. func pump() chan int { ch := make(chan int) go func() { for i := 0; ; i++ { ch <- i } }() return ch } stream := pump() fmt.Println(<-stream) // prints 0 "Function returning channel" is an important idiom. Friday, June 10, 2011 17
  • 18. Channel functions everywhere I am avoiding repeating famous examples you can find elsewhere. Here are a couple to look up: 1) The prime sieve; in the language specification and also in the tutorial. 2) Doug McIlroy's power series paper: http://plan9.bell-labs.com/who/rsc/thread/squint.pdf A Go version of this amazing program is in available in the test suite as: http://golang.org/test/chan/powser1.go Friday, June 10, 2011 18
  • 19. Range and channels The range clause on for loops accepts a channel as an operand, in which case the for loops over the values received from the channel. We rewrote pump; here's the rewrite for suck, making it launch the goroutine as well: func suck(ch chan int) { go func() { for v := range ch { fmt.Println(v) } }() } suck(pump()) // doesn't block now Friday, June 10, 2011 19
  • 20. Closing a channel How does range know when the channel is done delivering data? The sender calls the built-in function close: close(ch) Receiver tests if the sender has closed the channel using "comma ok": val, ok := <-ch Result is (value, true) while values are available; once channel is closed and drained, result is (zero, false). Friday, June 10, 2011 20
  • 21. Range on a channel, by hand A for range on a channel such as: for value := range <-ch { use(value) } is equivalent to: for { value, ok := <-ch if !ok { break } use(value) } Friday, June 10, 2011 21
  • 22. Close Key points: Only the sender should call close. Only the receiver can ask if channel has been closed. Can only ask while getting a value (avoids races). Call close only when it's necessary to signal to the receiver that no more values will arrive. Most of the time, close isn't needed; it's not analogous to closing a file. Channels are garbage-collected regardless. Friday, June 10, 2011 22
  • 23. Channel directionality In its simplest form a channel variable is an unbuffered (synchronous) value that can be used to send and receive. A channel type may be annotated to specify that it may only send or only receive: var recvOnly <-chan int var sendOnly chan<- int Friday, June 10, 2011 23
  • 24. Channel directionality (II) All channels are created bidirectional, but we can assign them to directional channel variables. Useful for instance in functions, for (type) safety: func sink(ch <-chan int) { for { <-ch } } func source(ch chan<- int) { for { ch <- 1 } } c := make(chan int) // bidirectional go source(c) go sink(c) Friday, June 10, 2011 24
  • 25. Synchronous channels Synchronous channels are unbuffered. Sends do not complete until a receiver has accepted the value. c := make(chan int) go func() { time.Sleep(60*1e9) x := <-c fmt.Println("received", x) }() fmt.Println("sending", 10) c <- 10 fmt.Println("sent", 10) Output: sending 10 (happens immediately) sent 10 (60s later, these 2 lines appear) received 10 Friday, June 10, 2011 25
  • 26. Asynchronous channels A buffered, asynchronous channel is created by telling make the number of elements in the buffer. c := make(chan int, 50) go func() { time.Sleep(60*1e9) x := <-c fmt.Println("received", x) }() fmt.Println("sending", 10) c <- 10 fmt.Println("sent", 10) Output: sending 10 (happens immediately) sent 10 (now) received 10 (60s later) Friday, June 10, 2011 26
  • 27. Buffer is not part of the type Note that the buffer's size, or even its existence, is not part of the channel's type, only of the value. This code is therefore legal, although dangerous: buf = make(chan int, 1) unbuf = make(chan int) buf = unbuf unbuf = buf Buffering is a property of the value, not of the type. Friday, June 10, 2011 27
  • 28. Select Select is a control structure in Go analogous to a communications switch statement. Each case must be a communication, either send or receive. ci, cs := make(chan int), make(chan string) select { case v := <-ci: fmt.Printf("received %d from cin", v) case v := <-cs: fmt.Printf("received %s from csn", v) } Select executes one runnable case at random. If no case is runnable, it blocks until one is. A default clause is always runnable. Friday, June 10, 2011 28
  • 29. Select semantics Quick summary: - Every case must be a (possibly :=) communication - All channel expressions are evaluated - All expressions to be sent are evaluated - If any communication can proceed, it does; others are ignored - If multiple cases are ready, one is selected at random, fairly. Others do not execute. - Otherwise: - If there is a default clause, that runs - If there is no default, select statement blocks until one communication can proceed; there is no re-evaluation of channels or values Friday, June 10, 2011 29
  • 30. Random bit generator Silly but illustrative example. c := make(chan int) go func() { for { fmt.Println(<-c) } }() for { select { case c <- 0: // no statement, no fall through case c <- 1: } } Prints 0 1 1 0 0 1 1 1 0 1 ... Friday, June 10, 2011 30
  • 31. Testing for communicability Can a communication proceed without blocking? Select with a default clause can tell us: select { case v := <-ch: fmt.Println("received", v) default: fmt.Println("ch not ready for receive") } The default clause executes if no other case can proceed, so this is the idiom for non-blocking receive; non-blocking send is the obvious variant. Friday, June 10, 2011 31
  • 32. Timeout Can a communication succeed in a given interval? Package time contains the After function: func After(ns int64) <-chan int64 It delivers a value (the current time) on the returned channel after the specified interval. Use it in select to implement timeout: select { case v := <-ch: fmt.Println("received", v) case <-time.After(30*1e9): fmt.Println("timed out after 30 seconds") } Friday, June 10, 2011 32
  • 33. Multiplexing Channels are first-class values, which means they can be sent over channels. This property makes it easy to write a service multiplexer since the client can supply, along with its request, the channel on which to reply. chanOfChans := make(chan chan int) Or more typically type Reply struct { ... } type Request struct { arg1, arg2 someType replyc chan *Reply } Friday, June 10, 2011 33
  • 34. Multiplexing server type request struct { a, b int replyc chan int } type binOp func(a, b int) int func run(op binOp, req *request) { req.replyc <- op(req.a, req.b) } func server(op binOp, service <-chan *request) { for { req := <-service // requests arrive here go run(op, req) // don't wait for op } } Friday, June 10, 2011 34
  • 35. Starting the server Use the "function returning channel" idiom to create a channel to a new server: func startServer(op binOp) chan<- *request { service := make(chan *request) go server(op, req) return service } adderChan := startServer( func(a, b int) int { return a + b } ) Friday, June 10, 2011 35
  • 36. The client A similar example is worked in more detail in the tutorial, but here's a variant: func (r *request) String() string { return fmt.Sprintf("%d+%d=%d", r.a, r.b, <-r.replyc) } req1 := &request{7, 8, make(chan int)} req2 := &request{17, 18, make(chan int)} Requests are ready; send them: adderChan <- req1 adderChan <- req2 Can retrieve results in any order; r.replyc demuxes: fmt.Println(req2, req1) Friday, June 10, 2011 36
  • 37. Teardown In the multiplex example, the server runs forever. To tear it down cleanly, signal with a channel. This server has the same functionality but with a quit channel: func server(op binOp, service <-chan *request, quit <-chan bool) { for { select { case req := <-service: go run(op, req) // don't wait for it case <-quit: return } } } Friday, June 10, 2011 37
  • 38. Starting the server The rest of the code is mostly the same, with an extra channel: func startServer(op binOp) (service chan<- *request, quit chan<- bool) { service = make(chan *request) quit = make(chan bool) go server(op, service, quit) return service, quit } adderChan, quitChan := startServer( func(a, b int) int { return a + b } ) Friday, June 10, 2011 38
  • 39. Teardown: the client The client is unaffected until it's ready to shut down the server: req1 := &request{7, 8, make(chan int)} req2 := &request{17, 18, make(chan int)} adderChan <- req1 adderChan <- req2 fmt.Println(req2, req1) All done, signal server to exit: quitChan <- true Friday, June 10, 2011 39
  • 40. Chaining package main import ("flag"; "fmt") var nGoroutine = flag.Int("n", 100000, "how many") func f(left, right chan int) { left <- 1 + <-right } func main() { flag.Parse() leftmost := make(chan int) var left, right chan int = nil, leftmost for i := 0; i *nGoroutine; i++ { left, right = right, make(chan int) go f(left, right) } right - 0 // bang! x := -leftmost // wait for completion fmt.Println(x) // 100000 } Friday, June 10, 2011 40
  • 41. Example: Channel as Buffer cache var freeList = make(chan *Buffer, 100) var serverChan = make(chan *Buffer) func server() { for { b := -serverChan // Wait for work to do. process(b) // Process the request in buffer. select { case freeList - b: // Reuse buffer if room. default: // Otherwise drop it. } } } func client() { for { var b *Buffer select { case b = -freeList: // Grab one if available. default: b = new(Buffer) // Allocate if not. } load(b) // Read next request into b. serverChan - b // Send request to server. } } Friday, June 10, 2011 41
  • 43. Concurrency issues Many issues, of course, but Go tries to take care of them. Channel send and receive are atomic. The select statement is very carefully defined and implemented, etc. But goroutines run in shared memory, communication networks can deadlock, multithreaded debuggers suck, and so on. What to do? Friday, June 10, 2011 43
  • 44. Go gives you the primitives Don't program the way you would in C or C++ or even Java. Channels give you synchronization and communication both, and that makes them powerful but also easy to reason about if you use them well. The rule is: Do not communicate by sharing memory. Instead, share memory by communicating. The very act of communication guarantees synchronization! Friday, June 10, 2011 44
  • 45. The model For instance, use a channel to send data to a dedicated server goroutine. If only one goroutine at a time has a pointer to the data, there are no concurrency issues. This is the model we advocate for programming servers, at least. It's the old one thread per client approach, generalized - and in use since the 1980s. It works very well. My old talk (mentioned above) goes into this idea in more depth. Friday, June 10, 2011 45
  • 46. The memory model The nasty details about synchronization and shared memory are written down at: http://golang.org/doc/go_mem.html But you rarely need to understand them if you follow our approach. Friday, June 10, 2011 46
  • 47. The Go Programming Language Part 3 Rob Pike r@google.com (updated June 2011) Friday, June 10, 2011 47