Skip to content

Go Language

Go Reflect: Correctly Get an Empty Interface Type

This blog records the tricky part of using reflect to get an empty interface type correctly. The background is that a component requires the configuration type during function call as it might use the type to construct a type and set up the value based on some external data set. If we cannot find a respective in some scenarios, empty interface type should be passed.

However, at the beginning due to the wrong usage of reflect, a nil type instead of interface{} type is passed by reflect.TypeOf(i).

var i interface{}
// this is correct
typ := reflect.TypeOf(&i).Elem()

// this is wrong usage, the typ is nil
typ := reflect.TypeOf(i)

Errcheck Linter Implementation

Recently, I'm going to develop a static tool to analyze our self-defined framework syntax inside our company. Hence, I investigated a lot around the open source linters to find some ideas. This blogs talks about the implementation of errcheck. It's based on AST and relies on the node types to handle the different cases.

Go Context Implementation(2)

In the blog about symbol table, we discuss the context implementation for variables storage and the symbol table due to they are similar. In this blog, I will discuss the remaining parts about the context implementation.

Besides the value storage, context provides the cancel, timeout and deadline features. In this article, we focus on how the sub contexts are notified by the parent one while children context doesn't affect their parents.

Go Tool Pprof

Profile is a common concept among programming languages, it helps to access the program status like CPU, memory usage, and so forth. You may assume that only the languages with runtime could profile, but that's totally wrong. Languages without runtime like C, C++ could profile themselves as well by diverse approaches. For example, the compiler could insert some code instructions for profiling, or the profiler could interrupt the program to record its states periodically.

Half done as I don't have enough knowledge about Go runtime management.