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

 ALL


  The Go Pointer Magic

Go is a language with the pointer type, by which we canPass pointer into a function and update value in-place.Add methods to a struct as (* T) A, which is different from (T) A().However, the pointer is type-safe in Go, meaning that there are such restrictions of the pointer.Different types of pointers are unconvertible.Pointer type cannot be used for calculation.Pointer types cannot be compared, either == nor !=.No mutual assignment between different pointer-type variables.For example, *int cannot be converted to *int32in the following code.func main() {...

3,916 0       POINTER GOLANG UNSAFE


  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,776 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,899 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...

15,857 0       PROTO BUFFER GOLANG PLUGIN


  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,503 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,498 0       GOLANG NOTIFYCONTEXT GRACEFUL SHUTDOWN


  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,291 0       GOLANG EXECUTABLE MAIN PACKAGE MULTIPLE FILE


  Understanding GoLang interface

If goroutine and channel are considered as the foundation for GoLang concurrency, interface would be the key for data types in GoLang. In practical Go programming, almost all data types are built/used around interfaces, interface is the core of GoLang data structures.Go is not a typical OOP language, it has no class and inheritance concept syntactically. But it doesn't mean that there cannot be polymorphism in GoLang. Because of interface, it achieves the same polymorphism effect as in C++, though syntactically different.Although Go doesn't have concept of class, it allows different data types...

7,443 1       INTERFACE GOLANG