Go: Why it goes
by Serhii Pichkurov
In this talk Serhii will talk about Go, also known as Golang – an open source language developed at Google and used in production by companies such as Docker, Dropbox, Facebook and Google itself. Go is now heavily used as a general-purpose programming language that’s a pleasure to use and maintain. This introductory talk contains many live demos of basic language concepts, concurrency model, simple HTTP-based endpoint implementation and, of course, tests using build-in framework. This presentation will be interesting for backend engineers and DevOps to understand why Go had become so popular and how it might help to build robust and maintainable services.
Join this session after which you can start coding using language that has static safe compiler, GC and is as fast as C++ or Java, with even simpler syntax than Python!
3. Agenda
● Go is not C, not Java, not anything
● Gopher
● Rob Pike argument
● Main ideas
● Concurrency model
● Tools
● Issues and gotchas 3
4. Characteristics
Go (often referred to as golang) is:
4
●Clean procedural language designed
for scalable cloud software
●open source
●statically typed with duck typing
7. Motivated by Google’s needs:
Why new lang?
7
● No surprises
● Efficiency
● Safety
● Concurrency
● Scalability
● Fast development cycle
● A cute mascot
8. Who uses Go at Google?
Lots of projects. Thousands of Go programmers. Millions
of lines of Go code.
Public examples:
● Kubernetes
● SPDY proxy for Chrome on mobile devices
● dl.google.com is written in Go
● YouTube Vitess MySQL balancer
8
14. Rob Pike argument
● Go is different
● Go does not try to be like the other
languages
● Go does not compete on features
● As of Go 1, the language is fixed
14
18. Go elements
● concrete data types
● functions and methods
● interfaces
● structs
● packages
● concurrency
● good tools, fast builds
All the pieces feel simple in practice
18
27. Maps
m = make(map[string]int) // declare
m["route"] = 66 // put
i := m["route"] // get
delete(m, "route")
_, ok := m["route"] //check is present
27
29. for key, value := range m { //iterate
fmt.Println("Key:", key, "Value:", value)
}
commits := map[string]int{ //static population
"gri": 1908,
"adg": 912,
}
29
Maps
30. Interfaces
Just a set of methods. No data. Simple idea, but more
complex than expected.
type Reader interface {
Read(p []byte) (n int, err error)
}
_, err := reader.Read(p)
30
31. Structs
type Person struct {
FirstName string
LastName string
}
person := Person{FirstName:"title", LastName:"http:..."}
31
33. Pointers
A pointer holds the memory address of a variable.
The & operator generates a pointer to its operand:
i := 42
p = &i
The * operator denotes the pointer's underlying value:
fmt.Println(*p) // read i through the pointer p
*p = 21 // set i through the pointer p
33
36. Packages
We can import one or more package, there are three types:
system, local and remote.
System – out of the box. Like http, testing, etc
Local – in your repository, no need to use full path.
Remote – has complete import path pointing to the server.
To import remote package:
import "github.com/nf/wav"
36
37. Getting dependencies
For remote package use go get to fetch into workspace.
go get github.com/nf/wav
Other operations also done via go CLI tool:
go build
go test
go run
and more
37
39. Concurrency
Based on CSP that was first described in a 1978 paper by
Tony Hoare.
Has three elements:
● coroutines (execution)
● channels (communication)
● select (coordination)
39
41. Goroutines
Start a goroutine with the go keyword:
go function(args)
Like garbage collection, eliminate considerations from the
programmer's concern:
● no stack size
● no return or completion status
● no mechanism for management
● no "ID"
41
42. Goroutines
●Goroutines are like lightweight threads.
●Go programs can have hundreds of
thousands of them
●The Go runtime schedules goroutines
onto OS threads
●Blocked goroutines don't use a thread
42
43. Channels
Channels provide communication between
goroutines.
c := make(chan string)
// goroutine 1
c <- "hello!"
// goroutine 2
s := <-c
fmt.Println(s) // "hello!"
43
44. Select
A select statement blocks until one of its cases can run, then it
executes that case.
msg := "hi"
select {
case n := <-in:
fmt.Println("received", n)
case out <- msg:
fmt.Println("sent", msg)
}
44
47. Go and Java have much in common
● C family (imperative, braces)
● Statically typed
● Garbage collected
● Memory safe (nil references,
runtime bounds checks)
47
48. ● Variables are always initialized
(zero/nil/false)
● Methods
● Interfaces
● Type assertions (instanceof)
● Reflection
48
Go and Java have much in common
49. Go differs from Java in several ways
● Programs compile to machine code. There's no VM.
● Statically linked binaries
● Multiple return
● Error handling
● Bit different GC(no compaction)
49
50. Go intentionally leaves out many features
● No classes
● No ‘implements’ keyword
● No final
● No exceptions
● No user-defined generics
50
51. Exceptions go away
● no exceptions by design
● handling with multiple return
● panic!
type error interface {
Error() string
}
51
52. Errors design
rpm, err := rpm.OpenPackageFile(localPath)
if err != nil {
log.Error("Failed to read RPM headers: %v", err)
return
}
file, err = file.ReadAt(archiveHead, int64(headerEnd))
if err != nil {
log.Error("Invalid archive head head: %v", err)
return
} 52
55. Vendoring
server-one uses the mux package in $GOPATH/src/github.com/gorilla/mux.
server-two uses the mux package in vendor.
$GOPATH
src/
server-one/
main.go (import "github.com/gorilla/mux")
server-two/
main.go (import "github.com/gorilla/mux")
vendor/
github.com/
gorilla/
mux/
...
55
56. Glide for Package Management
● Semantic Versions and Ranges
● Git, Bzr, Hg, Svn
● Works with Go toolchain
● Leverages vendor directory
● Imports from Godep, GB, GPM, Gom
● Private Repos and Forks
56
57. Go tools support
●go fmt
●goimports
●go lint or gometalinter
●godoc lookups
●go generate
●many more
●easy to write your own
57
58. IDE
There's no "Go IDE". Go tools meet you where you are.
●Eclipse
●JetBrains(GoLand)
●Atom
●Emacs
●Vim
●many others
58
59. My experience
Project that searches for vulnerabilities inside artifacts
●microservices
●RabbitMQ
●MongoDB/PostgreSQL
●REST API
●work with files and archives
●Docker
●34860 lines of code
59
68. “
I’m impressed about how easy
people can pick it up and start
being productive in a project ,
it’s also very easy to deploy
David Cuadrado, Authy
68