我目前正在阅读https://github.com/codegangsta/inject go 包的源代码,以了解该包的工作原理。
我有一些关于文件https://github.com/codegangsta/inject/blob/master/inject.go文件的问题,该文件使用了 Go 语言的一些元素,我不理解,也没有在文档中找到准确的解释.
// InterfaceOf dereferences a pointer to an Interface type.
// It panics if value is not an pointer to an interface.
func InterfaceOf(value interface{}) reflect.Type {
t := reflect.TypeOf(value)
for t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() != reflect.Interface {
panic("Called inject.InterfaceOf with a value that is not a pointer to an interface. (*MyInterface)(nil)")
}
return t
}
我的第一个问题是关于for循环的。为什么它使用带有测试表达式的 for 循环?
第二个与恐慌函数中的消息有关。“指向接口的指针”在(*MyInterface)(nil). 当您检查类型是否实现了结构时,我只在有关“编译时检查结构”的 go 文档中遇到类似的构造:
var _ SomeType = (*SomeInterface)(nil)
我没有找到有关带有(*Interface)(nil)接口和指针的语句的任何信息。
我们应该如何解释这个说法?与指向接口的指针有什么关系,我在哪里可以找到有关指向接口的指针的信息?
哈士奇WWW
拉风的咖菲猫
相关分类