V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
drzhaug
V2EX  ›  程序员

请教一个 go 的语法问题

  •  
  •   drzhaug · Aug 22, 2017 · 2692 views
    This topic created in 3177 days ago, the information mentioned may be changed or developed.

    代码

    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 replies    2017-08-22 16:28:25 +08:00
    koebehshian
        1
    koebehshian  
       Aug 22, 2017   ❤️ 1
    xrlin
        2
    xrlin  
       Aug 22, 2017 via iPhone   ❤️ 1
    Interface 转换后有类型 值两部分
    Muninn
        3
    Muninn  
       Aug 22, 2017   ❤️ 1
    反正尽量少写 == nil。。。
    一楼说的很清楚了
    我不明 error 都只敢!=
    jarlyyn
        4
    jarlyyn  
       Aug 22, 2017
    nil 和 interface(nil)是两个东西……
    drzhaug
        5
    drzhaug  
    OP
       Aug 22, 2017
    多谢各位啦
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   748 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 20:36 · PVG 04:36 · LAX 13:36 · JFK 16:36
    ♥ Do have faith in what you're doing.