Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 访问嵌入式结构值时索引超出范围错误_Go - Fatal编程技术网

Go 访问嵌入式结构值时索引超出范围错误

Go 访问嵌入式结构值时索引超出范围错误,go,Go,运行此代码时出现超出范围的错误: 去: 怎么了?代码可以在这里找到:类型为Ping的变量p,其字段/属性内容未初始化,因此当您访问内容的内容时,它会抛出该错误。为什么?因为未初始化的切片的值是nil。i、 ep.Content=[]类型为Ping的变量p及其字段/属性Content未初始化,因此当您访问内容的内容时,它会抛出该错误。为什么?因为未初始化的切片的值是nil。i、 ep.Content=[]类型为Ping的变量p及其字段/属性Content未初始化,因此当您访问内容的内容时,它会抛出

运行此代码时出现超出范围的错误:

去:


怎么了?代码可以在这里找到:

类型为
Ping
的变量
p
,其字段/属性
内容
未初始化,因此当您访问
内容
的内容时,它会抛出该错误。为什么?因为未初始化的
切片的值是
nil
。i、 e
p.Content=[]

类型为
Ping
的变量
p
及其字段/属性
Content
未初始化,因此当您访问
内容
的内容时,它会抛出该错误。为什么?因为未初始化的
切片的值是
nil
。i、 e
p.Content=[]

类型为
Ping
的变量
p
及其字段/属性
Content
未初始化,因此当您访问
内容
的内容时,它会抛出该错误。为什么?因为未初始化的
切片的值是
nil
。i、 e
p.Content=[]

类型为
Ping
的变量
p
及其字段/属性
Content
未初始化,因此当您访问
内容
的内容时,它会抛出该错误。为什么?因为未初始化的
切片的值是
nil
。i、 e
p.Content=[]

+1。您正在初始化
f
,但试图打印
p
中的值(未初始化)。+1。您正在初始化
f
,但试图打印
p
中的值(未初始化)。+1。您正在初始化
f
,但试图打印
p
中的值(未初始化)。+1。您正在初始化
f
,但试图打印
p
中的值(未初始化)。
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?
}