go语言一个包含nil指针的接口不是nil接口的理解

结合《go语言圣经》书中7.5.1节的说明和下面的代码,认真分析理解

package main

import (
	"bytes"
	"fmt"
	"io"
)

func hh(aa *bytes.Buffer) {
    
    
	if aa != nil {
    
    
		fmt.Println(300000)
	}
}

func ii(aa io.Writer) {
    
    
	if aa != nil {
    
    
		fmt.Println(400000)
	}
}

func main() {
    
    
	var buf *bytes.Buffer
	fmt.Println(buf == nil) //true
	fmt.Printf("%T\n", buf) //*bytes.Buffer
	if buf != nil {
    
             // 判断不成立
		fmt.Println(10000)
	}
	hh(buf) //aa==nil  没有输出
	ii(buf) //aa!=nil  成立有输出,400000

	var bb io.Writer
	fmt.Println(bb == nil) //true
	fmt.Printf("%T\n", bb) //<nil>
	if bb != nil {
    
             // 判断不成立
		fmt.Println(20000)
	}
	ii(bb) //aa==nil  没有输出

}

猜你喜欢

转载自blog.csdn.net/sinat_24354307/article/details/120725273