New language from Google, static safe compiler, with GC and as fast as C++ or Java, syntax simpler then Python - 2 hour-long tutorial and you can start code.
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 maintanable services.
Agenda of the presentation:
1. Go is not C, not Java, not anything
2. Rob Pike argument
3. Main ideas and basics
4. Concurrency model
5. Tools
6. Issues
2. About me
Serhii Pichkurov
◎5+ years of experience
◎Java
◎Mostly backend
◎No experience as presenter
before
◎9 months with Go
3. Agenda
❑ Go is not C, not Java, not anything
❑ Gopher
❑ Rob Pike argument
❑ Main ideas
❑ Concurrency model
❑ Tools
❑ Issues and gotchas
4. Characteristics
Go (often referred to as golang) is:
◎Clean procedural language designed
for scalable cloud software
◎open source
◎statically typed with duck typing
7. Motivated by Google’s needs:
● Efficiency
● Safety
● Concurrency
● Scalability
● Fast development cycle
● No surprises
● A cute mascot
Why new
lang?
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
17. 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
19. Go elements
● concrete data types
● functions and methods
● interfaces
● structs
● packages
● concurrency
● Plus: Good tools, fast builds.
● All the pieces feel simple in practice.
21. Arrays
An array's length is part of its type, so arrays
cannot be resized.
var arr [5]int
arr[4] = 42
22. Slices
A slice, on the other hand, is a dynamically-sized,
flexible view(reference) into the elements of an array
bytes := [5]byte{} // array
var slice = bytes[2:4]
names := []string{"leto", "paul", "teg"}
c := make([]string, len(names))
copy(c, names)
names = append(names,"ioav")
24. Maps
m = make(map[string]int) // declare
m["route"] = 66 // put
i := m["route"] // get
delete(m, "route")
_, ok := m["route"] //check is present
for key, value := range m { //iterate
fmt.Println("Key:", key, "Value:", value)
}
commits := map[string]int{ //static population
"gri": 1908,
"adg": 912,
}
26. 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)
28. 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
29. Tags aka annotations
type Person struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
MiddleName string
`json:"middle_name,omitempty"`
}
31. 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"
32. 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
33.
34. Concurrency
Based on CSP that was first described in a 1978
paper by Tony Hoare.
Has three elements:
● coroutines (execution)
● channels (communication)
● select (coordination)
36. 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"
37. 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
39. 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)
}
42. Go and Java have much
in common
◎ C family (imperative, braces)
◎ Statically typed
◎ Garbage collected
◎ Memory safe (nil references,
runtime bounds checks)
43. Go and Java have much
in common
◎ Variables are always initialized
(zero/nil/false)
◎ Methods
◎ Interfaces
◎ Type assertions (instanceof)
◎ Reflection
44. Go differs from Java in
several ways
◎ Programs compile to machine code. There's no VM.
◎ Statically linked binaries
◎ Control over memory layout
◎ Function values and lexical closures
◎ GC does not have compaction phase
◎ Multiply return
45. Go intentionally leaves
out many features
◎ No classes
◎ No constructors
◎ No inheritance
◎ No ‘implements’ keyword
◎ No final
◎ No exceptions
◎ No user-defined generics
46. Exceptions go away
◎No Exceptions by design
◎Handling with multiple return
◎Panic!
type error interface {
Error() string
}
47. 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("Failed to read RPM head: %v",
err)
return
}
50. 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/
...
51. 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
52. Go tools support
◎go fmt
◎goimports
◎go lint or gometalinter
◎godoc lookups
◎go generate
◎many more
◎easy to write your own
53. IDE
There's no "Go IDE". Go tools meet you where
you are.
◎Eclipse
◎IntelliJ
◎Atom
◎Emacs
◎Vim
◎many others
54. 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