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

 GO


  Can two new objects point to the same memory address in GoLang?

Do you have any idea what the output will be for below GoLang snippet?package mainimport ( "fmt")type obj struct{}func main() { a := &obj{} fmt.Printf("%p\n", a) c := &obj{} fmt.Printf("%p\n", c) fmt.Println(a == c)}Many people would think that a and c are two different object instances which have different memory addresses. Hence a == c will be false. But if you try to run the above program, you would see below output0x5781c80x5781c8trueTo get to know the reason why the comparison returns true, some understanding of how Go allocates memory and how variable memory escape happens are ne...

12,301 0       ZEROBASE VARIABLE ESCAPE GOLANG GO


  Mock Solutions for GoLang Unit Test

In Go development, Unit Test is inevitable. And it is essential to use Mock when writing Unit Tests.Mock can help test isolate the business logic it depends on, enabling it to compile, link, and run independently.Mock needs Stub. Stub function replaces the real business logic function, returns the required result, and assists the test.I involved the related test code for Controllers while writing Kubernetes Operator recently, and there would be mocks for GRPC and HTTP requests. I did it in an old fashion way, but I believe there is a better and more graceful way to han...

12,261 0       UNIT TEST GOMOCK GOSTUB TESTIFY


  Advantages and disadvantages of GoLang

GoLang is a strong typed language which means it is less flexible than interpreted languages by nature. But Go provides Any type(interface) and Reflect mechanism which make the language very close to interpreted languages on flexibility. More and more people start to learn GoLang.This post is mainly for listing down some of the advantages and disadvantages of GoLang.AdvantagesPerformance(Machine code)GoLang is a compilation language which can be compiled to machine code and the compiled binary can be directly deployed to target machine without extra dependency. The performance is better than t...

12,066 2       ADVANTAGE DISADVANTAGE GOLANG GOROUTINE GENERICS


  A mini post on GoLang context

In a GoLang web server, every request coming in will be handled by a goroutine. In the request handler, the logic may also need to create new goroutine to handle other tasks like RPC call. When the request is processed and response is returned, these goroutines created need to be exited so that no goroutine leak should happen.package mainimport ( "fmt" "log" "net/http")func main() { http.HandleFunc("/echo", func(w http.ResponseWriter, r *http.Request) { fmt.Println(&r) w.Write([]byte("hello")) }) log.Fatal(http.ListenAndServe(":8080", nil))}go run main.go0xc0000060100xc0000760480xc000076...

11,847 0       GOLANG CONTEXT


  GoLang to build smaller executable file

Normally the executable file built with go is a bit large, it is always desired that a smaller executable file should be generated though. In this post, a couple of ways to reduce the size of the executable will be introduced. The end effect is that the executable file size would be much less than the normal generated one.The file which is built normally has below size.Mode LastWriteTime Length Name---- ------------- ------ -----a---- 12/14/2019 9:47 AM 1974272 main-ori.exeAdd build flags Two ld parameters can be added when usin...

10,669 2       GOLANG EXECUTABLE


  One good way to use optional parameter in function

In GoLang, it doesn't support method overloading like in Java, hence sometimes it would be a headache to create functions to construct new structs with different parameters. Normally, we would construct the new function as below when want to let others create a new struct instance with explicit function call.type Queue struct { Name string}func NewQueue(name string) *Queue { return &Queue{name}}But with the scope and complexity of the struct increases, there might be more properties added to Queue.type Queue struct { Name string MaxLimit int}How to enhance the NewQueue() to accomo...

9,854 0       OPTIONAL PARAMETER VARIADIC FUNCTION OPTION PATTERN


  combining dwarf failed: unknown load command 0x80000034 solution in GoLang

In case there is below error encountered while running some go program:combining dwarf failed: unknown load command 0x80000034This may be due to that you are running go 1.16 on a Mac M! machine. If you go to check go version, it may tell you that you are running an arm version of go.go version go1.16.10 darwin/arm64In such cases, you can try below methods to workaround the issue.Try to upgrade the go version to 1.17 and run the code again. If this is not an option for you because you have to stick to go 1.16 for some reason, you may try to install an amd version of go 1.16. This should work pe...

8,189 1       GO 1.16 0X80000034


  Maintain multiple versions of Go in one single environment

In a development environment, there might be multiple projects going on at the same time and they may require different development environments with different versions of build tool. In many programming languages, it is possible to have multiple versions of different build tool or development tool on a single environment. For example, there can be multiple JDKs, multiple versions of Ruby using RVM. For GoLang, there is a similar tool called GVM which can also be used to maintain multiple versions of Go in one single environment.In this post, we provide a mini guide on how to setup multiple ve...

8,123 0       GOLANG GVM GVM PKGSET RVM