Table of contents
1.
Introduction
2.
Structure in Golang
2.1.
Syntax
3.
What is Structure Equality?
4.
Examples
4.1.
Example1(Using comparable fields)
4.2.
Output
4.3.
Explanation
4.4.
Example2(Using Non-Comparable Fields)
4.5.
Output
4.6.
Explanation
5.
Frequently Asked Questions
5.1.
What is a structure?
5.2.
What do you mean by structure equality?
5.3.
How do you check for struct equality in Go?
5.4.
Does all the field types are comparable in Go?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Structure Equality Concept in Go

Author Aniket Majhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Welcome readers! We hope that you are doing great.

Go(also known as Golang or Go language) is a statically typedcompiled programming, an open-source programming language used for general purposes designed by Google in 2007. The main intention of creating Go was to design a programming language to address criticism of other languages by keeping their characteristics the same. 

Now Go is widely used at Google. Even many other organisations are opting for Go. It is also used in many open-source projects.

If you want to learn more about Go, follow these articles, GoWhat is Golang?, Introduction to Go.

Today, we will be discussing the Structure Equality Concept in Go.

This blog will help you to extend your knowledge about the Go. So, without further ado, let’s start our discussion.

Structure in Golang

structure in Golang is a user-defined type, which allows you to combine a group of elements, possibly of different types, into a single type.

Structures are used to represent different types of records and real-world entities. This concept is somehow similar to Classes in Object-Oriented Programming.

Syntax

If you want to learn more about structs, follow this article Structs in Go.

What is Structure Equality?

As the name suggests, Structure equality is the checking of whether two structures are equal or not. 

In Go, we are allowed to check whether two or more structures are equal or not. Here structures are equal in the sense that their field values are equal.

So, two structures are equal if their field type are comparable and all the field values are equal.
 

Does all the field types are comparable in Go?

No, not all the field types are comparable in Go. There are some of the comparable field types are defined by Go specifications are shown below:

  • boolean
  • numeric
  • pointer
  • string
  • channel
  • Interface type
  • Array (if all the type value is comparable)
  • Structs (if all its fields are comparable)
     

Some of the field types that are not comparable are shown below:

  • Slice
  • Map
  • Function 

Examples

Let’s give some examples,

We can compare the structs using the == operator.

Example1(Using comparable fields)

package main

import "fmt"

// struct A
type A struct {
    x int
    y string
    z int
}


func main() {

    a := A{1 , "Hello World" , 2}
    b := A{1 , "Hello World" , 2}
    c := A{2 , "Hello World" , 10}

    if a == b {
        fmt.Println("a and b are equal")
    }else{
        fmt.Println("a and b are not equal")
    }

    if a == c {
        fmt.Println("a and c are equal")
    } else {
        fmt.Println("a and c are not equal")
    }
}

Output

Explanation

  • In the above example, we compared structures aand using the “==” operator.
  • As and are the same, it prints “a and b are equal”.
  • As and c are the same, it prints “a and c are not equal”.

Example2(Using Non-Comparable Fields)

package main

import "fmt"

// struct A
type A struct {
    x int
    y string
    z []string // non comparable field
}


func main() {

    a := A{x: 1 , y: "Hello World" , z: []string{"Hello" , "World"}}
    b := A{x: 1 , y:"Hello World" ,  z: []string{"Hello" , "World"}}
   
    // it will raise a compilation error
   
    if a == b {
        fmt.Println("a and b are equal")
    }else{
        fmt.Println("a and b are not equal")
    }
}

Output

Explanation

We compared two non-comparable field types, slice in string.slice. It results in a "Compilation error".

Frequently Asked Questions

What is a structure?

A structure is a user-defined type which allows you to combine a group of elements, possibly of different types, into a single type.
 

What do you mean by structure equality?

In Go, we are allowed to check whether two or more structures are equal or not. Here structures are equal in the sense that their field values are equal. This is known as structure equality.
 

How do you check for struct equality in Go?

To check for struct equality in Go, we can use the equal to(==) operator. If both are equal, it returns true otherwise returns false.
 

Does all the field types are comparable in Go?

No, not all the field types are comparable in Go. There are some of the comparable field types are defined by Go specifications are shown below:

  • boolean
  • numeric
  • pointer
  • string
  • channel
  • Interface type
  • Array (if all the type value is comparable)
  • Structs (if all its fields are comparable)
     

Some of the field types that are not comparable are shown below:

  • Slice
  • Map
  • Function 

Conclusion

In this article, we have extensively discussed the Structure Equality Concept in Go.

We started with the basic introduction, then we discussed,

  • About the Structure in Golang
  • What is Structure Equality
  • Examples
     

You can also consider our Online Coding Courses such as the DSA in PythonC++ DSA CourseDSA in Java Course to give your career an edge over others.

 

Happy Reading!

Live masterclass