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:
例如:
<pre><code class="has"> Type structure_name struct { } </code> </pre>There are many ways to check if structure is empty. Examples are given below.
有很多方法可以检查结构是否为空 。 示例如下。
Example 1:
范例1:
<pre><code class="has">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") } } </code> </pre> <pre><code class="has">It is an empty structure </code> </pre>Example 2:
范例2:
<pre><code class="has">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") } } </code> </pre> <pre><code class="has">It is not an empty structure </code> </pre>If a structure has fields, then how to check whether structure has been initialised or not?
如果结构具有字段,那么如何检查结构是否已初始化?
Please follow given below examples...
请遵循以下示例...
Syntax:
句法:
<pre><code class="has"> Type structure_name struct { variable_name type } </code> </pre>Example 1:
范例1:
<pre><code class="has">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") } } </code> </pre>Output
输出量
<pre><code class="has">Structure is empty </code> </pre>Example 2:
范例2:
<pre><code class="has">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") } } </code> </pre>Output
输出量
<pre><code class="has">Structure is not empty </code> </pre>Using switch statement
使用switch语句
Example 1:
范例1:
<pre><code class="has">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") } } </code> </pre>Output
输出量
<pre><code class="has">Structure is empty </code> </pre>Example 2:
范例2:
<pre><code class="has">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") } } </code> </pre>Output
输出量
<pre><code class="has">Structure is not empty </code> </pre> <blockquote>翻译自: https://www.includehelp.com/golang/how-to-check-if-structure-is-empty.aspx
</blockquote>golang判断结构体为空
您可能感兴趣的文章:
golang判断结构体为空_如何在Golang中检查结构是否为空?
数据结构和算法(Golang实现)(7)简单入门Golang-标准库
golang判断结构体为空
Golang中interface内部构造与面试真题分析
golang中的nil
GO--接口开发,空结构体如何返回一个空数组
数据结构和算法(Golang实现)(28)查找算法-AVL树
数据结构和算法(Golang实现)(1)简单入门Golang-前言
Go语言学习(十二)面向对象编程-结构体
数据结构和算法(Golang实现)(4)简单入门Golang-结构体和方法