Slide 1

Slide 1 text

Go for Python Programmers Alexandre Vicenzi Pythonista at Red Hat

Slide 2

Slide 2 text

History ● Robert Griesemer, Rob Pike and Ken Thompson ● Google ● Began in late 2007 ● First public release in 2009 ● First stable release in 2012

Slide 3

Slide 3 text

Why a new language? ● Frustration with existing languages ● Programming had become too difficult ● Many years with a quiet landscape of choices ● To take advantage of networking and multicore

Slide 4

Slide 4 text

Features ● Compiles to native machine code ● Memory safety ● Garbage collection ● Concurrency

Slide 5

Slide 5 text

Go vs Python

Slide 6

Slide 6 text

Code organization ● Keep everything in a single workspace ○ src - source files ○ bin - executable commands ● GOPATH environment variable ($HOME/go) ● Workspace contains multiple repositories ● Import path is the package location in the workspace

Slide 7

Slide 7 text

Package management ● No official tool yet ● Tools ○ dep - The official experiment ○ Godep ○ Govendor ○ Glide ○ many others ● Modules ○ Preliminary support ○ No need for GOPATH anymore ○ Go 1.13 (August 2019)

Slide 8

Slide 8 text

Object Oriented Programming ● No classes ● No inheritance ● No generics ● Interfaces ● Composition ● Type embedding

Slide 9

Slide 9 text

Pointers ● Everything in Go is passed by value ● No pointer arithmetic ● Pointer is represented by * ● * is also used to “dereference” ● & returns the memory address ● new function

Slide 10

Slide 10 text

Go concurrency ● Goroutines ○ Function executing concurrently with other functions in the same address space ○ Goroutines are multiplexed onto multiple OS threads ● Channels ○ Typed, synchronized, thread-safe by design ○ Buffered and Unbuffered (default)

Slide 11

Slide 11 text

What else is different? ● No decorators ● No named or optional arguments ● No iterators ● No generators ● No exceptions ● Part of the C family ● Labels and goto

Slide 12

Slide 12 text

Your first program package main import "fmt" func main() { fmt.Println("Hello, world.") }

Slide 13

Slide 13 text

factorial.py def factorial(n): if n == 0: return 1 return n * factorial(n - 1) print(factorial(5))

Slide 14

Slide 14 text

factorial.go package main import "fmt" func factorial(n int) int { if n == 0 { return 1 } return n * factorial(n-1) } func main() { fmt.Println(factorial(5)) }

Slide 15

Slide 15 text

fibonacci.py def fibonacci(n): a, b = 0, 1 yield a for i in range(n): a, b = b, a + b yield a for i in fibonacci(10): print(i)

Slide 16

Slide 16 text

fibonacci.go package main import "fmt" func fibonacci(c chan int, n int) { a, b := 0, 1 c <- a for i := 0; i < n; i++ { a, b = b, a+b c <- a } close(c) } func main() { c := make(chan int) go fibonacci(c, 10) for i := range c { fmt.Println(i) } }

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Conclusion Go is ● Simple ● Concurrent ● Robust ● Fun

Slide 19

Slide 19 text

Next steps ● golang.org ● tour.golang.org ● groups.google.com/d/forum/golang-nuts ● golang-book.com

Slide 20

Slide 20 text

Thank you Alexandre Vicenzi Pythonista at Red Hat alexandrevicenzi.com @alxvicenzi