Go struct definition
A struct is defined with the type keyword.
type User struct {
name string
Subject string
marks int
}
The type keyword is used to create a new type. It is followed by the type's name (User). We're going to make a struct with the struct keyword. We have a list of fields inside the curly brackets. A name and a type are assigned to each field.
Promoted Methods
Promoted methods behave similarly to Promoted Fields in the Go structure. 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 methods of that structure (other than nested structure) are promoted methods. Alternatively, prompted methods are those that are implemented by the child structure and are accessible by the parent structure.
Example 1
Now we'll look at a sample code to show how the approach described above for promoted methods works.
// Golang program to illustrate the
// concept of the promoted methods
package main
import "fmt"
// Structure
type details struct {
// Fields of the
// details structure
name string
age int
gender string
pexpenditure int
}
// Nested structure
type student struct {
rollno int
details
}
// Method
func (d details) promotmethod(texpenditure int) int {
return d.pexpenditure * texpenditure
}
func main() {
// Initializing the fields of
// the student structure
values := student{
rollno: 40,
details: details{
name: "Aditya",
age: 22,
gender: "Male",
pexpenditure: 200,
},
}
// Promoted fields of the
// student structure
fmt.Println("Name: ", values.name)
fmt.Println("Age: ", values.age)
fmt.Println("Gender: ", values.gender)
fmt.Println("Per day expenditure: ", values.pexpenditure)
// Promoted method of the
// student structure
fmt.Println("Total Expenditure: ", values.promotmethod(30))
// Normal fields of
// the student structure
fmt.Println("Student Rollno: ", values.rollno)
}
Output
Name: Aditya
Age: 22
Gender: Male
Per day expenditure: 200
Total Expenditure: 6,000
Student Rollno: 40
Example 2
Now we'll look at a sample Golang code where both child and parent structure contain methods of the same names. If a method has the same name but a different type of receiver exists in both the child and parent structures, then both methods are available in the parent structure.
// Golang program to illustrate the
// concept of the promoted methods
package main
import "fmt"
// Structure
type details struct {
// Fields of the
// details structure
name string
age int
gender string
pexpenditure int
}
// Method 1
func (s student) promotmethod(tarticle int) int {
return s.particle * tarticle
}
// Nested structure
type student struct {
particle int
rollno int
details
}
// Method 2
func (d details) promotmethod(texpenditure int) int {
return d.pexpenditure * texpenditure
}
// Main method
func main() {
// Initializing the fields of
// the employee structure
values := student{
rollno: 40,
particle: 5,
details: details{
name: "Aditya",
age: 22,
gender: "Male",
pexpenditure: 200,
},
}
// Promoted fields of
// the student structure
fmt.Println("Name: ", values.name)
fmt.Println("Age: ", values.age)
fmt.Println("Gender: ", values.gender)
fmt.Println("Per day expenditure: ", values.pexpenditure)
// Promoted method of
// the student structure
fmt.Println("Total Expenditure: ", values.promotmethod(30))
// Normal fields of
// the student structure
fmt.Println("Roll no: ", values.rollno)
fmt.Println("Total Articles: ", values.promotmethod(30))
}
Output
Name: Aditya
Age: 22
Gender: Male
Per day expenditure: 200
Total Expenditure: 6,000
Roll no: 40
Total Articles: 150
Frequently Asked Questions
Is it possible to compare structs in Golang?
If two structures are of the same type and have the same values for all fields, Golang allows you to compare them.
In Golang, how do you use methods?
Go language support methods. The only distinction between Go methods and Go functions is that the method has a receiver parameter. The method can access the receiver's properties using receiver argument. The receiver can be either struct or non-struct in this case.
In Golang, what's the difference between a method and a function?
In Golang, functions do not depend on state in any way. As per the mathematical definition of a function, provided some input parameters, the output values will always be the same. On the other hand, methods are inextricably related to the type to which they are attached, as they define and use their behavior.
How do you extend a structure in Golang?
In Golang, we cannot directly extend structs but instead use a concept called composition, where the struct is used to form other objects. So, we can say there is No Inheritance Concept in Golang.
Why is it that Go has nil?
Nil is a zero value in the Go programming language. It is an integer without a value that defaults to 0. The zero value for strings is an empty string, and so on. A pointer with nowhere to point has the value nil.
Conclusion
This article extensively discussed Promoted Methods in Structure in Go.
We hope you have learned something new from this blog. And if you're interested in learning more, see our posts on Structs in Go, Classes and Objects, Exploring the dynamics of Golang, What is Golang, Go Race Condition in Golang. Please vote for our blog to help other ninjas succeed.
Visit our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!
Happy Reading!