T i l
Postgres streaming replication protocol
I have know about the postgres wire protocol, but first I ran into streaming replication protocol.
I was looking at this code in stolon
replConnParams["replication"] = "1" db, err := sql.Open("postgres", replConnParams.ConnString()) if err != nil { return nil, err } defer db.Close() rows, err := query(ctx, db, "IDENTIFY_SYSTEM") if err != nil { return nil, err } I was wondering what is this IDENTIFY_SYSTEM. A G-search pointed me to this
T i l
Named break statement in Go
I came across this piece of code from The Go Programming Language book.
loop: for { select { case size, ok := <-fileSizes: if !ok { break loop // fileSizes was closed } nfiles++ nbytes += size case <-tick: printDiskUsage(nfiles, nbytes) } } The complete program is available in Github
Named break statement allows to break perhaps multiple control statements. In this example, break loop breaks the select and for statement.
T i l
capturing iteration variables in Go
I was going through The Go Programming Language and in chapter 5 there was a section Caveat: Capturing Iteration Variables.
The example is like this:
var rmdirs []func() for _, dir := range tempDirs() { // 1 os.MkdirAll(dir, 0755) // 2 rmdirs = append(rmdirs, func() { os.RemoveAll(dir) // NOTE: incorrect! // 3 }) } dir is the same variable. i.e. the address of the variable is same dir’s value evaluated at this point is different at each iteration IMPORTANT dir is not evaluated here.
T i l
all goroutines are asleep - deadlock!
program1
package main import ( "log" ) func main() { done := make(chan struct{}) go func() { log.Println("done") }() <-done //wait for background goroutine to finish } program2 run nc -l 8000 on another terminal
package main import ( "log" "net" ) func main() { conn, err := net.Dial("tcp", "localhost:8000") if err != nil { log.Fatal(err) } conn.Close() done := make(chan struct{}) go func() { log.Println("done") }() <-done //wait for background goroutine to finish } Both of those examples have a receive statement from a channel that no one ever sends to.
T i l
Embedded field in Go
Initializing embedded field: The field name of an anonymous field during a struct instantiation is the name of the struct (Time) type Event struct { ID int time.Time } event := Event{ ID: 1234, Time: time.Now(), } If an embedded field type implements an interface, the struct containing the embedded field will also implement this interface
T i l
Understanding shallow copy vs deep copy in Go
In Go, copying generally does a copy by value. So modifying the destination, doesn’t modify the source. However, that holds good only if you are copying value types not reference types (i.e. pointer, slice, map etc.)
package main import ( "fmt" ) func main() { type Cat struct { age int name string Friends []string //reference type } wilson := Cat{7, "Wilson", []string{"Tom", "Tabata", "Willie"}} // shallow copy nikita := wilson // modifies both nikita and wilson nikita.