Created
April 5, 2018 12:54
-
-
Save afreeland/f805addd64e9b5c2c875865e3d7eb624 to your computer and use it in GitHub Desktop.
Go: struct composition
This file contains hidden or 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 main() { | |
fmt.Println("Hello, playground") | |
mp := messagePrint{"foo"} | |
mp.printMessage() | |
// Error with initialization of foo | |
// emp := enhancePrint{"foo2"} | |
// Although this would work but not ideal since people would have to understand internal structure | |
// emp := enhancePrint{ messagePrint{"foo2"}} | |
emp := enhancePrint{} | |
emp.message = "foo2" | |
emp.printMessage() | |
} | |
type messagePrint struct { | |
message string | |
} | |
func (mp *messagePrint) printMessage() { | |
println(mp.message) | |
} | |
type enhancePrint struct { | |
messagePrint | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment