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判斷結構體為空

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/378494.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/378494.shtml
英文地址,請注明出處:http://en.pswp.cn/news/378494.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

智能車復工日記【5】:起跑線的識別與車庫入庫

博主聯系方式: QQ:1540984562 QQ交流群:892023501 群里會有往屆的smarters和電賽選手,群里也會不時分享一些有用的資料,有問題可以在群里多問問。 系列文章 【智能車Code review】—曲率計算、最小二乘法擬合 【智能車Code review】——坡道圖像與控制處理 【智能車Code re…

文本框只允許輸入數字.net/javascript

<input type"text" name"test" onKeyUp"test1.value(this.valuethis.value.replace(/\D/g,).substring(0,6)).substring(0,3)" > <br /><input name"test1" type"text" > 用Up和Down有區別 只能輸入數字、…

jquery全選,jquery全不選,jquery反選

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns"http://www.w3.org/1999/xhtml"> <head> <title>全選&#xff0c;不…

obj[]與obj._Ruby中帶有示例的Array.rassoc(obj)方法

obj[]與obj.Array.rassoc(obj)方法 (Array.rassoc(obj) Method) In this article, we will study about Array.rassoc(obj) method. You all must be thinking the method must be doing something which is related to the insertion of a certain element. It is not as simp…

oracle平均活動會話數,Oracle11g r2的活動會話數問題解決

如果你在Oracle11g r2的活動會話數的實際操作中&#xff0c;你對Oracle11g r2的活動會話數有不解之處時&#xff0c;你可以通過以下的文章對其的實際應用與功能有所了解&#xff0c;以下是文章的具體介紹&#xff0c;望你瀏覽完以下的內容會有所收獲。在Oracle11g r2的活動會話…

請分析比較下列四種染料在相同濃度和相同溫度的水染液中的聚集度大小?

請分析比較下列四種染料在相同濃度和相同溫度的水染液中的聚集度大小? 標準答案: 染料聚集度從大到小的順序為:3〉1〉4〉2 比較1-4染料結構差異,1、2共軛體系較短,3、4共軛體系較長,1、3磺酸基在端部,2、4磺酸基在中間; 染料3共軛體系長,范德華力大,而水溶性磺酸基團…

分割范圍Codeforces Round #181 (Div. 2)

每日一貼,今天的內容關鍵字為分割范圍 標題鏈接 A &#xff0c;B雖然是水題&#xff0c;但要是沒想清晰就易容掉進某個坑里。。 C&#xff1a;預處理出有所可能的和&#xff0c;然后個每個每和分給a b兩個數 D&#xff1a;競賽的時候標題沒看懂&#xff0c;題意就是說在一個n*n…

VC++動態鏈接庫深入淺出(轉)

1.概論  先來闡述一下DLL(Dynamic Linkable Library)的概念&#xff0c;你可以簡單的把DLL看成一種倉庫&#xff0c;它提供給你一些可以直接拿來用的變量、函數或類。在倉庫的發展史上經歷了“無庫&#xff0d;靜態鏈接庫&#xff0d;動態鏈接庫”的時代。 靜態鏈接庫與動態鏈…

智能車復工日記【6】:有bug的模糊PID記錄

目錄 系列文章DYY Warning算法參考鏈接:代碼.c文件.h文件調用方法希望有大佬能幫我指正錯誤之處。以后假如有機會的話再搞這個模糊吧,頭疼!!!系列文章 【智能車Code review】—曲率計算、最小二乘法擬合 【智能車Code review】——坡道圖像與控制處理 【智能車Code review…

車刀 matlab,可轉位車刀的結構

內容介紹原文檔由會員 違規屏蔽12 發布摘 要隨著有限元技術的快速發展&#xff0c;結構優化在航空航天、建筑、汽車、工程機械等領域的結構設計方面應用越來越廣泛。在刀具行業中&#xff0c;新型刀具材料不斷涌現&#xff0c;刀具的結構也在不斷發展&#xff0c;目前針對刀具的…

