Skip to content

Instantly share code, notes, and snippets.

@ankur22
Last active May 30, 2020 13:45
Show Gist options
  • Save ankur22/f61c6dfb527fc75d3ca2abbf0a22e3e6 to your computer and use it in GitHub Desktop.
Save ankur22/f61c6dfb527fc75d3ca2abbf0a22e3e6 to your computer and use it in GitHub Desktop.
Example of a embedded structs where the structs implement the same func
type logger struct{}
func (l *logger) do(in string) {
if l.validate(in) {
log.Println(in)
}
}
func (l *logger) validate(in string) bool {
if len(in) == 0 {
return false
}
return true
}
type checker struct{}
func (c *checker) validate(in string) bool {
if len(in) == 0 {
return false
}
return true
}
type loggerAndChecker struct {
*logger
*checker
}
func main() {
var lc loggerAndChecker
in := "Hello World"
// Uncomment below to see compilation error:
// if lc.validate(in) {
// lc.do(in)
// }
lc.do(in)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment