如何知道Golang中任意类型的变量是否为零?

因为并非所有类型都具有可比性一片.所以我们不能这样做
var v ArbitraryType
v == reflect.Zero(reflect.TypeOf(v)).Interface()

编辑 – 解决方案reflect.DeepEqual

var v ArbitratyType
zero := reflect.Zero(reflect.TypeOf(v)).Interface()
isZero := reflect.DeepEqual(v,zero)

去关于reflect.DeepEqual的文档

DeepEqual tests for deep equality. It uses normal == equality where possible but will scan elements of arrays,slices,maps,and fields of structs.

看这篇文章:

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

基本上,您需要针对不可比较类型的特殊情况.

相关文章

类型转换 1、int转string 2、string转int 3、string转float ...
package main import s "strings" import...
类使用:实现一个people中有一个sayhi的方法调用功能,代码如...
html代码: beego代码:
1、读取文件信息: 2、读取文件夹下的所有文件: 3、写入文件...
配置环境:Windows7+推荐IDE:LiteIDEGO下载地址:http:...