golang判断结构体为空_如何在Golang中检查结构是否为空?
golang判断结构体为空
The size of an empty structure is zero in Golang. Here, empty structure means, there is no field in the structure.
在Golang中, 空结构的大小为零。 在此, 空结构表示该结构中没有字段。
Eg:
例如:
Type structure_name struct {
}
There are many ways to check if structure is empty. Examples are given below.
有很多方法可以检查结构是否为空 。 示例如下。
Example 1:
范例1:
package main
import (
"fmt"
)
type Person struct {
}
func main() {
var st Person
if (Person{} == st) {
fmt.Println("It is an empty structure")
} else {
fmt.Println("It is not an empty structure")
}
}
It is an empty structure
Example 2:
范例2:
package main
import (
"fmt"
)
type Person struct {
age int
}
func main() {
var st Person
if (Person{20} == st) {
fmt.Println("It is an empty structure")
} else {
fmt.Println("It is not an empty structure")
}
}
It is not an empty structure
If a structure has fields, then how to check whether structure has been initialised or not?
如果结构具有字段,那么如何检查结构是否已初始化?
Please follow given below examples...
请遵循以下示例...
Syntax:
句法:
Type structure_name struct {
variable_name type
}
Example 1:
范例1:
package main
import (
"fmt"
"reflect"
)
type Person struct {
age int
}
func (x Person) IsStructureEmpty() bool {
return reflect.DeepEqual(x, Person{})
}
func main() {
x := Person{}
if x.IsStructureEmpty() {
fmt.Println("Structure is empty")
} else {
fmt.Println("Structure is not empty")
}
}
Output
输出量
Structure is empty
Example 2:
范例2:
package main
import (
"fmt"
"reflect"
)
type Person struct {
age int
}
func (x Person) IsStructureEmpty() bool {
return reflect.DeepEqual(x, Person{})
}
func main() {
x := Person{}
x.age = 20
if x.IsStructureEmpty() {
fmt.Println("Structure is empty")
} else {
fmt.Println("Structure is not empty")
}
}
Output
输出量
Structure is not empty
Using switch statement
使用switch语句
Example 1:
范例1:
package main
import (
"fmt"
)
type Person struct {
}
func main() {
x := Person{}
switch {
case x == Person{}:
fmt.Println("Structure is empty")
default:
fmt.Println("Structure is not empty")
}
}
Output
输出量
Structure is empty
Example 2:
范例2:
package main
import (
"fmt"
)
type Person struct {
age int
}
func main() {
x := Person{}
switch {
case x == Person{1}:
fmt.Println("Structure is empty")
default:
fmt.Println("Structure is not empty")
}
}
Output
输出量
Structure is not empty
翻译自: https://www.includehelp.com/golang/how-to-check-if-structure-is-empty.aspx
golang判断结构体为空
golang判断结构体为空_如何在Golang中检查结构是否为空?相关推荐
- 判断表达式值是否为空_如何在 Python 中判断列表是否为空
在判断列表是否为空时,你更喜欢哪种方式?决定因素是什么? 在 Python 中有很多检查列表是否是空的方式,在讨论解决方案前,先说一下不同方法涉及到的不同因素. 我们可以把判断表达式可以分为两个阵营: ...
- 自定义注解在拦截器中为空_如何在Android中为特定联系人设置自定义铃声
自定义注解在拦截器中为空 Everyone likes to know who's calling before they actually pick up the phone, and the ea ...
- python语言结构化语言_如何在python中处理结构化语言文件
我有一个像这样的大型结构化语言文件:TASK SchM_Task { TYPE = AUTO; SCHEDULE = NON; PRIORITY = 160; ACTIVATION = 1; TIMI ...
- python的内置数据结构可变不可变_如何在Python中实现不可变的数据结构?
问题 您需要在Python中实现不可变的数据结构. 介绍.. 当您要防止多个人同时在并行编程中修改一条数据时,不可变数据结构非常方便.可变数据结构(例如Array)可以随时更改,而可变数据结构则不能更 ...
- ubuntu修改ip地址后如何保存_如何在 Ubuntu 中检查你的 IP 地址 | Linux 中国
不知道你的 IP 地址是什么?以下是在 Ubuntu 和其他 Linux 发行版中检查 IP 地址的几种方法.-- Sergiu 不知道你的 IP 地址是什么?以下是在 Ubuntu 和其他 Linu ...
- 如何用python进行相关性分析_如何在python中检查连续变量和分类变量之间的相关性?...
将分类变量转换为虚拟变量,并将变量放在numpy.array中.例如: data.csv:age,size,color_head 4,50,black 9,100,blonde 12,120,brow ...
- java中如何检查字符串都是数字_如何在Java中检查字符串是否为数字?
我们将检查字符串是否为数字-借助逻辑,我们将解决此问题,第一步,我们将使用一个名为str的字符串变量,并将任何值存储在其中. 在第二步中,我们将使用一个名为str_numeric的布尔变量,该变量存储 ...
- python 输入数字变成密码_如何在python中检查数字的“密码”
我建议使用sets和stdlib中的string包作为可接受字符的列表.在 我还建议进行一点重构,以删除大量带有if / else分支的嵌套.在import string upper = set(li ...
- python字符串转浮点数_如何在Python中检查字符串是否为数字(浮点数)?
python字符串转浮点数 Using python it is very to interconvert the datatypes of a variable. A string can be e ...
最新文章
- DataGrid 或 DataView 中删除项时告知是否删除
- spring cloud教程之使用spring boot创建一个应用
- linux下安装新硬盘并挂载mount
- 【DP】【记忆化搜索】NIKOLA(jzoj 1150)
- c vector用法是什么
- day01语法python入门_2
- 采购订单单价金额屏蔽
- [Linux] linux下安装配置 zookeeper/redis/solr/tomcat/IK分词器 详细实例.
- 使用QTP录制自带Flight小实例
- python100例详解-Python编程之属性和方法实例详解
- mysql case then效率快_详解Mysql case then使用
- 学习document.location.href和document.location.replace 区别
- dm9000数据速率_STM32网络通信之DM9000A电路设计
- xml文件怎么转换成wps_word文档怎样转换成xml 怎么将XML文档转成WORD文档
- 储存profiles是什么意思_程序开发里面的profile 是什么意思
- 【Ant Design Vue】封装导出Excel文件的功能模块到ele-pro-table
- linux解压tar文件夹
- 美团商品知识图谱的构建及应用
- 关于电子账户开户四五要素
- 计算机工程学院新生欢迎标语,欢迎新生标语(精选50句)
热门文章
- eclipse中在类saolei.Test 中找不到main方法
- 推荐几款爬虫软件与无需编程的数据分析工具
- python 编辑数学公式_用python编写数学公式
- 第一个被赋予公明身份的机器人_一文读懂机器人的“眼睛”
- 聊聊一直困扰前端程序员的浏览器兼容-【css】
- 【区块链Go语言实现】Part 1:区块链基本原型
- unity json解析IPA后续
- 实现字符串的编码转换,用以解决字符串乱码问题
- 美团点评DBProxy读写分离使用说明
- android paint 圆角 绘制_[BOT] 一种android中实现“圆角矩形”的方法