Functions are fields in Golang
In Golang , you can define functions as fields in a struct. This feature allows you to bind behavior (methods) directly to data types, allowing for more organized and encapsulated management of data and related operations.

For example:
package main import "fmt" // Định nghĩa một cấu trúc chưa hàm làm trương type Person struct { Name string Greet func() string } func main() { person := Person{ Name: "A", } // Gán một chức năng cho trường Chào hỏi sau khi người được xác định person.Greet = func() string { return "Hello, " + person.Name } // Gọi trường hàm fmt.Println(person.Greet()) }
Syntax of function as field in Golang
type StructName struct { Field1 FieldType FunctionField func() ReturnType }
Structure with methods as function fields
You can also define a struct method that acts as a function field. This allows the struct to have behavior associated directly with it.
Syntax
type StructName struct { Field1 FieldType MethodField func() ReturnType }
For example:
package main import "fmt" type Person struct { Name string Greet func() string } func main() { person := Person{ Name: "A", } // Gán chức năng chào hỏi sau khi người đó được xác định person.Greet = func() string { return "Hello, " + person.Name } // Gọi trường hàm fmt.Println(person.Greet()) }
Result:
Hello, A
Structure with Parameter Function Field
You can define a function field that accepts parameters, providing more flexibility in how the function behaves.
Syntax
type StructName struct { Field1 FieldType MethodField func(param1 ParamType) ReturnType }
For example:
package main import "fmt" type Person struct { Name string Greet func(string) string } func main() { person := Person{ Name: "B", } // Gán hàm greet sau khi xác định được người person.Greet = func(greeting string) string { return greeting + ", " + person.Name } // Gọi trường hàm bằng 1 tham số fmt.Println(person.Greet("Hi")) }
Result:
Hi, B
Structure with multi-function fields
You can also define multiple function fields in a single struct to encapsulate different behaviors.
Syntax
type StructName struct { Field1 FieldType MethodField1 func() ReturnType MethodField2 func(param1 ParamType) ReturnType }
For example:
package main import "fmt" type Person struct { Name string Greet func(string) string Farewell func() string } func main() { person := Person{ Name: "C", } // Gane hàm greet và farewell sau khi xác định người person.Greet = func(greeting string) string { return greeting + ", " + person.Name } person.Farewell = func() string { return "Goodbye, " + person.Name } // Gọi các trường hàm fmt.Println(person.Greet("Hello")) fmt.Println(person.Farewell()) }
Result:
Hello, C Goodbye, C
Update 26 May 2025
You should read it
- Anonymous structures and fields in Golang
- What is GoLang? How to install GoLang on Windows 10
- What is Golang? Things you need to know about Golang programming language
- Difference between Go and C++
- What is Golang? Why should you use Golang language?
- Structural equality in Golang
- What is the difference between Go and Java?
- How to create basic program in Golang
- How to Install Go on Windows
- How to implement a graph data structure in Golang
- Difference between Go and Python
- Malware that specializes in eavesdropping and sabotage is discovered hiding on Telegram
Maybe you are interested
Steam finally has a native app for Macs running Apple M-series chips What is SgrmBroker.exe? Is SgrmBroker.exe safe? 10 reasons to be more generous with Microsoft 10 reasons threaten IE's hegemony 10 reasons to use iPad 3G instead of Wifi Maybe the Solar System 2.0 is not the 'cradle of nurturing life' as NASA hopes