How to compare Structs with the Different Values Assigned to Data Fields in Golang?

A struct (Structure) is a user-defined type in Golang that contains a collection of named fields/properties which creates own data types by combining one or more types. Also, this concept is generally compared with the classes in object-oriented programming. A struct has different fields of the same or different data types and is declared by composing a fixed set of unique fields.

Defining a struct type: The declaration of the struct starts with the keyword type, then a name for a new struct is defined and followed by keyword struct. After that curly bracket starts with defining a series of data fields with a name and a type.

Syntax:

type StructName struct {
    field1 fieldType1
    field2 fieldType2
}

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Creating a structure
type Employee struct {
    firstName string
        lastName string
            salary int
                age int
}

chevron_right


Below examples are given to explain- Using a comparison operator, structs of the same type can be compared.



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate the
// concept of Struct comparison
// using == operator
package main
  
import "fmt"
  
// Creating a structure
type Employee struct {
    FirstName, LastName string
    Salary              int
    Age                 int
}
  
// Main function
func main() {
  
    // Creating variables
    // of Employee structure
    A1 := Employee{
        FirstName: "Seema",
        LastName:  "Singh",
        Salary:    20000,
        Age:       23,
    }
  
    A2 := Employee{
        FirstName: "Seema",
        LastName:  "Singh",
        Salary:    20000,
        Age:       23,
    }
  
    // Checking if A1 is equal
    // to A2 or not
    // Using == operator
    if A1 == A2 {
  
        fmt.Println("Variable A1 is equal to variable A2")
  
    } else {
  
        fmt.Println("Variable A1 is not equal to variable A2")
    }
}

chevron_right


Output:

Variable A1 is equal to variable A2

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate the
// concept of Struct comparison
// with the Different Values Assigned
package main
  
import "fmt"
  
// Creating a structure
type triangle struct {
    side1 float64
    side2 float64
    side3 float64
    color string
}
  
// Main function
func main() {
  
    // Creating variables
    // of Triangle structure
    var tri1 = triangle{10, 20, 30, "Green"}
    tri2 := triangle{side1: 20, side2: 10,
                  side3: 10, color: "Red"}
  
    // Checking if tri1 is equal
    // to tri2 or not
    // Using == operator
    if tri1 == tri2 {
        fmt.Println("True")
    } else {
        fmt.Println("False")
    }
  
    // Checking if tri3 is equal
    // to tri4 or not
    // Using == operator
    tri3 := new(triangle)
    var tri4 = &triangle{}
  
    if tri3 == tri4 {
        fmt.Println("True")
    } else {
        fmt.Println("False")
    }
}

chevron_right


Output:

False
False



My Personal Notes arrow_drop_up


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.