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 = ▵{} if tri3 == tri4 { fmt.Println("True") } else { fmt.Println("False") } } |
Output:
False False
Recommended Posts:
- Golang program that uses structs as map keys
- Different ways to compare Strings in Golang
- Promoted Fields in Golang Structure
- strings.Fields() Function in Golang With Examples
- How to Access Interface Fields in Golang?
- How to detect the duplication of values of input fields ?
- How to compare times in Golang?
- How to compare two slices of bytes in Golang?
- strings.Compare() Function in Golang with Examples
- Compare Println vs Printf in Golang with Examples
- How to Compare Equality of Struct, Slice and Map in Golang?
- Mathematics | Rings, Integral domains and Fields
- Sort array of objects by object fields in PHP
- Scala | Controlling visibility of constructor fields
- Difference between private keyword and private fields in TypeScript
- How to add input fields dynamically on button click in AngularJS ?
- Different ways to concatenate two strings in Golang
- Different Ways to Find the Type of an Object in Golang
- Different Ways to Convert the Boolean Type in String in Golang
- Different Ways to Convert an Integer Variable to String in Golang
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.

