如何通过静态分析判断变量的类型?
假设我有以下代码:
func doSomething(x interface{}) {}
func main() {
p := Person()
doSomething(p)
}
而我想分析一下doSomething(person),是否可以通过静态分析得到Person的类型?
如果有多个级别的分配怎么办?
p1 := Person()
p2 := p1
doSomething(p2)
要么
parent := Parent()
p := Parent.Child() // type Person
doSomething(p)
用例是我有一个在整个(非常大的)代码库中常用的通用函数,并且想引入该函数的新类型安全版本。为此,我希望自动确定函数的“类型”并相应地重构它:
// old
DB.InsertRow(person)
// new
Person.InsertRow(person)
尚方宝剑之说
HUX布斯
相关分类