-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b677ff7
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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) | ||
} |