I get an out of range error when I run this code:
Go:
type Ping struct {
Content []aContent
}
type aContent struct {
Type string
Id string
Created_at int64
}
var p Ping
func main() {
f := Ping{Content: []aContent{{Type: "Hello", Id: "asdf"}}}
fmt.Println(f)
fmt.Println(p.Content[0].Created_at) //what's wrong?
}
What's wrong? The code can be found here: http://play.golang.org/p/uZm5LaUuGW
variable p
of type Ping
and its field/property Content
is un-initialized, so when you access the content of the Content
which is a slice
, it throws you that error. Why? Because the value of an uninitialized slice
is nil
. i.e p.Content == []