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
4 ★ | 1 Vote
You should read it
- 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
May be interested
- Arrays in Golangarrays in golang or go programming language are quite similar to other programming languages. here are the things you need to know about arrays in golang.
- Complex String Slice in Golangthere are two terms slice and composite literal. in this article we will learn how to create a slice and use composite literal in golang.
- How to copy one slice into another slice in Golangto copy one slice into another, go provides a built-in function called copy(). this function allows you to copy elements from one slice (source slice) into another slice (destination slice).
- What is the difference between Go and Java?is golang better than java? is golang harder than java? can golang replace java? this comparison will give you the answer.
- How to create basic program in Golanghello, world! is the first basic program in any programming language. you can write this first program in golang by following the steps below.
- How to Install Go on Windowsgolang can be easily installed on windows. here is a step-by-step guide to install golang on windows.
- How to implement a graph data structure in Golangcharts are one of the essential data structures that you must know as a programmer. let's learn how to create graph/graph data structures in golang !
- How to automate Microsoft Word forms with custom fieldssome jobs require creating letters, forms or other documents on a regular schedule. using custom fields in help documentation can save a lot of time.
- Difference between Go and Pythontwo popular programming language choices today are go and python. in this article, let's explore the differences between golang & python.
- Malware that specializes in eavesdropping and sabotage is discovered hiding on Telegramnew golang malware is using telegram as a 'lever' to spread itself everywhere.