-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
85 lines (71 loc) · 1.44 KB
/
options.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import "fmt"
// These would be defined by the thing to be DI'd
type NeedsInt interface {
getInt() int
}
type NeedsString interface {
getString() string
}
type NeedsFloat interface {
getFloat() float32
}
// Funciton or Library A defines the options it requires
type OptionsA interface {
NeedsInt
NeedsString
NeedsFloat
}
func NeedsA(op OptionsA) {
fmt.Println(op.getInt(), op.getString(), op.getFloat())
// I can call code with a subset of my options
NeedsB(op)
}
// Funciton or Library B defines the options it requires
type OptionsB interface {
NeedsInt
NeedsString
}
func NeedsB(op OptionsB) {
fmt.Println(op.getInt(), op.getString())
// Won't compile
// fmt.Printf(op.getInt(), op.getString(), op.getFloat()
// Won't compile
// NeedsA(op)
}
// My code uses the options
type MyOptions struct {
i int
s string
f float32
}
func (op MyOptions) getInt() int {
return op.i
}
func (op MyOptions) getString() string {
return op.s
}
func (op MyOptions) getFloat() float32 {
return op.f
}
// Some other code uses different options, with a shared subset
type OtherOptions struct {
other_i int
other_s string
}
func (op OtherOptions) getInt() int {
return op.other_i
}
func (op OtherOptions) getString() string {
return op.other_s
}
// If the options I have are sufficient I just pass them it.
func main() {
a := MyOptions{1, "hi", 3.14}
b := OtherOptions{2, "bye"}
NeedsA(a)
NeedsB(a)
// Won't compile
// NeedsA(b)
NeedsB(b)
}