Check for nil and nil interface in Go

后端 未结 4 1311
生来不讨喜
生来不讨喜 2020-12-07 18:48

Currently I\'m using this helper function to check for nil and nil interfaces

func isNil(a interface{}) bool {
  defer func() { recover() }()
  return a == n         


        
4条回答
  •  感动是毒
    2020-12-07 19:06

    See for example Kyle's answer in this thread at the golang-nuts mailing list.

    In short: If you never store (*T)(nil) in an interface, then you can reliably use comparison against nil, no need to use reflection. On the other hand, assigning untyped nil to an interface is always OK.

提交回复
热议问题