关于Go语言中nil和interface的问题
2014-02-19 20:06
603 查看
package demo01
//关于Go语言中nil和interface的问题
//Date:2014-2-19 20:04:25
import (
"fmt"
)
func NilTestBase() {
//test05()
//fmt.Println("=====================")
test06()
//nilStringTest()
}
//类Student是类Person的子类
type Person struct {
}
type Student struct {
Person
}
type IPerson interface {
speak()
}
//为Person添加方法以实现IPerson接口
func (*Person) speak() {
fmt.Println("I am a person")
}
//正常的返回值
func test01() Person {
var p Person
return p
}
//编译错误:不能将子类对象作为父类直接返回
//func test02() Person {
// var s Student
// return s
//}
//可以直接以interface{}形式返回
func test03() interface{} {
var s Student
return s
}
//如果Person实现了某个接口,则可以将Person对象作为接口类型的返回值
//但是,此时即使对象为空,将对象作为接口返回之后,接口也将不为空
func test04() IPerson {
var p *Person = nil
fmt.Println("in test04:", p == nil) //true
return p
}
func test05() {
i := test04()
fmt.Println("in test05:", i == nil) //false
fmt.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) //true
var p1 *Person = nil
var rip IPerson = p1
fmt.Println("7.rip==nil:", rip == nil) //false
fmt.Printf("8.Type of rip:%T \n", rip)
var b *IPerson
fmt.Println("9.b==nil:", b == nil) //true
//output:
/*
0.Type of nil:<nil>
1.Type of f:*interface {}
2.*f==nil: true
3.Type of p:*demo01.Person
4.Type of *p:demo01.Person
5.Type of ip:*demo01.IPerson
6.*ip==nil: true
7.rip==nil: false
8.Type of rip:*demo01.Person
9.b==nil: true
*/
}
func nilStringTest() {
var a string
var b *string = &a
fmt.Println("b==nil:", b == nil)
//fmt.Println("*b==nil:", *b == nil)
}
//关于Go语言中nil和interface的问题
//Date:2014-2-19 20:04:25
import (
"fmt"
)
func NilTestBase() {
//test05()
//fmt.Println("=====================")
test06()
//nilStringTest()
}
//类Student是类Person的子类
type Person struct {
}
type Student struct {
Person
}
type IPerson interface {
speak()
}
//为Person添加方法以实现IPerson接口
func (*Person) speak() {
fmt.Println("I am a person")
}
//正常的返回值
func test01() Person {
var p Person
return p
}
//编译错误:不能将子类对象作为父类直接返回
//func test02() Person {
// var s Student
// return s
//}
//可以直接以interface{}形式返回
func test03() interface{} {
var s Student
return s
}
//如果Person实现了某个接口,则可以将Person对象作为接口类型的返回值
//但是,此时即使对象为空,将对象作为接口返回之后,接口也将不为空
func test04() IPerson {
var p *Person = nil
fmt.Println("in test04:", p == nil) //true
return p
}
func test05() {
i := test04()
fmt.Println("in test05:", i == nil) //false
fmt.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) //true
var p1 *Person = nil
var rip IPerson = p1
fmt.Println("7.rip==nil:", rip == nil) //false
fmt.Printf("8.Type of rip:%T \n", rip)
var b *IPerson
fmt.Println("9.b==nil:", b == nil) //true
//output:
/*
0.Type of nil:<nil>
1.Type of f:*interface {}
2.*f==nil: true
3.Type of p:*demo01.Person
4.Type of *p:demo01.Person
5.Type of ip:*demo01.IPerson
6.*ip==nil: true
7.rip==nil: false
8.Type of rip:*demo01.Person
9.b==nil: true
*/
}
func nilStringTest() {
var a string
var b *string = &a
fmt.Println("b==nil:", b == nil)
//fmt.Println("*b==nil:", *b == nil)
}
相关文章推荐
- Go语言第一深坑:interface 与 nil 的比较
- 关于go语言中的Slice切片问题
- Go语言interface 与 nil 的比较
- 关于GDB调试go语言程序中出现 os.Args无法打印的问题
- 关于Java语言中的线程安全问题
- ios浅谈关于nil和 NIL区别及相关问题(转)
- 关于Abstract interface的问题。
- go语言中的interface使用
- 使用Vscode调试go语言问题
- 关于QNetworkInterface::allAddresses ()奇怪的问题
- Go语言中的Interface
- go nil转为interface{}后判断不可靠
- 关于语言的执行效率问题
- 采访:关于 Go 语言和《Go Web编程》
- 关于VS编译DevExpress默认产生几个多余的语言包的问题解决
- 关于apache不能解析php语言的问题
- swift 与 C 语言混合编程,主要关于指针变量的调用问题
- Go语言调用c动态库遇到的一些问题
- 关于PO文本不支持多语言维护问题
- go语言 interface 初探