Table of Contents
This updated guide explores functions are fields in golang with practical details, clear explanations, and useful takeaways. In Golang, you can define functions as fields in a struct. This feature lets you bind behavior (methods) directly to data types, allowing for more organized and encapsulated management of data and related operations.

for instance:
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 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 instance:
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 instance:
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 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 instance:
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
FAQ
What is the main takeaway about Functions Are Fields in Golang?
The key is to understand the core idea, compare the available options, and apply the practical guidance that matches your situation.
What should beginners know about Functions Are Fields in Golang?
Start with the essential steps and terminology. Avoid changing advanced settings until you understand how they affect performance, privacy, cost, or compatibility.
How can I use this information in practice?
Follow the relevant section in order, test one change at a time, and confirm the result before moving to the next step or recommendation.
Reader Comments 0
Sign in with email or Google to join the discussion.