How to know if a variable of arbitrary type is Zero in Golang?

前端 未结 4 1303
我寻月下人不归
我寻月下人不归 2021-01-18 01:35

Because not all types are comparable, e.g. a slice. So we can\'t do this

var v ArbitraryType
v == reflect.Zero(reflect.TypeOf(v)).Interface()
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-18 02:20

    See this post:

    Golang: Reflection - How to get zero value of a field type

    Basically you need to have special cases for the non comparable types.

提交回复
热议问题