How do you compare two struct instances that have pointers as fields in Golang


In golang, Pointers are the variables which store the address of other variables. A struct is a counterpart to class in object-oriented programming where different fields can be placed in the struct, where these fields can be implemented later on and they have a type like int, float, string etc. In this article, we will write a Go language program to compare two struct instances that have pointers as fields.

Mathematical representation for assigning the address of variable “b” to variable “a” −

a = &b 

& denotes the address of operator, and receives the memory address of variable b and assigns it to the variable a.

Algorithm

  • Step 1 − This program imports main and fmt as necessary packages

  • Step 2 − Create an Employee struct with two fields where Name is of type string and age is of type int

  • Step 3 − Create a main function

  • Step 4 − In the main, create an e1 variable which is assigned to the values initialized in the first case using ampersand operator

  • Step 5 − In this step, create an e2 variable is assigned to the new field values using ampersand operator

  • Step 6 − Then, assign e3=e1

  • Step 7 − In this step, check for the equality of e1 and e2 and in the second case check for the equality of e1 and e3

  • Step 8 − A Boolean value will be returned which will be printed on the console using Println function from the fmt package where ln means new line

Example 1

In this example, we will write a Go language program to compare two struct instances by using an Employee struct with two fields Name and Age.

package main
import "fmt"

type Employee struct {
	Name string
	Age  int
}

func main() {
	e1 := &Employee{Name: "Karan", Age: 28}
	e2 := &Employee{Name: "Robin", Age: 35}
	e3 := e1

	fmt.Println("Is e1 is equal to e2?")
	fmt.Println(e1 == e2)
	fmt.Println("Is e1 equal to e3?")
	fmt.Println(e1 == e3)
}

Output

Is e1 is equal to e2?
false
Is e1 equal to e3?
true

Example 2

In this example, we will write a Go language program to compare two struct instances by using a Car struct with two fields Model and Year

package main

import "fmt"

type Car struct {
	Model string
	Year  int
}

func main() {
	c1 := &Car{Model: "Suzuki", Year: 2022}
	c2 := &Car{Model: "Hyundai", Year: 2019}
	c3 := c2

	fmt.Println("is c1 equal to c2?")
	fmt.Println(c1 == c2)
	fmt.Println("is derefrenced c2 is equal to c3?")
	fmt.Println(*c2 == *c3)
	fmt.Println("Is the model and year of c1 and c2 same?")
	fmt.Println(c1.Model == c2.Model && c1.Year == c2.Year)
	fmt.Println("Is c1 equal to c3?")
	fmt.Println(c1 == c3)
}

Output

is c1 equal to c2?
false
is derefrenced c2 is equal to c3?
true
Is the model and year of c1 and c2 same?
false
Is c1 equal to c3?
false

Conclusion

In this article we have explored a program of comparing two structs that have pointers as fields using two examples. In the first example, we used an Employee struct, in the second example, we used a Car struct.

Updated on: 06-Jul-2023

449 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements