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

 GO


  The hidden risk of passing slice as function parameter

In Go's source code or other open source libraries, there are lots of cases where a slice pointer is passed to function instead of slice itself. This brings up a doubt why not passing slice directly as its internal is backed by an array pointer to point to underlying data?For example, in log package, the formatHeader function takes a parameter buf as type *[]byte instead of []byte.func (l *Logger) formatHeader(buf *[]byte, t time.Time, file string, line int) {}Let's understand the rationale behind this starting with an example.func modifySlice(innerSlice []string) { innerSlice[0] ...

7,995 2       GOLANG SLICE SLICE POINTER


  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,268 0       UNIT TEST GOMOCK GOSTUB TESTIFY


  Understand more about Go basics with one interview question

First, let's take a look at below Go interview question:package mainconst s = "Go101.org"// len(s) == 9// 1 << 9 == 512// 512 / 128 == 4var a byte = 1 << len(s) / 128var b byte = 1 << len(s[:]) / 128func main() { println(a, b)}What would be the output in your mind? The output would be 4 0. Surprising?Before getting to the output values, some concepts in Go need to be introduced and explained in more detail.len() len() is a built-in function in Go to get the length of array, slice or string. There is one important statement about len() in Go specification.For some argume...

3,872 0       LEN() SHIFT OPERATION CONSTANT GOLANG


  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,858 0       OPTIONAL PARAMETER VARIADIC FUNCTION OPTION PATTERN


  Implement struct no copy in GoLang

There is some case where some of the struct in GoLang which is not meant to be copied. For example, sometimes a global configuration which should have only one version passed around the whole application and should not be copied and modified.In GoLang, there is no intuitive solution on preventing copying of struct. But there is still some way which can be leveraged to help prevent this while developing the code. The trick is to define some struct implementing sync.Locker interface and has this struct embedded in any struct not meant to be copied.// noCopy may be embedded into structs which mus...

19,467 0       GO VET NOCOPY NO COPY


  A simple example on implementing progress bar in GoLang

Sometimes when handling a long running process, there would be need to track the progress so that people know something is still running instead of doubting something goes wrong. In this case, a progress bar which indicates the current status and progress would be desired.This post will show an example on how to implement progress bar using GoLang. Let's take a look at the final outcome first before jumping into the implementation detail.The key in the implementation is actually just the \r control flag for fmt.Printf(). \r is actually the carriage return control which tells the cursor to move...

21,399 5       TUTORIAL EXAMPLE GOLANG PROGRESS BAR


  How to check whether a struct implements an interface in GoLang

Unlike other programming languages like Java, when implementing an interface in GoLang, there is no syntax like below to explicit tell the relationship:type I interface {}type A struct implements I {}When looking at the code or running the code in GoLang, it might lead to a case one doesn't know whether a struct implements interface before trying to use it as some interface.According to GoLang spec, as long as a struct implements all functions of an interface, it is considered as having implemented that interface and can be used wherever that interface is expected.To check whether a struct imp...

19,258 0       GOLANG INTERFACE IMPLEMENTATION CHECK


  Understand unsafe in GoLang

Before going to understand unsafe package in GoLang, the first thing needs to talk about is the pointer in GoLang. If you have a background of C language, you must know what pointer means and its usage. With pointer, you are free to operate any data at memory level which means you have great power, but this means that you have great responsibility as well. That's why it might be considered unsafe in lots of cases.Take a look at a simple example of doubling an integer.package mainimport "fmt"func double(x int) { x += x}func main() { var a = 3 double(a) fmt.Println(a) // 3}The above ...

17,545 0       ZERO-COPY UNSAFE GOLANG