Function as a Field in Golang Structure
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.
Function is a user-defined type in the Go language, you are able to construct a function field in the Go structure. We can also use an anonymous function to construct a function field in the Go structure.
In Golang, the func keyword is used to declare a function. Golang function has a name,, a list of input arguments separated by commas and their types, the outcome type(s), and a body. A function's input parameters and return type(s) are optional. Without any input or output, a function can be declared.
Example
// Go program to illustrate the function
// as a field in Go structure
package main
import "fmt"
// Finalsalary of function type
type Finalsalary func(int, int) int
// Creating structure
type Employee struct {
name string
gender string
Mproject int
Pay int
// Function as a field
salary Finalsalary
}
// Main method
func main() {
// Initializing the fields
// of the structure
result := Employee{
name: "Aditya",
gender: "Male",
Mproject: 10,
Pay: 5000,
salary: func(Ma int, pay int) int {
return Ma * pay
},
}
// Display values
fmt.Println("Employee Name: ", result.name)
fmt.Println("Gender: ", result.gender)
fmt.Println("Total number of projects on which a employee work in May: ", result.Mproject)
fmt.Println("Per project pay: ", result.Pay)
fmt.Println("Total salary: ", result.salary(result.Mproject, result.Pay))
}
Output
Employee Name: Aditya
Gender: Male
Total number of projects on which employee work in May: 10
Per project pay: 5000
Total salary: 50000
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 the 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 the state in any way. As per the mathematical definition of a function, the output values will always be the same, provided some input parameters. On the other hand, methods are inextricably related to the type they are attached to, as they define and use their behavior.
In Golang, how do you declare a function?
In Golang, the func keyword is used to declare a function. Golang function has a name, a list of input arguments separated by commas and their types, the outcome type(s), and a body. A function's input parameters and return type(s) are optional. Without any input or output, a function can be declared.
In Golang, what is function?
A function is a collection of statements in a program that are used to complete a particular activity. A function, at its most basic level, takes an input and returns an output. You can use functions to combine multiple code blocks into a single component.
Conclusion
This article extensively discussed function as a Field in Golang Structure
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.
You can also consider our Online Coding Courses such as the DSA in Python, C++ DSA Course, DSA in Java Course to give your career an edge over others.
Happy Reading!