V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
drzhaug
V2EX  ›  程序员

请教一个 go 的语法问题

  •  
  •   drzhaug · 2017-08-22 15:55:01 +08:00 · 2000 次点击
    这是一个创建于 2437 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码

    type People interface {
    	Show()
    }
    
    type Student struct {
    }
    
    func (stu *Student) Show() {
    
    }
    
    func live() People {
    	var stu *Student
    	return stu
    }
    
    func main() {
    	if live() == nil {
    		fmt.Println("A")
    	} else {
    		fmt.Println("B")
    	}
    }
    
    

    我查了好久也没找到 为什么 live()返回值强制转换为 interface 类型后 live 返回值依然为 nil,但是 live() == nil 为 false ! 请大神指点一下。

    5 条回复    2017-08-22 16:28:25 +08:00
    koebehshian
        1
    koebehshian  
       2017-08-22 16:13:22 +08:00   ❤️ 1
    xrlin
        2
    xrlin  
       2017-08-22 16:15:24 +08:00 via iPhone   ❤️ 1
    Interface 转换后有类型 值两部分
    Muninn
        3
    Muninn  
       2017-08-22 16:18:53 +08:00   ❤️ 1
    反正尽量少写 == nil。。。
    一楼说的很清楚了
    我不明 error 都只敢!=
    jarlyyn
        4
    jarlyyn  
       2017-08-22 16:21:51 +08:00
    nil 和 interface(nil)是两个东西……
    drzhaug
        5
    drzhaug  
    OP
       2017-08-22 16:28:25 +08:00
    多谢各位啦
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1244 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 23:20 · PVG 07:20 · LAX 16:20 · JFK 19:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.