I am trying to recognize the golangs, and at the moment I am trying to understand the pointers. I defined three types of structure
type Engine struct {
power int
}
type Tires struct {
number int
}
type Cars struct {
*Engine
Tires
}
As you can see, in the Cars structure I defined an anonymous type pointer * Engine. Look in the main.
func main () {
car := new(Cars)
car.number = 4
car.power = 342
fmt.Println(car)
}
When I try to compile, I have the following errors
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x23bb]
How can I access the power field?
source
share