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

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 structuretype 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 == operatorpackage main  import "fmt"  // Creating a structuretype Employee struct {    FirstName, LastName string    Salary              int    Age                 int}  // Main functionfunc 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 Assignedpackage main  import "fmt"  // Creating a structuretype triangle struct {    side1 float64    side2 float64    side3 float64    color string}  // Main functionfunc 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
Golang-Program
Picked
Go Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
6 Best Books to Learn Go Programming Language
strings.Replace() Function in Golang With Examples
fmt.Sprintf() Function in Golang With Examples
How to Split a String in Golang?
Inheritance in GoLang
Different Ways to Find the Type of Variable in Golang
Interfaces in Golang
How to Trim a String in Golang?
How to convert a string in lower case in Golang?
How to compare times in Golang?