Checking if structure is empty or not in Golang

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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.")
    }
}

chevron_right


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:



filter_none

edit
close

play_arrow

link
brightness_4
code

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.")
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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.")
    }
}

chevron_right


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.