日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

golang判断结构体为空_如何在Golang中检查结构是否为空?

發布時間:2023/12/1 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 mainimport ("fmt" )type Person struct {}func main() {var st Personif (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 mainimport ("fmt" )type Person struct {age int }func main() {var st Personif (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 mainimport ("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 mainimport ("fmt""reflect" )type Person struct {age int }func (x Person) IsStructureEmpty() bool {return reflect.DeepEqual(x, Person{}) }func main() {x := Person{}x.age = 20if 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 mainimport ("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 mainimport ("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中检查结构是否为空?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。