This is what
our presentations
should look like
JAN. 2, 2015
Go:
Concurrency
is Built In
MAY 14,, 2015
What’s Similar?
GOLANG vs PYTHON
Similarities with Python
• Can be “imperative” or “object-oriented”
• Great for writing web servers (guess what my day job is)
• Arrays use Python-like slicing
• Easy iteration over arrays (lists) and maps (dicts) using range
• Multiple return values from functions
• Package management (go get vs. pip install)
• Anonymous functions
3
What’s Different?
GOLANG vs PYTHON
Differences with Python
• Structs vs. objects
• Strongly-typed
• Compiled, C-like syntax (ugh)
• Interface vs inheritance
• Modules vs. packages
• camelCase
• CapitalName : capitalName :: attribute : _attribute
• return errors instead of raising exceptions
5
Go has no GIL
GOLANG vs PYTHON
It’s not bolted on like asyncio, or hacked in à la Tornado.
The Go runtime has a scheduler that automatically schedules multiple
concurrent operations efficiently and automatically.
It’s so efficient, it doesn’t even guarantee all concurrent operations even
run!
Concurrency is built into the runtime
7
Concurrency
• goroutines
• defer
• wait groups (via sync package)
• channels
8
Everything in Go is a “goroutine”, including main(). Dispatch a function to
execute in a separate goroutine using the go keyword:
func doStuff(string a) {
fmt.Println(a)
}
func main() {
var a string = “Python is cool!”
go doStuff(a)
}
goroutines
9
You can defer execution of a line until the end of a function:
func processFile(string filename) bool, error {
f, err := os.Open(filename)
defer f.close()
if err != nil {
return false, err
}
...
return true, nil
}
defer
10
Fire off multiple goroutines, wait until they are done before moving on:
var wg sync.WaitGroup
var urls = []string{
"http://www.golang.org/",
"http://www.google.com",
"http://www.somestupidname.com/",
}
for _, url := range urls {
wg.Add(1)
go func(url string) {
defer wg.Done()
http.Get(url)
}(url)
}
wg.Wait()
wait group
11
Allow communication/synchronization between goroutines:
func pinger(c chan string) {
for i := 0; ; i++ {
c <- "ping"
}
}
func printer(c chan string) {
for {
msg := <- c
fmt.Println(msg)
time.Sleep(time.Second * 1)
}
}
channels
12
func main() {
var c chan string = make(chan string)
go pinger(c)
go printer(c)
var input string
fmt.Scanln(&input)
}
13
“Object-oriented design is the
roman numerals of
computing.”
GOLANG DESIGNER ROB PIKE
Use the best tool for the job.
MY ADVICE:
http://tour.golang.org
START HERE

Golang preso

  • 1.
    This is what ourpresentations should look like JAN. 2, 2015 Go: Concurrency is Built In MAY 14,, 2015
  • 2.
  • 3.
    Similarities with Python •Can be “imperative” or “object-oriented” • Great for writing web servers (guess what my day job is) • Arrays use Python-like slicing • Easy iteration over arrays (lists) and maps (dicts) using range • Multiple return values from functions • Package management (go get vs. pip install) • Anonymous functions 3
  • 4.
  • 5.
    Differences with Python •Structs vs. objects • Strongly-typed • Compiled, C-like syntax (ugh) • Interface vs inheritance • Modules vs. packages • camelCase • CapitalName : capitalName :: attribute : _attribute • return errors instead of raising exceptions 5
  • 6.
    Go has noGIL GOLANG vs PYTHON
  • 7.
    It’s not boltedon like asyncio, or hacked in à la Tornado. The Go runtime has a scheduler that automatically schedules multiple concurrent operations efficiently and automatically. It’s so efficient, it doesn’t even guarantee all concurrent operations even run! Concurrency is built into the runtime 7
  • 8.
    Concurrency • goroutines • defer •wait groups (via sync package) • channels 8
  • 9.
    Everything in Gois a “goroutine”, including main(). Dispatch a function to execute in a separate goroutine using the go keyword: func doStuff(string a) { fmt.Println(a) } func main() { var a string = “Python is cool!” go doStuff(a) } goroutines 9
  • 10.
    You can deferexecution of a line until the end of a function: func processFile(string filename) bool, error { f, err := os.Open(filename) defer f.close() if err != nil { return false, err } ... return true, nil } defer 10
  • 11.
    Fire off multiplegoroutines, wait until they are done before moving on: var wg sync.WaitGroup var urls = []string{ "http://www.golang.org/", "http://www.google.com", "http://www.somestupidname.com/", } for _, url := range urls { wg.Add(1) go func(url string) { defer wg.Done() http.Get(url) }(url) } wg.Wait() wait group 11
  • 12.
    Allow communication/synchronization betweengoroutines: func pinger(c chan string) { for i := 0; ; i++ { c <- "ping" } } func printer(c chan string) { for { msg := <- c fmt.Println(msg) time.Sleep(time.Second * 1) } } channels 12 func main() { var c chan string = make(chan string) go pinger(c) go printer(c) var input string fmt.Scanln(&input) }
  • 13.
    13 “Object-oriented design isthe roman numerals of computing.” GOLANG DESIGNER ROB PIKE
  • 14.
    Use the besttool for the job. MY ADVICE:
  • 15.