Example
Let us understand the above concept of promoted fields in structures with the help of an example:
/* Go program to understand the
concept of the promoted fields */
package main
import ("fmt")
// Structure
type Flight struct {
/* Fields of the
Flight structure */
Flight_name string
Flight_no int
}
// Nested structure
type Information struct {
Departure_from string
Arrival_at string
Flight
}
func main() {
/* Initializing the fields of
the Flight structure */
values := Information{
Departure_from : "Ahmedabad",
Arrival_at : "Lucknow",
Flight: Flight{
Flight_name: "6E185",
Flight_no: 185,
},
}
// Promoted fields of the Flight structure
fmt.Println("Flight Name: ", values.Flight_name)
fmt.Println("Flight no: ", values.Flight_no)
/* Normal fields of
the Flight structure */
fmt.Println("Departure : ", values.Departure_from)
fmt.Println("Arrival : ", values.Arrival_at)
}
Output:
Flight Name: 6E185
Flight no: 185
Departure from: Ahmedabad
Arrival at: Lucknow
Explanation:
In the given example, there are two structures mentioned Flight and Information. Here, the normal structure is the Flight structure, and the nested structure contains the Information structure as fields in it, similar to the anonymous fields. Now, the fields of the Flight structure, i.e., Flight_name and Flight_number, are prompted to the Information structure and known as prompted fields. Therefore, now you can directly access them by using Information structures like values. Flight_name and values.Flight_number.
Key Points
Field names can be given automatically with a variable or explicitly as embedded types without field names.
Here are a few more points to take note of while using struct in Golang:
● Within a struct type, field names must be unique.
● An embedded type's field or method can be promoted.
● In the struct, promoted fields cannot be used as field names.
● A literal string tag can be used after a field declaration.
● A capital letter is always used to start an exported struct field.
● We can have function types and interface types as struct fields in addition to basic kinds.
Why use structs in Go
● The first argument is that structs make data relationships clear. When it comes to keeping track of geometric points, having a single object with x and y fields makes more sense than having individual x and y points that can't be associated with one another.
● The second argument is that structs make it easier to work with data blocks. If your software incorporates employee data, keeping all of the employee data in one entity rather than having distinct variables for each piece of data makes more sense.
● Finally, structs shorten function argument lists, making them easier to read.
Frequently Asked Questions
What are structures in Golang programming?
In Golang, a structure (or struct) is a user-defined type that allows elements of potentially diverse styles to be combined into a single class. A struct to represent any real-world thing with a collection of properties or fields.
How do you use structures in Golang?
The keyword ‘struct’ is used for creating structures. Polymorphic values are aggregated and embedded using the struct type. As a result, we can use structures to build a unique style of data.
Is it possible to compare structs in Golang?
Golang allows you to compare two structures that are of the same type and have the same values in all fields.
In Golang, is struct a pointer?
The struct types hold variables of various data types, and the struct variable is used to access the struct's members. In Go, we can also make a struct-type pointer variable.
How do you extend a structure in Golang?
In Golang, we can't directly expand structs; instead, we employ the composition idea, in which the struct is used to create new objects. As a result, we can conclude that Golang has no inheritance concept.
Conclusion
In this article, we have briefly discussed structs in Golang that allows us to combine pieces of various kinds into a single unit. It comes in handy when you need to design a structure that will only be used once.
We hope you found this article helpful. If you wish to delve deep into this topic and other related topics, check our blogs on What is Golang, Structs in Go, Exploring the dynamics of Golang, and Classes and Objects for a better understanding.
Do upvote our blogs if you found them useful and engaging!
Happy Coding!