DtCms.Model.Advertising.cs

using System; namespace DtCms.Model {/// <summary>/// 廣告位實體類Advertising/// </summary>[Serializable]public class Advertising{public Advertising(){ }#region Modelprivate int _id;private string _title;private int _adtype;private string _adre…

分析染色溫度對染色熱力學性能和染色動力學性能的影響?實際生產時,染色溫度選擇應考慮哪些因素?舉例說明?并闡明實現低溫染色的原理。

分析染色溫度對染色熱力學性能和染色動力學性能的影響?實際生產時,染色溫度選擇應考慮哪些因素?舉例說明?并闡明實現低溫染色的原理。 答:染料的結構包括物理結構和化學結構,溫度高低會影響染料結構,影響染料在染浴中的狀態,染料的溶解、聚集、電離等性能,從而影響染…

c++stl和std_std :: replace()函數以及C ++ STL中的示例

cstl和stdC STL std :: replace()函數 (C STL std::replace() function) replace() function is a library function of algorithm header, it is used to replace an old value with a new value in the given range of a container, it accepts iterators pointing to the s…

《Two Dozen Short Lessons in Haskell》(二十)分數

《Two Dozen Short Lessons in Haskell》&#xff08;Copyright © 1995, 1996, 1997 by Rex Page&#xff0c;有人翻譯為Haskell二十四學時教程&#xff0c;該書如果不用于贏利&#xff0c;可以任意發布&#xff0c;但需要保留他們的copyright&#xff09;這本書是學習 Ha…

oracle 查詢不能重復,oracle – 如何防止在選擇查詢中選擇重復行?

我被賦予了從Oracle數據庫中選擇關鍵數據的任務,但我注意到我的select正在返回重復的行.我不需要它們用于我的報告但我不希望它們刪除它們.有人可以幫助只獲取我需要的數據.我嘗試了以下代碼,但這沒有用.SELECT distinct bbp.SUBCAR "Treadwell",bbp.BATCH_ID "…

數字圖像課程工程大作業分析

試題分析&#xff1a; 在連續的視頻中對火焰及水柱的軌跡檢測&#xff0c;效果如圖。 ** 提示&#xff1a; 1、火焰可利用亮度和顏色 2、水柱的軌跡需要先用背景差分獲得水柱的連通域&#xff0c;然后利用連通域上的像素點進行曲線的擬合&#xff0c;水槍的位置視為已知&#…

設計電子商務網站的10個技巧(轉自ITEye)

導讀&#xff1a;隨著先進科學技術的應用&#xff0c;人們無需外出逛幾個小時來“獵”東西&#xff0c;直接坐在家里就可以購買所需商品&#xff0c;支付服務費用。你只需一臺電腦就能搞定。人們習慣了周到的服務和漂亮的櫥窗&#xff0c;對網店的選擇也不例外。因此&#xff0…

分析酸對酸性染料染羊毛染色性能的影響?舉例說明酸性染料染羊毛時,如何選擇合適的染浴pH值?并說明原因。

分析酸對酸性染料染羊毛染色性能的影響?舉例說明酸性染料染羊毛時,如何選擇合適的染浴pH值?并說明原因。 標準答案: 羊毛屬于蛋白質纖維,屬于兩性纖維,酸影響羊毛的解離程度,帶電性及帶電量,影響熱力學性能。等電點以下,羊毛帶正電荷,與陰離子酸性染料之間存在靜電引…

ORACLE連接數據庫(備忘)

常用命令&#xff1a; conn sys/密碼 as sysdba 連接數據庫轉載于:https://www.cnblogs.com/jiangu66/archive/2013/05/01/3053787.html

stl swap函數_vector :: swap()函數以及C ++ STL中的示例

stl swap函數C vector :: swap()函數 (C vector::swap() function) vector::swap() is a library function of "vector" header, it is used to swap the content of the vectors, it is called with a vector and accepts another vector as an argument and swaps…