Table of contents
1.
Introduction
2.
The struct
3.
Go struct definition
4.
Promoted Methods
4.1.
Example 1
4.2.
Output
4.3.
Example 2
4.4.
Output
5.
Frequently Asked Questions
5.1.
Is it possible to compare structs in Golang?
5.2.
In Golang, how do you use methods?
5.3.
In Golang, what's the difference between a method and a function?
5.4.
How do you extend a structure in Golang?
5.5.
Why is it that Go has nil?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Promoted Methods in Structure in Go

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

Introduction

Go is a general-purpose programming language with a focus on systems programming. It is strongly typed, garbage-collected, and supports concurrent programming explicitly. Packages are the building blocks of programs, and their attributes allow for efficient dependency management. Google engineers created Go to construct stable and efficient software. 

A standard library, package management, static typing, testing support, and platform independence are among the features of Go. Distributed packages are the foundation of Go's standard library. Go's support for user-based and external package management is referred to as package management. A simple set of commands can be used to publish packages. Static typing is a type system that provides conversions and compatibility while avoiding the difficulties that come with dynamically typed languages. Unit tests can be run in parallel with written code in Go. Furthermore, Go's modular design allows the code to be compiled on practically any platform.

This article will provide you with sufficient knowledge of Promoted Methods in Structure in Go to enable you to go to higher levels of expertise. Before reading this article, you should have a basic understanding of computer programming terms. It will be pretty simple for you to grasp the principles if you have a decent command of C. If that is not the case, you can return here after reading our Introduction to C article.

The struct

A struct is a type that has been specified by the user and has a collection of fields. It's utilized to bring together similar data into a single unit. A lightweight class without inheritance can be likened to a Go struct.

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 GoClasses and ObjectsExploring the dynamics of GolangWhat is GolangGo 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!

 

Live masterclass