This is a creation in Article, where the information may have evolved or changed.
Package demo01//questions about nil and interface in the Go language//date:2014-2-19 20:04:25import ("FMT") func niltestbase () {//test05 ()//fmt. Println ("=====================") test06 ()//nilstringtest ()}//class Student is a subclass of class person type person struct {}type Student The struct {Person}type IPerson interface {speak ()}//adds a method for the person to implement the IPerson interface func (*person) speak () {fmt. Println ("I am a person")}//normal return value func test01 () person {var p personreturn p}//Compile error: The subclass object cannot be returned directly as a parent class//func test02 () person { var s student//return s//}//can return func test03 () interface{} {var s studentreturn s}//directly as interface{} if the person implements an interface, You can use the Person object as the return value of the interface type//However, at this point, after the object is returned as an interface, even if the object is empty, the interface will not be empty func test04 () IPerson {var p *person = nilfmt. Println ("In test04:", p = = nil)//truereturn P}func test05 () {i: = test04 () fmt. Println ("In test05:", i = = nil)//falsefmt. Println ("In test05:", I. (*person) = = nil)//true}func test06 () {fmt. Printf ("0.Type of nil:%t \ n", nil) F: = new (interface{}) fmt. Printf ("1.Type of f:%t \ n", f) fmt. Println ("2.*f==nil:", *f = = nil) P: = new(person) fmt. Printf ("3.Type of p:%t \ n", p) fmt. Printf ("4.Type of *p:%t \ n", *p) IP: = new (IPerson) fmt. Printf ("5.Type of ip:%t \ n", IP) fmt. Println ("6.*ip==nil:", *ip = = nil)//truevar P1 *person = nilvar Rip IPerson = p1fmt. Println ("7.rip==nil:", rip = = nil)//falsefmt. Printf ("8.Type of rip:%t \ n", RIP) var b *ipersonfmt.println ("9.b==nil:", b = = nil)//true//output:/*0.type of nil:<nil& Gt;1.type of F:*interface {}2.*f==nil:true3. Type of P:*demo01. Person4.type of *p:demo01. Person5.type of Ip:*demo01. Iperson6.*ip==nil:true7.rip==nil:false8. Type of Rip:*demo01. Person9.b==nil:true*/}func nilstringtest () {var a stringvar b *string = &afmt. Println ("B==nil:", b = = nil)//fmt. Println ("*b==nil:", *b = = nil)}