Hacker Forum: The Go Programming Language
Hacker Union
2 December 2013
Aditya Mukerjee
Aditya Mukerjee
Full list of design goals, according to Rob Pike: http://talks.golang.org/2012/splash.article
See also: Go for Pythonists
Most of the points from the previous slide apply
func addOne(a int) int { return a + 1 } func main(){ a := 5 b := addOne(a) //Assigned type is inferred }
func doSomething(){ var foo int foo = 6 bar := 8 }
func PrintAllNames(names []string){ for _, name := range names { if name != "" { fmt.Println(name) } } }
user, err := api.GetUser(id) if err != nil{ // Handle API error }
type CheckingAccount struct { Balance int superSecretId int64 }
type Person struct { MainAccount CheckingAccount } type Bank struct { Accounts []CheckingAccount SavingsAccounts []struct{ Balance int InterestRate float64 } }
type Account interface { Deposit(int) error } type CheckingAccount struct { Balance int superSecretId int64 } func (destination CheckingAccount) Deposit(amount int) error { destination.Balance += amount return nil } // We want to donate money to any account type (Checking, Savings) // Donations require a tax receipt func Donate(recipient Account, amount int) error { err := recipient.Deposit(amount) if err != nil{ return err } IssueTaxReceipt(amount) }
From the docs:
- A goroutine is "a function executing concurrently with other goroutines in the same address space"
- Goroutines are light
- Goroutines are multiplexed onto multiple OS threads
- Goroutines are similar to the & operator in bash/sh
func Greet(name string) { log.Printf("Greetings, %s!", name) time.Sleep(3 * time.Second) } func main() { Greet("Alice") Greet("Bob") }
func Greet(name string) { log.Printf("Greetings, %s!", name) time.Sleep(3 * time.Second) } func main() { go Greet("Alice") go Greet("Bob") time.Sleep(5 * time.Second) }
Yes, it really is just that easy.
func Greet(name string, response_chan chan string) { greeting := fmt.Sprintf("Greetings, %s!", name) response_chan <- greeting } func main() { cs := make(chan string) go Greet("Alice", cs) greeting := <-cs log.Print(greeting) }
func Greet(name string, response_chan chan string) { time.Sleep(3 * time.Second) greeting := fmt.Sprintf("Greetings, %s!", name) response_chan <- greeting log.Print("Greeting function is tired - sleeping for a bit") time.Sleep(3 * time.Second) log.Print("now the greeting function is done sleeping - terminating") } func main() { cs := make(chan string) go Greet("Alice", cs) log.Print("Continuing execution while we wait for greeter to respond") greeting := <-cs log.Print(greeting) time.Sleep(10 * time.Second) }
Some tools included for free in the standard library:
//Execute a query that will automatically be throttled func (c *TwitterApi) throttledQuery(queryQueue chan queryChan) { for q := range queryQueue { endpoint_path := q.endpoint_path method := q.method keyVals := q.keyVals response_ch := q.response_ch err := c.execQuery(url, form, data, method) response_ch <- struct { result []byte err error }{result, err} // Reading from this will block until the specified time has elapsed <-time.After(SECONDS_PER_QUERY) } }
select { case <-algorithm1: // a read from ch has occurred case <-algorithm2: // the read from ch has timed out }
select { case <-ch: // a read from ch has occurred case <-timeout: // the read from ch has timed out }
(See Pluto on Github)
go-json - automatically generate static struct definitions for JSON unmarshalling
mgo - a MongoDB client library for Go
redigo - A Redis client library in Go
Aditya Mukerjee