Go - defer
The defer statement is used to postpone a function call executed immediately before the surrounding function returns. The common uses of defer include releasing resources (i.e., unlock the mutex, close file handle.), do some tracing(i.e., record the running time of function), etc. E.g., an ordinary accessing global variable exclusively is like this:
var mu sync.Mutex var m = make(map[string]int) func lookup(key string) int { mu.Lock() v := m[key] mu.Unlock() return v } An equivalent but concise format using defer is as follow:
[Read More]