Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What are Promoted fields in structs in Go?
2.1.
Syntax
3.
Example
3.1.
Output:
3.2.
Explanation:
4.
Key Points 
5.
Why use structs in Go
6.
Frequently Asked Questions
6.1.
What are structures in Golang programming?
6.2.
How do you use structures in Golang?
6.3.
Is it possible to compare structs in Golang?
6.4.
In Golang, is struct a pointer?
6.5.
How do you extend a structure in Golang?
7.
Conclusion
Last Updated: Mar 27, 2024

Promoted fields in structure in Go

Author Ishita mishra
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

One of the top ten most-loved languages to use and the third most popular language on Github (in terms of stars) Go is a multi-purpose programming language with a focus on system programming. It offers explicit support for concurrent programming and is strongly typed and garbage-collected allowing for efficient dependency management. 

In this article, we will study the promoted fields in structure in Golang and understand the importance and requirement of structure in Golang with the help of an example.

What are Promoted fields in structs in Go?

Promoted fields are similar to anonymous fields in the Go structure; the type of the field is the name of the field. We employ this notion in nested structures, when a structure is a field in another structure, by simply adding the structure's name to another structure, which acts as an anonymous field to the nested structure. And the fields of that structure are promoted fields, which are fields that are part of the nested structure. If a field has a similar name as the anonymous structure, nested structure, or parent structure, then that field does not promote; only fields with different names are promoted to the structure.

Syntax

type x struct{
// Fields
}
type y struct{
// Fields of y structure 
x
}
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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 GolangStructs in GoExploring 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!

Live masterclass