Created
October 10, 2019 20:13
-
-
Save packrat386/62c746c211db4c75b0e4432e5329bee6 to your computer and use it in GitHub Desktop.
How to dependency inject for a function in go
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
package main | |
import "fmt" | |
func handle(f func(input string) string) { fmt.Println(f("foo")) } | |
func main() { | |
handle(func(input string) string { | |
return handler(input, &ic{42}, &sc{"bar"}) | |
}) | |
} | |
func handler(input string, i intClient, s stringClient) string { | |
return fmt.Sprintf("%s: %s %d\n", s.String(), input, i.Int()) | |
} | |
type intClient interface { | |
Int() int | |
} | |
type ic struct { | |
ret int | |
} | |
func (i *ic) Int() int { | |
return i.ret | |
} | |
type stringClient interface { | |
String() string | |
} | |
type sc struct { | |
ret string | |
} | |
func (s *sc) String() string { | |
return s.ret | |
} |
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
package main | |
import "fmt" | |
func handle(f func(input string) string) { fmt.Println(f("foo")) } | |
func main() { | |
handle(func(input string) string { | |
return handler(input, inter(42), stringer("bar")) | |
}) | |
} | |
func handler(input string, intreader func() int, stringreader func() string) string { | |
return fmt.Sprintf("%s: %s %d\n", stringreader(), input, intreader()) | |
} | |
func inter(i int) func() int { | |
return func() int { | |
return i | |
} | |
} | |
func stringer(s string) func() string { | |
return func() string { | |
return s | |
} | |
} |
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
package main | |
import "fmt" | |
func handle(f func(input string) string) { fmt.Println(f("foo")) } | |
func main() { | |
handle(NewHandler(42, "bar").DoIt) | |
} | |
type handler { | |
i intClient | |
s stringClient | |
} | |
func (h *handler) DoIt(input string) string { | |
return fmt.Sprintf("%s: %s %d\n", h.s.String(), input, h.i.Int()) | |
} | |
func NewHandler(i int, s string) { | |
return &handler{ | |
i: &ic{i}, | |
s: &sc{s}, | |
} | |
} | |
type intClient interface { | |
Int() int | |
} | |
type ic struct { | |
ret int | |
} | |
func (i *ic) Int() int { | |
return i.ret | |
} | |
type stringClient interface { | |
String() string | |
} | |
type sc struct { | |
ret string | |
} | |
func (s *sc) String() string { | |
return s.ret | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment