Skip to content
Related Articles

Related Articles

Improve Article
How to compare Structs with the Different Values Assigned to Data Fields in Golang?
  • Last Updated : 26 May, 2020

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:




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

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



Example 1:




// 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")
    }
}

Output:

Variable A1 is equal to variable A2

Example 2:




// 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")
    }
}

Output:

False
False



My Personal Notes arrow_drop_up
Recommended Articles
Page :