English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Golang Basic Tutorial

Golang Control Statements

Golang Function & Method

Golang Struct

Golang Slice & Array

Golang String

Golang Pointer

Golang Interface

Golang Concurrency

Golang exceptions (Error)

Golang other miscellanea

Go Struct Comparison

Golang's structures or struct are user-defined types that allow us to create a set of different types of elements in a single unit. Any real entity that has a set of properties or fields can be represented as a structure. This concept is often compared to classes in object-oriented programming. It can be called a lightweight class, which does not support inheritance but supports composition.

In Go language, one can use== operatororDeeplyEqual() methodCompare two structures of the same type and containing the same field values. If the structures are equal to each other (in terms of their field values), then the operators and methods both return true; otherwise, they return false. Moreover, if the variables being compared belong to different structures, the compiler will produce an error. Let's discuss this concept with an example:

Note: The DeeplyEqual() method is defined under the "reflect" package.

Use == operator to compare if structures are equal

//Concept of structure equality
//Use == operator
package main
import "fmt"
//Create a structure
type Author struct {
    name       string
    branch     string
    language  string
    Particles int
}
func main()  {
    //Create variables
    //Author structure
    a1 :=  Author{
        name:       "Moana",
        branch:     "CSE",
        language:  "Python",
        Particles: 38,
    }
    a2 :=  Author{
        name:       "Moana",
        branch:     "CSE",
        language:  "Python",
        Particles: 38,
    }
    a3 :=  Author{
        name:       "Dona",
        branch:     "CSE",
        language:  "Python",
        Particles: 38,
    }
    //use == operator to check a1With a2whether equal
    if  a1 ==  a2 {
        fmt.Println("Variable a1Equal to variable a2)
    } else {
        fmt.Println("Variable a1Not equal to variable a2)
    }
    if  a2 ==  a3 {
        fmt.Println("Variable a2Equal to variable a3)
    } else {
        fmt.Println("Variable a2Not equal to variable a3)
    }
}

Output:

Variable a1Equal to variable a2
Variable a2Not equal to variable a3

Using DeepEqual() method to compare if structures are equal

//Using DeepEqual() method
package main
import (
    "fmt"
    "reflect"
)
//Create a structure
type Author struct {
    name       string
    branch     string
    language  string
    Particles int
}
func main()  {
    //Define variables
    //Author structure
    a1 :=  Author{
        name:       "Soana",
        branch:     "CSE",
        language:  "Perl",
        Particles: 48,
    }
    a2 :=  Author{
        name:       "Soana",
        branch:     "CSE",
        language:  "Perl",
        Particles: 48,
    }
    a3 :=  Author{
        name:       "Dia",
        branch:     "CSE",
        language:  "Perl",
        Particles: 48,
    }
    //compare a1and a2, using Deep Equal() method
    fmt.Println("a1Equal to a2question mark:  ",  reflect.DeepEqual(a1,  a2))
    //compare a2and a3, using Deep Equal() method
    fmt.Println("a2Equal to a3question mark:  ",  reflect.DeepEqual(a2,  a3))
}

Output:

a1Equal to a2question mark:  true
a2Equal to a3Yes:  false