Golang program to compare two instances of this struct for equality, taking into account the values in the slice


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 the equality of two structs.

Syntax

func len(v Type) int

The len() method returns the length of any parameter. It accepts one input, the data type variable whose length we want to know.

func range(variable)

The range function iterates through any data type. To utilize this, we must first put the range keyword, followed by the data type to which we want to iterate.

Algorithm

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

  • Step 2 − Create MyStruct struct with three fields: Id of type int, Name of type string and Items slice of type int

  • Step 3 − Create a main function

  • Step 4 − In the main, create a struct1 and implement ID, Name and the Items fields in that particular struct

  • Step 5 − In this step, create a struct2 and similarly like the step4 implement the fields for the struct2

  • Step 6 − Then, call the function compare_structs with two inputs struct1 and struct2 and store it inside the isEqual variable

  • Step 7 − In the function compare_structs, firstly check if the id’s or the names are not equal return false

  • Step 8 − In this case, compare the length of struct items, if they are not equal return false

  • Step 9 − Then, run a loop for the items of struct1 and each iteration, compare those items with the items of struct2, if they are not same return false

  • Step 10 − But if, they are same, true will be returned after the loop is terminated

  • Step 11 − A Boolean value i.e. is false or true will be printed on the console using the Println function from the fmt package where ln means new line

Example

In this example, we will write a Go language program to compare the equality of the two structs by comparing their length, field values and the items in the fields.

package main
import "fmt"

type MyStruct struct {
	ID    int
	Name  string
	Items []int
}

func main() {

	struct1 := MyStruct{
		ID:    1,
		Name:  "Struct 1",
		Items: []int{10, 20, 30},
	}

	struct2 := MyStruct{
		ID:    20,
		Name:  "Struct 2",
		Items: []int{10, 20, 30},
	}

	isEqual := compare_structs(struct1, struct2)
	fmt.Println("Are the structs equal?")
	fmt.Println(isEqual)
}

func compare_structs(s1, s2 MyStruct) bool {

	if s1.ID != s2.ID || s1.Name != s2.Name {
		return false
	}

	if len(s1.Items) != len(s2.Items) {
		return false
	}

	for i := range s1.Items {
		if s1.Items[i] != s2.Items[i] {
			return false
		}
	}
	return true
}

Output

Are the structs equal?
False

Conclusion

In this article, we discussed how we can compare the quality of two structs. We compiled and executed the program of comparing the instances of struct using two structs with one item as a slice whose elements are compared using iteration.

Updated on: 06-Jul-2023

137 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements