What is initialization name of embedded struct field?

Given

type point struct {
	position float64
}
type shapebase struct {
	point
}
type circle struct {
	shapebase
	radius float64
}

how do I initialize a circle using named fields? I have tried every variation I can think of of

p := &circle{radius: 5.0, position: shapebase{point{3.0}}}

and all my attempts get errors similar to

cannot use promoted field shapebase.point.position in struct literal of type circle

p := &Circle{shapebase: shapebase{point{3.0}}, radius: 5.0}

I’d recommend a constructor function:

func NewCircle(pos point, rad float64) *Circle {
    return &Circle{
        shapebase: shapebase{
            point: point,
        },
        radius: rad,
    }
}
2 Likes

Thanks. Okay. I guess the answer to my specific question is that you need the embedded struct name twice: shapebase: shapebase{....

I’d call that a Factory not a Ctor but whatever.

I guess Go does not have true constructors. Not sure I “get” that. How would one design a complex struct and make sure that every instance had “internal integrity” or internal consistency or all required fields filled in before usage? Ditto for Dtors: does automatic garbage collection totally obviate the need for a struct to have a rigorous means to clean up after itself?

Obviously Go was designed by people a lot more knowledgeable than myself but I am not sure I like the divorcing of data and methods. Seems to me to be a step backwards in OOP. Isn’t encapsulation important? Or does Go intend that to be handled at the package level, not the struct level?