Today's Question:  What does your personal desk look like?        GIVE A SHOUT

 GO


  Will nil == nil be true in GoLang

There is some interview question may ask whether nil == nil be true in GoLang. Wil it be true. false or compilation error? To know the answer, some knowledge about nil in GoLang needs to be explained first.nil definitionAccording to Go's official documentation, the definition of nil is// nil is a predeclared identifier representing the zero value for a// pointer, channel, func, interface, map, or slice type.var nil Type // Type must be a pointer, channel, func, interface, map, or slice typenil is the zero value of types like pointer, channel, func, interface, map or slice. Itself is NOT a GoLa...

5,890 2       GOLANG NIL NIL EXPLANATION


  Time to think about supporting max/min functions for integers in GoLang

Sometime back we wrote a post explaining why there is no max/min function support for integers in GoLang, one of the reasons is that no overloading is supported and the function name has been used for other numeric types float64. To solve this problem and make max/min function support integer as well, either GoLang should support overloading like in Java or the same set of functions need to be created in a different package other than standard math. These don't look like good options as overloading is more a OOP concept and supporting the same set of functions in a different package doesn't so...

4,933 0       GOLANG GENERICS MAX MIN


  Fix --go_out: protoc-gen-go: plugins are not supported

When generating RPC code using proto file template in GoLang, one may face the issue like below when running the command protoc.Error: exit status 1Output: --go_out: protoc-gen-go: plugins are not supported; use 'protoc --go-grpc_out=...' to generate gRPCSee https://grpc.io/docs/languages/go/quickstart/#regenerate-grpc-code for more information.Normally this issue is caused because the version of protoc-gen-go being used is not correct. One way to fix the issue is to follow the instructions provided by Google to run a new command.Another way to fix this is by having the Github version of proto...

16,025 0       PLUGIN GOLANG PROTO BUFFER


  Top 10 Go Coding Traps and Tips

Go is currently the most common programming language in cloud development. Though I use it very much in my work, I am still repeating certain mistakes. This article is more a record of these errors, figuring out the causes and solutions so that people who read this article will save themselves time when coming across the same problems.Let’s cut through to the tips.Don’t rely on index var in the for loopThe most common mistake we make is that we often create goroutine inside a for loop and rely on the index variable. For example,package mainimport ( "fmt" "sync")fun...

1,518 0       TIPS GOLANG NIL INTERFACE


  New function signal.NotifyContext in GoLang 1.16

os/signal package in GoLang may not be frequently used but it provides some good features like Shutdown() which can be used to gracefully shutdown a running HTTP server.func (srv *Server) Shutdown(ctx context.Context) errorWith this function, there is no need to use third party library to gracefully shutdown HTTP server. How is it being used?package mainimport ( "context" "fmt" "net/http" "os" "os/signal" "time")func main() { server := http.Server{ Addr: ":8080", } http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Second * 10) fmt.Fprint(w, "Hello world!") ...

6,542 0       GRACEFUL SHUTDOWN NOTIFYCONTEXT GOLANG


  Install multiple versions of Go

As a developer, there might be need in some cases to have multiple versions of Go installed. One may test out some new features of the latest version of Go but also wanna maintain a stable version of Go for daily development purpose. How to maintain multiple versions of Go?The GoLang official website has provided a way to maintain multiple versions of Go by using go get to install the different versions of Go.Below are steps:Download a normal version of Go and install itGo to the Go release page to find out which version of go to installRun the go get command to download the version and instal...

5,254 0       GO VERSION GO BINARY MULTIPLE VERSION


  Different ways of handling concurrent job in GoLang

GoLang provides very good support of concurrency and the way to make some code to run concurrent is pretty simple and straightforward. Adding a simple go before a normal function call will make the function call asynchronous.In real cases normally people would concurrently run some jobs to improve the speed and efficiency. One important part of running jobs concurrently is about aggregating results so that the consequent function call would be able to proceed. There are multiple ways handling this scenarios.One of them is using WaitGroup from sync package. The pattern is like:var wg sync.WaitG...

2,567 0       COMPARISON WORKER POOL WAITGROUP CONCURRENT


  Run code with multiple files in the same main package in GoLang

To run a GoLang program, there needs to be a main() function defined. In some cases when developing some demo program which has multiple files and just wanna put them in the same main package and this folder is not in GOPATH, how to run the program?Let's say we have following folder structure where the main() function is defined in main.go.If you just run below command, it would fail to start to run the program and gives some error if some struct is defined in other files and being used.PS D:\Project\Go\sourcecode_updater\v2> go run main.go# command-line-arguments.\main.go:35:11: undefined:...

20,400 0       GOLANG EXECUTABLE MAIN PACKAGE MULTIPLE FILE