Last active
May 30, 2020 13:45
-
-
Save ankur22/f61c6dfb527fc75d3ca2abbf0a22e3e6 to your computer and use it in GitHub Desktop.
Example of a embedded structs where the structs implement the same func
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
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