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

 ALL


  Build a Kubectl Plugin from Scratch

by authorThe command-line tool kubectl is indispensable when using Kubernetes. You need it to query related Pod and Service information both in developing or performing some maintenance operations, such as events, scale, rolling update, etc.However, when using kubectl, there are many inconveniences. Though Kubernetes is officially maintaining kubectl¹, and you can submit the problems, improvement, and even PR in its Github Issues, you still have to wait long before its release.The most common solution is Kubectl-Plugin Pattern²Plugins extend kubectl with new sub-commands, allowing fo...

4,195 1       GOLANG PLUGIN KUBECTL KUBENETES


  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,850 0       LEN() SHIFT OPERATION CONSTANT GOLANG


  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,300 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,191 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,502 0       ZERO-COPY UNSAFE GOLANG


  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,140 0       POINTER FOR LOOP GOLANG FOR RANGE


  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,835 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,646 2       EXECUTABLE GOLANG