development

HTTP Request Body for Form Values

HTTP Request Header HTTP clients and servers largely depend on the “Content-Type” header to determine how to parse the request/response body. Common values for the “Content-Type” header are application/json application/atom+xml application/pdf application/octet-stream application/x-www-form-urlencoded and request/response body should be processed as JSON, XML, PDF, Raw Stream, or Form Data respectively. How is Form Data Sent? 99.99% […]

Read more
development

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 […]

Read more
development

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 […]

Read more
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) } […]

Read more
development

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 […]

Read more
development

Redis Expire Key: How TTL behaves?

An operation that modifies a key’s value resets the TTL. DEL, SET, GETSET and all the *STORE An operation that alters the key’s value leaves TTL untouched. Create a list with TTL = 120 redis> rpush “mylist” “first” (integer) 1 redis> rpush “mylist” “second” (integer) 2 redis> expire “mylist” 120 (integer) 1 Add new elements […]

Read more
development

Liveness vs Readiness Health Checks in Application

The liveness check finds if the application is alive or not. The application defines the logic and the liveness check verifies the expected output. A webserver liveness check could include an HTTP handler that returns a fixed response or checks application dependencies. Sample Liveness check send a pong response in the handler combine health check […]

Read more
development

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 […]

Read more
development

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.

Read more