golang: How to Add Better Context to Errors

golang: How to Add Better Context to Errors There are at least three ways to add more context to errors. fmt.Errorf errors package zap library package main import ( “errors” “fmt” werr “github.com/pkg/errors” “go.uber.org/zap” ) // 1. wraps with a message func wrap(e error) error { return fmt.Errorf(“another layer – %w”, e) } // 2. wraps with callstack and message…

Get a Free Public Web Address

Get a Free Public Web Address What is ngrok? Ngrok exposes local servers behind NATs and firewalls to the public internet over secure tunnels. Run personal cloud services from your home Demo websites without deploying Build webhook consumers on your dev machine Test mobile apps connected to your locally running backend Stable addresses for your connected devices that are deployed…

How to Check nil Interface in Golang?

It’s not a good idea to check an interface for nil. Try the following code: type user interface{} type staff struct{} func compareNil() { var generic user generic = nil // works as expected fmt.Printf(“value=%v type=%T (generic==nil)=%v\n”, generic, generic, generic == nil) generic = (*staff)(nil) // fails my expectation fmt.Printf(“value=%v type=%T (generic==nil)=%v\n”, generic, generic, generic == nil) } go playground:…

Google Auth App Not Working: Quits on Launch on iOS

Google Auth App Not Working: Quits on Launch on iOS After upgrading iOS, the Auth app started exiting on launch. Solution Go to Settings->General->Storage Now find Google Auth App in the list Offload App. It retains all your data. Install again. It could work now!

json: cannot unmarshal object into Go struct field

Problem json: cannot unmarshal object into Go struct field Code package main import ( “encoding/json” “fmt” ) // DTO object type mould struct { Particle tiny } type tiny interface { GetName() string } // Implement the interface type speck struct { Name string `json:”name”` } func (s *speck) GetName() string { return s.Name } // Constructor func NewSpeck(name string)…

Quick Read on UTF-8 in Golang

Raw Strings we create a “raw string”, enclosed by back quotes, so it can contain only literal text. Regular strings, enclosed by double quotes, can contain escape sequences as we showed above. package main import ( “fmt” ) func main() { fmt.Println(`go\\n`) fmt.Println(“escapedGo\\n”) } Output go\\n escapedGo\n Raw string is always UTF-8 because it is part of the Go source…

Benefits of Monorepo

#build #monorepo Why Mono-repo Using a monorepo where HEAD always points to a consistent and valid version removes the problem of tracking multiple repo versions entirely Static analysis can run across project boundaries without any extra work. Many other things, like cross-project integration testing and code search are also greatly simplified. You just refactor the API and all of its…

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% of forms in your web…