Chaox
V2EX  ›  问与答

golang 关于 nil 的问题

  •  
  •   Chaox · Nov 19, 2020 · 1659 views
    This topic created in 2030 days ago, the information mentioned may be changed or developed.

    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 replies    2020-11-19 13:30:00 +08:00
    darksword21
        1
    darksword21  
    PRO
       Nov 19, 2020
    好问题,楼下回答
    elechi
        2
    elechi  
       Nov 19, 2020
    理解成空指针就行
    // Type must be a pointer, channel, func, interface, map, or slice type
    对应的,上面这些其实都是指针类型
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3072 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 12:54 · PVG 20:54 · LAX 05:54 · JFK 08:54
    ♥ Do have faith in what you're doing.