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

 GO


  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,836 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,815 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,361 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,246 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,161 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,485 0       GOLANG UNSAFE ZERO-COPY


  Some tricks and tips for using for range in GoLang

GoLang provides two major ways to loop through elements of array, slice and map. They are for and for range. Many people find that for range is very convenient when don't care about the index of the element. In this post, some tricks and tips would be talked about regarding for range.1. Loop and get pointer of each elementAssume there is a code snippet like below which is to get the pointer of each element in an array and create a new array with the corresponding pointer.arr := [2]int{1, 2}res := []*int{}for _, v := range arr { res = append(res, &v)}//expect: 1 2fmt.Println(*res[0],*res...

29,108 0       FOR RANGE GOLANG FOR LOOP POINTER


  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,829 0       GOLANG CONTEXT