package main

type S struct{}

func (s S) F() {}

type IF interface {
	F()
}

func InitType() S {
	var s S
	return s
}

func InitPointer() *S {
	var s *S
	return s
}
func InitEfaceType() interface{} {
	var s S
	return s
}

func InitEfacePointer() interface{} {
	var s *S
	return s
}

func InitIfaceType() IF {
	var s S
	return s
}

func InitIfacePointer() IF {
	var s *S
	return s
}

func main() {
	//println(InitType() == nil)
	println(InitPointer() == nil)
	println(InitEfaceType() == nil)
	println(InitEfacePointer() == nil)
	println(InitIfaceType() == nil)
	println(InitIfacePointer() == nil)
}

结果是

// cannot convert nil to type S
true
false
false
false
false

第一个cannot convert nil to type S是?

true 是因为函数返回的是指针类型,指针类型的零值是 nil

后面的结果都是 false 的原因是:后面的函数返回的结果都是 interface,而一个 interface 如果想要等于 nil 的话,那么它的类型都必须等于 nil,也就是说必须明确的返回 nil 才可以。但是这些函数都是返回来了含有类型的 interface,所以不等于 nil。

参考