Tag: development

How to Add Multiple Directories in GOPATH with VSCode?

Since go versions keep moving up, maintaining GOPATH inside a Go installation path is a pain. export GOPATH=/home/a-user/go/1.14.9 ls /home/a-user/go/1.14.0/src bitbucket.org github.com go.uber.org golang.org gopkg.in Adding multiple directories in GOPATH Create a directory e.g. for work mkdir $HOME/mywork GOPATH assumes a structure in the directory, so create three directories inside. ls $HOME/mywork bin pkg src You will do all your…

VSCode Plugins for Golang Development

Rich Go language support for Visual Studio Code Theme: Dark Knight: A simple dark theme for Golang projects Git Blame & Git History Postfix templates for Golang Adds go to implementation on context menu All-language autocompleter — TabNine uses machine learning to help you write code faster. This is a minimal list of Go development extensions in my opinion.

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…

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:…

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

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…