pointers - Golang down casting struct -
i'm relatively new @ go , struggling initialising structs.
the classic example
type car struct { wheelcount int } type ferrari struct { car driver string } // initialise ferrari f := ferrari{car{4},"some dude"}
my question is, how *ferrari if have *car created constructor?
i able to following
func newcar(wheels int) *car{ return &car{wheels}; } car := newcar(4); ferrari := ferrari{car,"some dude"}; // error cannot use car (type *car) type car in field value
am approaching problem incorrectly? can 1 dereference car somehow?
the error message pretty clear. can't use car
pointer car
. need either redefine ferrari
embed pointer car
type ferrari struct { *car driver string }
or dereference pointer in literal:
ferrari := ferrari{*car, "some dude"}
Comments
Post a Comment