If the structure is empty means that there is no field present inside that particular structure. In Golang, the size of an empty structure is zero. Whenever the user wants to know if the created structure is empty or not, he can access the structure in the main function through a variable. If there doesn’t exist any field inside the structure, he can simply display that the structure is empty.
Syntax:
type structure_name struct {
}
There are different ways to find out whether a structure is empty or not as shown below.
1) To check if the structure is empty:
package main import ( "fmt") type Book struct { } func main() { var bk Book if (Book{} == bk) { fmt.Println("It is an empty structure.") } else { fmt.Println("It is not an empty structure.") } } |
Output:
It is an empty structure.
Explanation: In the above example, we have created a structure named “Book” in which there is no existing field. In the main function, we created a variable to access our structure. Since there are no fields specified in the structure, it will print that it is an empty structure. Now, if there are fields present in the structure, it will return the message that it is not an empty structure as shown below:
package main import ( "fmt") type Book struct { qty int} func main() { var bk Book if (Book{500} == bk) { fmt.Println("It is an empty structure.") } else { fmt.Println("It is not an empty structure.") } } |
Output:
It is not an empty structure.
Explanation: In the above example, we have created a structure named “Book” in which we have declared a field named “qty” of data type int. In the main function, we created a variable to access our structure. Since there is a field present in the structure, it will print that it is not an empty structure.
2) Using switch case:
package main import ( "fmt") type articles struct { } func main() { x := articles{} switch { case x == articles{}: fmt.Println("Structure is empty.") default: fmt.Println("Structure is not empty.") } } |
Output:
Structure is empty.
Explanation: In this example, we created a structure named “articles” in which no fields are declared. Inside the main function, we created a variable “x” and used a switch case to access our structure. Since there are no fields present in the structure, the program will display that the structure is empty.
Recommended Posts:
- Checking the Given File Exists or Not in Golang
- Golang | Checking the string for the specified regular expression
- How to Create an Empty File in Golang?
- Golang | Checking the byte of slice for specified regular expression
- Checking Slice of bytes for equality under Unicode case folding in Golang
- Nested Structure in Golang
- Structure Equality in Golang
- Promoted Methods in Golang Structure
- Anonymous Structure and Field in Golang
- Promoted Fields in Golang Structure
- Function as a Field in Golang Structure
- Printing structure variables in console in Golang
- unordered_map empty in C++ STL
- unordered_set empty() function in C++ STL
- How to check whether an array is empty using PHP?
- Best way to initialize empty array in PHP
- multimap empty() function in C++ STL
- Why to check both isset() and !empty() function in PHP ?
- Check if an array is empty or not in JavaScript
- How to check an object is empty using JavaScript?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

