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

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -