Code learning tools
创始人
2024-02-23 02:10:41
0

这里写目录标题

  • 1. Code learning tools
    • 1.1. Chrome Sourcegraph plugin
    • 1.2. Print statements never go out of style
    • 1.3. When in doubt, PANIC
    • 1.4. Visit the past with GitHub blame

1. Code learning tools

I know what you are thinking. Brad, you are new to Kube and Go and you quickly learned all this. You must be a genius! Well, sadly I have a list of Twitter followers who would be more than happy to jump in with solid concrete evidence that would easily refute that statement. But through the help of others I have identified several tools and techniques that can really help accelerate your ability to learn the Kubernetes source code. In this section I describe my favorite techniques: use of the Chrome Sourcegraph plugin, properly formatted print statements, the use of a Go panic to get desperately needed stack traces, and GitHub blame to travel back in time.

1.1. Chrome Sourcegraph plugin

Morgan Bauer introduced me to one of the coolest tools I have seen for learning Kubernetes code. The Chrome Sourcegraph plugin provides several advanced IDE features that make it dramatically easier to understand Kubernetes Go code when browsing GitHub repositories. Here is an example of how it can help. When I first started looking at Kubernetes code I found the following code snippet absolutely depressing to parse through and understand. It had a ton of functions and it was just overwhelming.
在这里插入图片描述

Screen capture of code section I found bewildering and depressing when new to Go and Kubernetes programming styles

When looking at this same piece of code in a Chrome browser with the Sourcegraph extension installed you can hover the mouse over each function and quickly get a description of the function, what is passed into the function and what it returns. This is a huge time saver as you can avoid having to grep the code base to understand where a function is defined and what it does. An example of this is shown in the figure below.
在这里插入图片描述

Screen capture of Sourcegraph hover view, which makes it obvious that ContinueOnError operates on a Builder object and returns a Builder object and describes what the function does

The Chrome Sourcegraph extension also has an advanced view that provides the ability to peek into the function that is being invoked. This extremely useful capability is shown here.
在这里插入图片描述

Screen capture of Chrome Sourcegraph advanced view that provides the ability to peek into the function that is being invoked

One issue I noticed with Chrome Sourcegraph is that sometimes it hangs and fails to pop up the code details. My experience has been that this is easily fixed by simply hitting the refresh button on the browser.

1.2. Print statements never go out of style

Adding print statements as I have shown throughout this article is a huge help to validating that the code is executing in fashion that matches how you are interpreting it. The %#v formatting option shown below typically provides the best debugging information. Don’t forget that you may have to add "fmt"to your list of imports if it is not already included in the module.

fmt.Println("\n createAndRefresh Info = %#v", info)

1.3. When in doubt, PANIC

I was having a very difficult time determining how the createAndRefresh function in Create.go was invoked. Finally, I decided to throw in a panic into the code to force a stack trace to be generated and printed to the screen. The code below shows how I added a panic to the function. This was a huge help as it helped me to determine which type of Visitor was actually being used to invoke the createAndRefresh function.

func createAndRefresh(info *resource.Info) error {fmt.Println("\n createAndRefresh Info = %#v", info)panic("Want Stack Trace")obj, err := resource.NewHelper(info.Client, info.Mapping).Create(info.Namespace, true, info.Object)if err != nil {return err}info.Refresh(obj, true)return nil
}

1.4. Visit the past with GitHub blame

Sometimes you look at some lines of source code and you think to yourself, what was the person thinking when they committed those lines of code. Thankfully, the GitHub browser interface has a blame option available as a button on the user interface. The figure below shows the location of the blame button.
在这里插入图片描述

Screen capture of blame button on the GitHub browser interface

When you push the blame button, you are given a view of the code that has the commits responsible for each line of code in the source file. This allows you to go back in time and look at the commit that added a particular line of code and determine what the developer was trying to accomplish when that line of code was added. The figure below illustrates the use of the blame option and on the left hand side all the commits are listed.
在这里插入图片描述

相关内容

热门资讯

ST金鸿(000669)披露重... 截至2025年12月23日收盘,ST金鸿(000669)报收于3.02元,较前一交易日下跌1.31%...
三部门印发《关于完善幼儿园收费... 国家发展改革委、教育部、财政部联合印发的《关于完善幼儿园收费政策的通知》23日对外公布,旨在更好促进...
原创 美... 在当前复杂的国际局势中,俄罗斯与西方之间的张力显得尤为突出。最近,美国情报总监图尔西·加巴德明确表示...
山东诸城偷排危废致4死案一名主... 山东潍坊诸城市舜王街道一厂房深夜偷排危废,产生硫化氢等大量有毒气体,导致4人死亡、3人重伤以及32人...
男子称在邯郸一超市购买的猪肉检... 近日,河北省邯郸市丛台区一消费者王先生反映,其在阳光超市龙湖店购买的猪肉,食用时感觉有异样。随后,他...
市场监管总局:今年首次以法律形... 市场监管总局副局长柳军23日在专题发布会上介绍,2025年,全国食品安全形势总体平稳。市场监管总局坚...
关于健全对刑事案件犯罪嫌疑人、... “两高一部”发布《关于健全对刑事案件犯罪嫌疑人、被告人身份审查工作机制的意见》 为准确、及时查明案件...
完善幼儿园收费政策 三部门发通... 中新网12月23日电 据国家发展和改革委员会网站消息,23日,国家发展改革委、教育部、财政部发布关于...
男子4年强奸继女六七十次,被判... 日前,河北省石家庄市栾城区人民法院在中国裁判文书网公开了一份刑事判决书,男子刘冬(化名)在4年间竟六...
合肥一烤肉店回应宠物狗上桌吃饭... 12月22日,安徽合肥一家烤肉店有宠物狗上桌吃饭,餐桌上的餐盘里放有食物,宠物狗在不断啃食生肉。 2...