Skip to content

Instantly share code, notes, and snippets.

@yalue
Last active March 26, 2017 12:49
Show Gist options
  • Save yalue/f49fbe8d5f17621287204190c67091f7 to your computer and use it in GitHub Desktop.
Save yalue/f49fbe8d5f17621287204190c67091f7 to your computer and use it in GitHub Desktop.
Go question
package main
import (
"fmt"
)
type type1 struct {
number int
}
func (p *type1) String() string {
return fmt.Sprintf("Type 1: %d", p.number)
}
type type2 struct {
type1
message string
}
func (p *type2) String() string {
return fmt.Sprintf("Type 2: %s, %d", p.message, p.number)
}
type type3 type2
func main() {
value := &type2{
type1: type1{
number: 1337,
},
message: "Hi there",
}
// Why does this print type1's String() rather than type2's?
fmt.Printf("My value: %s\n", (*type3)(value))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment