This talk will start with an overview of Go, then dive into some examples of using it to work with Postgres. We'll show basics like running queries, then demonstrate how Go makes difficult things easy, such as inspecting Postgres's TCP wire protocol and providing retry mechanisms and monitoring for restores (including fun stories, tips and tricks).
Join us and see why Go - with it's powerful concurrency primitives and unbeatable performance - can be one of the most powerful tools in your toolbox.
2. Who am I?
Senior Engineer @ Procore
Work on availability, performance, and (now mostly)
security
Been writing Go code actively for 2 years
Twitter:
Email: aj.bahnken@procore.com
@ajvbahnken
9. "Less is exponentially more" by Rob Pike
Go is purposefully lacking certain features.
Link: https://commandcenter.blogspot.com/2012/06/less-is-
exponentially-more.html
16. Concurrency is a breeze
package main
import (
"fmt"
"time"
)
func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
go say("world")
say("hello") // Allows for the goroutine to run, by blocking.
}
21. Testing is simple and built in + race detector
$ ls
processing.go processing_test.go utils.go
$ go test
PASS
ok ~/pgnetdetective/processing 0.165s
$ go test race
PASS
ok ~/pgnetdetective/processing 2.133s
22. Error handling instead of exceptions
func MyFunc() (string, error) {
str, err := run()
if err != nil {
return "", err
}
return str, nil
}
func MustMyFunc() string {
str, err := run()
if err != nil {
panic("run() returned an err: "+err.String())
}
return str
}
47. So I got some output like this:
******* Query *******
Query: SELECT attr.attname FROM pg_attribute attr
INNER JOIN pg_constraint cons ON attr.attrelid = cons.conrelid
AND attr.attnum = any(cons.conkey) WHERE cons.contype = p
AND cons.conrelid = "drawing_log_imports"::regclass
TotalNetBytes: 170 MB
TotalResponsePackets: 64041
TotalQueryPackets: 63