关于 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循环?
第二个问题与panic函数中的消息有关。其中提到了一个接口指针(*MyInterface)(nil)
。我只在go文档中遇到过类似的结构,用于检查类型是否实现一个结构体。
var _ SomeType = (*SomeInterface)(nil)
我没有找到关于
(*Interface)(nil)
和接口指针的任何信息。我们应该如何解释这个语句?指针与接口有什么关系,我在哪里可以找到关于接口指针的信息?