Created
May 30, 2020 14:21
-
-
Save ankur22/4218ae7c10cc401b884c06a7fdec804b to your computer and use it in GitHub Desktop.
Example of embedded structs with overlapping string fields
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 { | |
s string | |
s2 string | |
} | |
func (l *logger) do(in string) { | |
log.Println(in) | |
} | |
type checker struct { | |
s string | |
s2 string | |
} | |
func (c *checker) validate(in string) bool { | |
if len(in) == 0 { | |
return false | |
} | |
return true | |
} | |
type loggerAndChecker struct { | |
*logger | |
*checker | |
s string | |
} | |
func main() { | |
lc := loggerAndChecker{logger: &logger{s2: "Hi"}, checker: &checker{s2: "Bye"}, s: "Hello World"} | |
in := lc.s | |
if lc.validate(in) { | |
lc.do(in) | |
} | |
// Comment out to see compilation error | |
// lc.do(lc.s2) | |
lc.do(lc.logger.s2) | |
lc.do(lc.checker.s2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment