5. “Can't reach the pedals. no brakes.
gaining speed. eyes bulging in horror.
technical debt crash eminent. “
— Tim Dysinger, @dysinger
6. Paul Phillips
@extempore2
“go is a terrible, terrible language, yet
still a major productivity boost.
Amdahl's law in another context.”
“You often see languages which are
fighting the last war. Go is fighting
the War of 1812.”
7. “reduce: what you will do with your
expectations after you start with Go”
“Rust and Scala drown you in
complexity. Go drowns you in
simplicity.”
Paul Phillips
@extempore2
19. func main() {
fmt.Println("1 + 2 =", add(1, 2))
}
func add(a int, b int) int {
return a + b
}
Functions
20. func main() {
fmt.Println("1 + 2 =", add(1, 2))
}
func add(a int, b int) int {
return a + b
}
Functions
21. func main() {
fmt.Println("1 + 2 =", add(1, 2))
}
func add(a, b int) int {
return a + b
}
Functions
22. func main() {
s, p := calculate(1, 2)
fmt.Printf("1+2=%d, 1*2=%d", s, p)
}
func calculate(a, b int) (int, int) {
return a + b, a * b
}
Functions
23. func main() {
s, p := calculate(1, 2)
fmt.Printf("1+2=%d, 1*2=%d", s, p)
}
func calculate(a, b int) (int, int) {
return a + b, a * b
}
Functions
24. func main() {
s, p := calculate(1, 2)
fmt.Printf("1+2=%d, 1*2=%d", s, p)
}
func calculate(a, b int) (s, p int) {
s := a + b
p := a * b
return
}
Functions
25. func main() {
double := func(a int) int {
return a * 2
}
double(14)
}
Functions as values
26. func main() {
double := factory()
double(14)
}
func factory() func(a int) int {
return func(a int) int {
return a * 2
}
}
Functions as values
27. Arrays
var a [4]int
a[0] = 1
i := a[0]
http://blog.golang.org/go-slices-usage-and-internals
arr := [2]string{"Foo", "Bar"}
arr := […]string{"Foo", "Bar"}
28. Slices
a := []int{1, 2, 3} // [1 2 3]
No size definition
b := append(a, 4, 5) // [1 2 3 4 5]
c := make(c, 3) // [0 0 0]
d := b[1:3] // [2 3]
e := b[:3] // [1 2 3]
46. func ready(w string, sec int) {
time.Sleep(time.Duration(sec) * time.Second)
fmt.Println(w, "is ready!")
}
func main() {
go ready("Tea", 2)
go ready("Coffee", 1)
fmt.Println("I'm waiting")
time.Sleep(5 * time.Second)
}
I’m waiting
Coffee is ready
Tea is ready
47. Channels
ci := make(chan int)
cs := make(chan string)
cf := make(chan interface{})
ci <- 1 ← send value 1 into channel ci
i := <-ci ← read int from channel ci
48. var c chan int
func ready(w string, sec int) {
time.Sleep(time.Duration(sec) * time.Second)
fmt.Println(w, "is ready!”)
с <-‐ 1
}
func main() {
go ready("Tea", 2)
go ready("Coffee", 1)
fmt.Println("I'm waiting")
<-‐c
<-‐c
}
49. var done = make(chan bool)
var msgs = make(chan int)
func produce() {
for i := 0; i < 10; i++ {
msgs <-‐ i
}
done <-‐ true
}
func consume() {
for {
msg := <-‐msgs
println(msg)
}
}
func main() {
go produce()
go consume()
<-‐ done
}
50. var done = make(chan bool)
var msgs = make(chan int)
func produce() {
for i := 0; i < 10; i++ {
msgs <-‐ i
}
done <-‐ true
}
func consume() {
for {
msg := <-‐msgs
println(msg)
}
}
func main() {
go produce()
go consume()
<-‐ done
}
Start go-‐routines
51. var done = make(chan bool)
var msgs = make(chan int)
func produce() {
for i := 0; i < 10; i++ {
msgs <-‐ i
}
done <-‐ true
}
func consume() {
for {
msg := <-‐msgs
println(msg)
}
}
func main() {
go produce()
go consume()
<-‐ done
}
Send values
52. var done = make(chan bool)
var msgs = make(chan int)
func produce() {
for i := 0; i < 10; i++ {
msgs <-‐ i
}
done <-‐ true
}
func consume() {
for {
msg := <-‐msgs
println(msg)
}
}
func main() {
go produce()
go consume()
<-‐ done
}
Receive values and print
53. var done = make(chan bool)
var msgs = make(chan int)
func produce() {
for i := 0; i < 10; i++ {
msgs <-‐ i
}
done <-‐ true
}
func consume() {
for {
msg := <-‐msgs
println(msg)
}
}
func main() {
go produce()
go consume()
<-‐ done
}
Done!
60. What’s missing?
Debugger
IntelliJ IDEA plugin is still in development
Can use GDB, but better do something else instead
Dependency management
A lot of different solutions, no de facto standard
-vendor option in 1.5
62. Testing
package math
func Add(a, b int) int {
return a + b
} package math
import "testing"
func TestAdd(t *testing.T){
if Add(1, 3) != 4 {
t.Error("Expecting 4")
}
}
63. Testing
package math
func Add(a, b int) int {
return a + b
} package math
import "testing"
func TestAdd(t *testing.T){
if Add(1, 3) != 4 {
t.Error("Expecting 4")
}
}
/usr/local/go/bin/go test -‐v ./... -‐run ^TestAdd$
Testing started at 03:35 ...
? _/Users/anton/work-‐src/mygo [no test files]PASS
ok _/Users/anton/work-‐src/mygo/math 0.007s
64. Q: Can you build the enterprise apps in Go?
And now a million $$ question
https://www.youtube.com/watch?v=cFJkLfujOts
Building bank in Go:
A: Apparently, you can :)
65. Can I haz
decimal type?
https://github.com/shopspring/decimal
import "github.com/shopspring/decimal"
…
x, _ := decimal.NewFromString("0.1")
sum := x.Add(x).Add(x) // 0.3
67. Frameworks
Gorilla web toolkit http://www.gorillatoolkit.org/
Go-Kit http://gokit.io
List of various frameworks and libraries:
https://github.com/avelino/awesome-go