Go @ Barcamp Saigon

Concurrent programming with Go

"Using the websocket package, [...] was able to add the web viewer on his train ride home."

"We wrote [...] in two or three beers."

What is Go?

Why Go?

Concurrency!

CSP

Goroutines

Channels

Concurrency primitives demo

func main() {
      c := make(chan string)
      d := make(chan string)

      go func() {
          for {
              c <- "msg" // sending blocks until there is a receiver
          }
      }()
      go func() {
          for {
              d <- ("relayed " + <-c) // process received value, then send it out
          }
      }()

      for {
          select {
          case val := <-c:
              println(val)
          case val := <-d:
              println(val)
          }
      }
}
      

Select

              select {
              case x = <-c:
                  // x was received from c
              case d <- y:
                  // y was sent to d
              default:
                  // non-blocking case
              }
      

The Go Way

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

A concurrent windows system

A TCP echo server

spawns multiple goroutines and also does informative logging :)

import ("net"; "io"; "os"; "log")

func die(err os.Error) {
    if err != nil { log.Fatal(err) }
}

func main() {
    server, err := net.Listen("tcp", "127.0.0.1:3640"); die(err)
    for {
        conn, err := server.Accept(); die(err)
        go func() {
            defer conn.Close()
            n, err := io.Copy(conn, conn)
            log.Printf("echoed %d byte to %s\n", n, conn.RemoteAddr())
            if err != nil {
                log.Printf("error: %s", err)
            }
        }()
    }
}
      

Sequential goroutine but non-blocking I/O

Go is a lot more than concurrency

A few bits of history

Where to go from here?

Thank you! Questions?

For more information about our company, visit Skunkworks.vn. We are hiring!