Friday, April 06, 2018

book: The Go Programming Language

book:
The Go Programming Language
Alan A. A. Donovan · Brian W. Kernighan

"When you’re learning a new language, there’s a natural tendency to write code as you would have written it in a language you already know. Be aware of this bias as you learn Go and try to avoid it."

"Do not communicate by sharing memory;
instead, share memory by communicating."



adonovan/gopl.io: Example programs from "The Go Programming Language" @ GitHub


The Go Programming Language (Addison-Wesley Professional Computing Series): 9780134190440: Computer Science Books @ Amazon.com

Many people learned C lang from this book:
Amazon.com: C Programming Language, 2nd Edition (8601410794231): Brian W. Kernighan, Dennis M. Ritchie: Books

When co-creator of C  Ken Thompson co-created new Go programming language,
Brian W. Kernighan, co-author of original C book, co-authored new excellent Go book.

With book on new computer language, Kernighan guides students at Princeton and beyond


Notes and examples from the book:

notes/index.md at master · shichao-an/notes @ GitHub

Contents - Shichao's Notes

Chapter 9. Concurrency with Shared Variables - Shichao's Notes

Thread-safety without explicit locks:

// Package bank provides a concurrency-safe bank with one account.
package bank

var deposits = make(chan int) // send amount to deposit
var balances = make(chan int) // receive balance

func Deposit(amount int) { deposits <- amount }
func Balance() int       { return <-balances }

func teller() {
    var balance int // balance is confined to teller goroutine
    for {
        select {
        case amount := <-deposits:
            balance += amount
        case balances <- balance:
        }
    }
}

func init() {
    go teller() // start the monitor goroutine
}

No comments: