2. A response to Google’s internal needs: - efficient large scale programming - speed of compilation - distributed systems - multicore hardware - networked computing
4. A complete (if simple) web server: import ( "fmt" "http" ) func handler(c *http.Conn, r *http.Request) { fmt.Fprintf(c, "Hello, %s.", r.URL.Path[1:]) } func main() { http.ListenAndServe(":8080", http.HandlerFunc(handler)) }
5. concepts: * Go's concurrency is strongly influenced by Communicating Sequential Processes (Hoare, 1978) * Go has types and interfaces, but no inheritance. It is arguably more object-oriented than previously mentioned languages, being closer to the original Smalltalk meaning (1970s)
7. Concurrent: * In traditional concurrent programs, you communicate by sharing memory. In Go, you share memory by communicating. * Threads and locks are concurrency primitives; CSP is a concurrency model.
8. Parallel computation is easy: var a string func f() { a = "hello, world" } func main() { go f() println(a) }
9. via lock: var l sync.Mutex var a string func f() { a = "hello, world" l.Unlock() } func main() { l.Lock() go f() l.Lock() println(a) }
10. via channel: var c = make(chan int, 10) var a string func f() { a = "hello, world" c <- 0 } func main() { go f() <- c println(a) }
11. via once: var a string var once sync.Once func setup() { println("invoke setup()") a = "hello, world" } func doprint() { once.Do(setup) print(a) } func twoprint() { go doprint() go doprint() }