In Go, structs are incredible and powerful composite data types that allow us to group multiple variables of different data types into a single unit.
They play a crucial role in building the real-world applications as they allow us to represent the real-world entities as single unites.
One of the common tasks when working with structs in Go is checking whether the struct is empty or not.
In this tutorial, we will learn about the workings of structs in Go. We will mainly focus on learning how to check if a struct is empty.
Golang Structs
Before we delve into struct copying, let us first understand the basics of structs in Go.
Defining a Struct
To define a struct in Go, we use the “type” keyword followed by a struct definition. An example of a struct is as follows:
ModelName string
Mileage int
Year int
}
In the given example, we define a struct called “Car” with three main fields: ModelName, Mileage, and Year.
Struct Instantiation
To create an instance of a struct, we can simply declare a variable of the struct type and initialize it using a struct literal.
Example:
ModelName: "Mercedes Benz GLS-250 (4Matic)",
Mileage: 299,
Year: 2029,
}
In this case, we create an instance of the struct called “c” and assign a value to each of the fields that pertains to that instance.
Accessing the Struct Fields
We can access the fields of a struct using the dot notation as shown in the following example code:
import "fmt"
type Car struct {
ModelName string
Mileage int
Year int
}
func main() {
c := Car{
ModelName: "Mercedes Benz GLS-250 (4Matic)",
Mileage: 299,
Year: 2029,
}
fmt.Println("Model Name:", c.ModelName)
fmt.Println("Mileage:", c.Mileage)
fmt.Println("Year:", c.Year)
}
Once we run the given code, it should print the fields and values of the struct instance as follows:
Mileage: 299
Year: 2029
Check the Empty Struct in Golang
When we check whether a struct is empty, we essentially determine if all the fields of the struct are set to their zero values.
If all the fields of the struct are set to their zero values, we consider them as empty. Let us dive in and discuss the techniques of checking for empty struct.
Method 1: Compare with the Zero Value
The first technique that we will use involves comparing the struct instance with a new instance of the same struct.
This is because when we declare a struct without explicitly initializing its fields, Go automatically sets them to their zero values for integers (“” for strings and nil for pointers).
Take a look at the following example that demonstrates how to compare the struct values:
import "fmt"
type Car struct {
ModelName string
Mileage int
Year int
}
func isEmptyStruct(c Car) bool {
emptyCar := Car{}
return c == emptyCar
}
func main() {
car1 := Car{}
car2 := Car{
ModelName: "Mercedes Benz GLS-250 (4Matic)",
Mileage: 299,
Year: 2029,
}
fmt.Println("car1 is empty:", isEmptyStruct(car1))
fmt.Println("car2 is empty:", isEmptyStruct(car2))
}
In the given example, we define a function called “isEmptyStruct” which performs a comparison to check whether the struct is empty.
Running the code should return the following output:
car2 is empty: false
Method 2: Using a Reflection
The reflection package plays a crucial role in inspecting the types during runtime. When it comes to comparison, it allows us to inspect the fields of a struct dynamically by iterating through the fields of the struct.
An example is as follows:
import (
"fmt"
"reflect"
)
type Car struct {
ModelName string
Mileage int
Year int
}
func isEmptyStruct(p interface{}) bool {
val := reflect.ValueOf(p)
for i := 0; i < val.NumField(); i++ {
field := val.Field(i)
if !reflect.DeepEqual(field.Interface(), reflect.Zero(field.Type()).Interface()) {
return false
}
}
return true
}
func main() {
car1 := Car{}
car2 := Car{
ModelName: "Mercedes Benz GLS-250 (4Matic)",
Mileage: 299,
Year: 2029,
}
fmt.Println("car1 is empty:", isEmptyStruct(car1))
fmt.Println("car2 is empty:", isEmptyStruct(car2))
}\
In this example, we iterate over the fields of the struct using a “for” loop. We then check if the field is equal to “reflect.Zero” which determines if the struct is empty.
Conclusion
In this tutorial, we discussed the various methods and techniques that we can use to check whether a struct is empty or not.