Skip to content

Instantly share code, notes, and snippets.

@afreeland
Created April 5, 2018 12:54
Show Gist options
  • Save afreeland/f805addd64e9b5c2c875865e3d7eb624 to your computer and use it in GitHub Desktop.
Save afreeland/f805addd64e9b5c2c875865e3d7eb624 to your computer and use it in GitHub Desktop.
Go: struct composition
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