This is my Go code. Also available at Go Playground
package main
import "fmt"
type App struct {
OneHandler *OneHandler
TwoHandler *TwoHandler
}
type OneHandler struct {
}
type TwoHandler struct {
NestedTwoHandler *NestedTwoHandler
}
type NestedTwoHandler struct {
NestedNestedTwoHandler *NestedNestedTwoHandler
}
type NestedNestedTwoHandler struct {
}
func main() {
app := &App{
OneHandler: new(OneHandler),
TwoHandler: new(TwoHandler),
}
fmt.Println(*app)
fmt.Println(*app.OneHandler)
fmt.Println(*app.TwoHandler)
}
Its output is
{0x19583c 0x1040c128}
{}
{<nil>}
Why is NestedTwoHandler
nil
? I was expecting it to be {some_pointer_location}
with NestedNestedTwoHandler
being {}
. How can I create an empty deep nested struct using new
?