博客
关于我
Go语言如何判断是否是零值
阅读量:430 次
发布时间:2019-03-06

本文共 629 字,大约阅读时间需要 2 分钟。

通过封装IsZeroOfUnderlyingType方法判断,代码如下

package mainimport (	"fmt"	"reflect")type Person struct {	Name string	Age  int}func IsZeroOfUnderlyingType(x interface{}) bool {	return reflect.DeepEqual(x, reflect.Zero(reflect.TypeOf(x)).Interface())}func main() {	var person Person //定义一个零值	fmt.Println(IsZeroOfUnderlyingType(person)) //零值结构体,输出true	person.Name = "chenqiognhe"                 //结构体属性Name赋值	fmt.Println(IsZeroOfUnderlyingType(person)) //输出false	fmt.Println(IsZeroOfUnderlyingType(person.Age)) //Age仍是零值,输出true	person.Age = 18                                 //Age赋值	fmt.Println(IsZeroOfUnderlyingType(person.Age)) //输出false}

转载地址:http://uwlkz.baihongyu.com/

你可能感兴趣的文章
NeurIPS(神经信息处理系统大会)-ChatGPT4o作答
查看>>
neuroph轻量级神经网络框架
查看>>
Neutron系列 : Neutron OVS OpenFlow 流表 和 L2 Population(7)
查看>>
new Blob()实现不同类型的文件下载功能
查看>>
New Concept English three (35)
查看>>
NEW DATE()之参数传递
查看>>
New Journey--工作五年所思所感小记
查看>>
new Queue(REGISTER_DELAY_QUEUE, true, false, false, params)
查看>>
New Relic——手机应用app开发达人的福利立即就到啦!
查看>>
new work
查看>>
new 一个button 然后dispose,最后这个button是null吗???
查看>>
NewspaceGPT的故事续写能力太强了
查看>>
NewspaceGPT绘制时序图
查看>>
NewspaceGPT绘制类图
查看>>
new一个对象的过程
查看>>
new和delete用法小结
查看>>
new对象时,JVM内部究竟藏了什么小秘密?
查看>>
new操作符的实现原理
查看>>
Next.js React Server Components 教程
查看>>
NextGen Mirth Connect XStream反序列化远程代码执行漏洞(CVE-2023-43208)
查看>>