golang吧 关注:6,345贴子:14,547
  • 3回复贴,共1

萌新求问,下面这段代码为啥会panictype Action

只看楼主收藏回复

萌新求问,下面这段代码为啥会panic
type Action interface {
String()string
}
type TestAction struct {
s string
}
func (r *TestAction)String()string {
return r.s
}
func returnAction()*TestAction {
return nil
}
func getAction() Action {
return returnAction()
}
func TestI(t *testing.T){
a:= getAction()
if a!=nil{
a.String()
}
}


IP属地:安徽来自Android客户端1楼2019-10-27 18:16回复
    An interface value is nil only if the inner value and type are both unset, (nil, nil). In particular, a nil interface will always hold a nil type. If we store a nil pointer of type *int inside an interface value, the inner type will be *int regardless of the value of the pointer: (*int, nil). Such an interface value will therefore be non-nil even when the pointer inside is nil.


    IP属地:浙江2楼2019-10-27 21:19
    收起回复