Function as a Field in Golang Structure
Last Updated : 06 Nov, 2024
In Go, you can define functions as fields within a structure (struct). This feature allows you to associate behaviors (methods) directly with data types, enabling a more organized and encapsulated way of managing data and its related operations.
Example
package main
import "fmt"
// Define a struct with a function as a field
type Person struct {
Name string
Greet func() string
}
func main() {
person := Person{
Name: "A",
}
// Assign a function to the Greet field after person is defined
person.Greet = func() string {
return "Hello, " + person.Name
}
// Call the function field
fmt.Println(person.Greet())
}
Syntax
type StructName struct {
Field1 FieldType
FunctionField func() ReturnType
}
Struct with Method as a Function Field
You can also define a struct method that acts as a function field. This enables the struct to have behavior directly associated with it.
Syntax
type StructName struct {
Field1 FieldType
MethodField func() ReturnType
}
Example
Go package main import "fmt" type Person struct { Name string Greet func() string } func main() { person := Person{ Name: "A", } // Assign the greet function after the person is defined person.Greet = func() string { return "Hello, " + person.Name } // Call the function field fmt.Println(person.Greet()) }
Struct with Parameterized Function Field
You can define a function field that accepts parameters, providing more flexibility in how the function operates.
Syntax
type StructName struct {
Field1 FieldType
MethodField func(param1 ParamType) ReturnType
}
Example
Go package main import "fmt" type Person struct { Name string Greet func(string) string } func main() { person := Person{ Name: "B", } // Assign the greet function after the person is defined person.Greet = func(greeting string) string { return greeting + ", " + person.Name } // Call the function field with a parameter fmt.Println(person.Greet("Hi")) }
Struct with Multiple Function Fields
You can also define multiple function fields within a single struct to encapsulate various behaviors.
Syntax
type StructName struct {
Field1 FieldType
MethodField1 func() ReturnType
MethodField2 func(param1 ParamType) ReturnType
}
Example
Go package main import "fmt" type Person struct { Name string Greet func(string) string Farewell func() string } func main() { person := Person{ Name: "C", } // Assign the greet and farewell functions after the person is defined person.Greet = func(greeting string) string { return greeting + ", " + person.Name } person.Farewell = func() string { return "Goodbye, " + person.Name } // Call the function fields fmt.Println(person.Greet("Hello")) fmt.Println(person.Farewell()) }
OutputHello, C Goodbye, C
Similar Reads
Anonymous Structure and Field in Golang In Golang, structures (or structs) allow us to group elements of various types into a single unit, which is useful for modeling real-world entities. Anonymous structures are unnamed, temporary structures used for a one-time purpose, while anonymous fields allow embedding fields without names.Example
3 min read
strings.Fields() Function in Golang With Examples strings.Fields() Function in Golang is used to splits the given string around each instance of one or more consecutive white space characters, as defined by unicode.IsSpace, returning a slice of substrings of str or an empty slice if str contains only white space. Syntax: func Fields(str string) []s
2 min read
reflect.Field() Function in Golang with Examples Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Field() Function in Golang is used to get the i'th field of the struct v. To access this function, one needs to i
2 min read
strings.FieldsFunc() Function in Golang With Examples strings.FieldsFunc() Function in Golang is used to splits the given string str at each run of Unicode code points c satisfying f(c) and returns an array of slices of str. Syntax: func FieldsFunc(str string, f func(rune) bool) []string Here, str is the given string, the rune is a built-in type meant
2 min read
Structure Equality in Golang A structure or struct in Golang is a user-defined type, which allows us to create a group of elements of different types into a single unit. Any real-world entity which has some set of properties or fields can be represented as a struct. This concept is generally compared with the classes in object-
3 min read