Tag: development

What is SemVer or Semantic Versioning?

In the Linux world, kernel releases follow MAJOR.MINOR.PATCH version format. Semver is a similar concept. It advises to follow a simple set of rules: Major version is changed for incompatible changes. Minor versions are changed for new functionality Patch version is changed after bug fixes. Versions are always incremented and reset to zero: Patch version is set to zero if…

golang: NewReader vs NewBufferString

NewReader func bytes.NewReader(b []byte) *bytes.Reader bytes.NewReader on pkg.go.dev NewReader returns a new Reader reading from b. NewBufferString func bytes.NewBufferString(s string) *bytes.Buffer bytes.NewBufferString on pkg.go.dev NewBufferString creates and initializes a new Buffer using string s as its initial contents. It is intended to prepare a buffer to read an existing string. Difference // A Reader implements the io.Reader, io.ReaderAt, io.WriterTo, io.Seeker,…

go: cannot find main module, but found .git config to create a module there, run: go mod

go: cannot find main module, but found .git/config to create a module there, run: go mod This problem occurred in a monorepo opened in VSCode. The repo has many services placed in isolated directories with respective go.mod files. I realized from various sources that the problem is from two places: Move the mono repo under $GOPATH/src. The ~/.zshrc or ~/.bashrc…

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