forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example4.go
61 lines (45 loc) · 1.36 KB
/
example4.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// Sample program to show how to declare and use function types.
package main
import "fmt"
// handler represents a function for handling events.
type handler func(string)
// =============================================================================
// data is a struct to bind methods to.
type data struct {
name string
age int
}
// event displays events for this data.
func (d *data) event(message string) {
fmt.Println(d.name, message)
}
// =============================================================================
// event displays global events.
func event(message string) {
fmt.Println(message)
}
// =============================================================================
func main() {
// Declare a variable of type data.
d := data{
name: "Bill",
}
// Use the fireEvent handler that accepts any
// function or method with the right signature.
fireEvent1(d.event)
// Declare a variable of type handler for the global event function.
h := handler(event)
// User the fireEvent handler that accepts
// values of type handler.
fireEvent2(h)
}
// fireEvent1 uses an anonymous function type.
func fireEvent1(f func(string)) {
f("message 1")
}
// fireEvent2 uses a function type.
func fireEvent2(h handler) {
h("message 2")
}