Tag: development

Golang: Careful with Named Return Parameters in Function

package main import ( “fmt” ) func riskyParams() (num int, flag bool) { if num == 0 { // Named parameters have default values in the scope flag = true } return // Beware of such return. You might return something unintended } func main() { num, flag := riskyParams() fmt.Printf(“num=%d flag=%v\n”, num, flag) } The example highlights unintended side-effects…

The Best Git Commands for Dev

The most common part is: The basic cycle git pull master to get up to date git checkout -b myname-feature to get a branch of your own git add <files> that you changed/created git add . to add everything in the current directory git add -u to add all existing files in the repo git commit git commit -m “multiline…

Why be careful with defer in Golang

When does defer function executes? The standard answer is: defer runs a function before the enclosing function returns. It is a wrong explanation. The defer executes as follows: The enclosing function has processed returned values and stored them in registers All defer functions run in a LIFO order of definition. If the return parameters are named, the defer function can…

Interface Quirks in Golang

An interface defines a set of methods. A struct type implements these methods and qualifies the object type as an interface type. However, how the struct implements the interface? There are two ways: As a pointer handler As a value handler Implementation with a struct value handler package main import ( “fmt” ) type Dummy interface { Add(int, int)int }…

go-kit: Sample Code for Function to Log

go-kit: Sample Code for Function to Log func getLogger() log.Logger { var logger log.Logger logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr)) logger = log.With(logger, “instance_id”, 123) return logger } The above function is easily usable in a go-kit project to log JSON style data. func myfun() { mylogger := getLogger() mylogger.Log(“a key”, “a value”) } Reference https://github.com/go-kit/kit/tree/master/log Written with StackEdit.

etcd: A Reliable Distributed KV Store

Pronounced et-cee-dee Persistent KV store Uses Raft protocol to maintain data consistency Developed in Go Popular Use case: Kubernetes, Consul Installation (OSX) Step 1 $ ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)” < /dev/null 2> /dev/null Step 2 $ brew install etcd Step 3 $ which etcd /usr/local/bin/etcd Start Server and Make Requests Step 4 $ /usr/local/bin/etcd [WARNING] Deprecated ‘–logger=capnslog’ flag is…