downcast higher-type to lower

Viewed 2790

Is there beside copying the inner Box value by hand a language feature to down-cast RatedBox into a Box?

type Box struct {
    Name string
}

type RatedBox struct {
    Box
    Points int
}

func main() {
    rated := RatedBox{Box: Box{Name: "foo"}, Points: 10}

    box := Box(rated) // does not work
}

go-playground

// works, but is quite verbose for structs with more members
box := Box{Name: rated.Name}
1 Answers
Related