Tag: golang

How to Check nil Interface in Golang?

Never check an interface for nil. Try the following code: type user interface{} type staff struct{} func compareNil() { var generic user generic = (*staff)(nil) fmt.Printf(“value=%v type=%T (generic==nil)=%v\n”, generic, generic, generic == nil) generic = (nil) fmt.Printf(“value=%v type=%T (generic==nil)=%v\n”, generic, generic, generic == nil) } go playground: https://play.golang.org/p/7J9DeIjgNia Output value=<nil> type=*main.staff (generic==nil)=false value=<nil> type=<nil> (generic==nil)=true…

Why HTTP Status in Response is always StatusOK?

Why the following code works as expected but the next one does not? Response 1 resp.WriteHeader(http.StatusBadRequest) _, _ = resp.Write([]byte(`status code is 400`)) But if we change the order and write the body first and header later: Response 2 _, _ = resp.Write([]byte(`status code is not 400!!`)) resp.WriteHeader(http.StatusBadRequest) The header is set to http.StatusOK in…

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) }…

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…

How to Fix File is not `gofumpt`-ed (gofumpt) error

gofumpt is a stricter version of gofmt. The lint tool finds more errors and reports. One such error is that File is not gofumpt-ed. The solution to the problem is as follows: Please install gofumpt The easiest solution is to run gofumpt on the troubled files. gofumpt -w <fileame> Written with StackEdit.

Time Complexity of a Memoized Algorithm

The memoization eliminated duplicate calls to a function. A memoized implementation of Fibonacci code is as follows: package complexity import “fmt” var mem map[int]int func fibo(count int) int { if count == 0 || count == 1 { return count } if mem[count] != -1 { return mem[count] } s := fibo(count-1) + fibo(count-2) mem[count]…

Summary: How to Write Go Code

Summary: How to Write Go Code The code organization follows repository -> module -> packages. Use go mod init and initialize module root as desired e.g. example.com/user/hello The Go binary goes to the path set in GOBIN All imported modules are stored in GOPATH/pkg/mod subdirectory The command go clean -modcache cleans downloaded packages. Reference https://golang.org/doc/code.html&hellip;

Go, gorm, nested JSON and associations

Go, gorm, nested JSON and associations gorm provides a clean way to store a nested JSON with the relation of associations among tables. The following code creates a DB with three levels of nesting. package main import ( “encoding/json” “fmt” “github.com/jinzhu/gorm” _ “github.com/lib/pq” ) const ( host = “localhost” port = 5432 user = “postgres”…

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…