V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
Chaox
V2EX  ›  问与答

golang 关于 nil 的问题

  •  
  •   Chaox · 2020-11-19 11:25:34 +08:00 · 905 次点击
    这是一个创建于 1256 天前的主题,其中的信息可能已经有所发展或是发生改变。

    nil 的底层实现在源码中为:

    // nil is a predeclared identifier representing the zero value for a
    // pointer, channel, func, interface, map, or slice type.
    var nil Type // Type must be a pointer, channel, func, interface, map, or slice type
    
    // Type is here for the purposes of documentation only. It is a stand-in
    // for any Go type, but represents the same type for any given function
    // invocation.
    type Type int
    
    

    在源码中好像是 int 类型? 我的疑惑是 nil 在实际使用中是有类型区分的,可以把 nil 转换为不同类型的 nil 。那 nil 是接口类型还是 int 类型? 如果接口类型他的 data,type 是什么内容呢?如果是 int 类型底层实现又是如何呢?

    2 条回复    2020-11-19 13:30:00 +08:00
    darksword21
        1
    darksword21  
       2020-11-19 11:39:04 +08:00
    好问题,楼下回答
    elechi
        2
    elechi  
       2020-11-19 13:30:00 +08:00
    理解成空指针就行
    // Type must be a pointer, channel, func, interface, map, or slice type
    对应的,上面这些其实都是指针类型
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4108 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 05:26 · PVG 13:26 · LAX 22:26 · JFK 01:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.