Checking if structure is empty or not in Golang


In Go, structures are used to define custom data types that can hold multiple fields of different data types. Sometimes, it is necessary to check if a structure is empty or not. An empty structure is one that has all of its fields set to their zero values. In this article, we will explore different ways to check if a structure is empty or not in Go.

Using Reflect Package

The reflect package in Go provides a way to perform reflection on values of various types, including structures. Using the reflect package, we can check if a structure is empty or not. Here is an example −

Example

package main

import (
   "fmt"
   "reflect"
)

type Employee struct {
   Name     string
   Age      int
   Salary   float64
}

func main() {
   emp1 := Employee{}
   emp2 := Employee{"John Doe", 25, 3000}

   if reflect.DeepEqual(emp1, Employee{}) {
      fmt.Println("emp1 is empty")
   } else {
      fmt.Println("emp1 is not empty")
   }

   if reflect.DeepEqual(emp2, Employee{}) {
      fmt.Println("emp2 is empty")
   } else {
      fmt.Println("emp2 is not empty")
   }
}

Output

emp1 is empty
emp2 is not empty

In this code, we define a structure Employee with three fields: Name, Age, and Salary. We create two instances of the Employee structure, emp1 and emp2. The emp1 structure is empty because all its fields are set to their zero values. We use the reflect.DeepEqual function to compare emp1 with an empty instance of Employee structure. If they are equal, it means that emp1 is empty. Similarly, we compare emp2 with an empty instance of Employee structure to check if it is empty.

Using Custom Methods

Another way to check if a structure is empty or not is by defining custom methods. We can define a method that checks if all fields of the structure are set to their zero values. Here is an example −

Example

package main

import "fmt"

type Employee struct {
   Name     string
   Age      int
   Salary   float64
}

func (e Employee) isEmpty() bool {
   return e.Name == "" && e.Age == 0 && e.Salary == 0
}

func main() {
   emp1 := Employee{}
   emp2 := Employee{"John Doe", 25, 3000}

   if emp1.isEmpty() {
      fmt.Println("emp1 is empty")
   } else {
      fmt.Println("emp1 is not empty")
   }

   if emp2.isEmpty() {
      fmt.Println("emp2 is empty")
   } else {
      fmt.Println("emp2 is not empty")
   }
}

Output

emp1 is empty
emp2 is not empty

In this code, we define a custom method isEmpty() for the Employee structure. This method checks if all fields of the structure are set to their zero values. We call this method on the emp1 and emp2 structures to check if they are empty or not.

Conclusion

In this article, we explored different ways to check if a structure is empty or not in Go. We can use the reflect package to perform reflection on structures and compare them with an empty instance of the same structure. We can also define custom methods that check if all fields of the structure are set to their zero values. By using these techniques, we can easily determine if a structure is empty or not in our Go programs.

Updated on: 07-Apr-2023

858 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements