【问题标题】:index out of range when working with slices使用切片时索引超出范围
【发布时间】:2021-06-21 00:55:15
【问题描述】:

我正在尝试将旧版本的 jsonline 转换为新版本(具有不同的结构)。

现在旧文件具有以下结构

{"user": "myname", "uuid": "1242315425", "data": {"Niveau1": ["AUTRE", "RC"], "Niveau2": ["RA06"], "Niveau3": ["RA06_01"]}}

但是,Niveau2 和 Niveau3 并不总是存在,列表的长度也不总是相同。

新文件结构更复杂

{"user": "myname", "uuid": "1242315425","annotation":{"classifications":{"Niveau1":{"labels":[{"value":"AUTRE"}, {"value":"RC"}]}, "Niveau2": {"labels": [{"value":"RA06"}], "Niveau3": {"labels": [{"value":"RA06_01"}]}}}}

到目前为止我所做的是(在以适当的结构解析文件后)以下函数

func convert(oldAnnots []AnnotV1) (newAnnots []AnnotV2) {
    for _, element := range oldAnnots {
            var newAnnot AnnotV2

            newAnnot.User = element.User
            newAnnot.Uuid = element.Uuid
            
            
            if element.Data.Niveau1 != nil {
                for i, annot1 := range element.Data.Niveau1 {
                    newAnnot.Annotation.Classif.Niveau1.Labels[i].Value = annot1
                }
          }
            if element.Data.Niveau2 != nil {
                for j, annot2 := range element.Data.Niveau2 {
                    newAnnot.Annotation.Classif.Niveau2.Labels[j].Value = annot2
                }
          }
            if element.Data.Niveau3 != nil {
                for k, annot3 := range element.Data.Niveau3 {
                    newAnnot.Annotation.Classif.Niveau3.Labels[k].Value = annot3
                }
          }
            newAnnots = append(newAnnots, newAnnot)
    }
    return
}

但是,我收到错误消息,指出索引 [0] 超出了我的切片范围。

panic: runtime error: index out of range [0] with length 0

两种结构的定义如下

type AnnotV1 struct {
    Uuid string `json:"uuid"`
    Data struct {
        Niveau1 []string `json:"Niveau1"`
        Niveau2 []string `json:"Niveau2"`
        Niveau3 []string `json:"Niveau3"`
    } `json:"data"`
    User string `json:"user"`
}

type AnnotV2 struct {
    Uuid string `json:"uuid"`
    Annotation struct {
        Classif struct {
            Niveau1 struct {
                Labels []struct {
                    Value string `json:value`
                } `json:"labels"`
            }
            Niveau2 struct {
                Labels []struct {
                    Value string `json:value`
                } `json:"labels"`
            }
            Niveau3 struct {
                Labels []struct {
                    Value string `json:value`
                } `json:"labels"`
            }
        } `json:"classifications"`
    } `json:"annotation"`
    User string `json:"user"`
}

【问题讨论】:

  • 那么,恐慌发生在哪一行代码上? runtme 告诉你。它还告诉您完整的调用链导致了这种恐慌。你从这些信息中收集到了什么?
  • 好吧,我以为我是由if element.Data.NiveauX != nil 处理的。无论如何,这不会直接导致问题(因为错误已经在 Niveau1 中。如果我添加 annot1 的打印,它会正确打印,但我无法完成分配 newAnnot.Annotation.Classif.Niveau1.Labels[i].Value = annot1
  • 您需要将Labels 切片预先分配到与旧Niveau 切片相同的长度,以便能够对其进行索引。或者你需要使用附加。在这两种情况下,您可能都应该声明一个名为 Label 的类型以使其更容易执行。
  • 请注意,检查一个切片是否有!= nil 可能是不够的:声明s := []int{} 会产生一个非nil 的切片,其元素为零(len(s) == 0)。
  • 谢谢。我通过创建另一个结构 Label 和 make([]Labels, len(NiveauX)) 做到了这一点,但我还是遇到了错误(输入非文字)

标签: go range slice indexoutofboundsexception


【解决方案1】:
type Label struct {
    Value string `json:"value"`
}

type AnnotV2 struct {
    Uuid string `json:"uuid"`
    Annotation struct {
        Classif struct {
            Niveau1 struct {
                Labels []Label `json:"labels"`
            }
            Niveau2 struct {
                Labels []Label `json:"labels"`
            }
            Niveau3 struct {
                Labels []Label `json:"labels"`
            }
        } `json:"classifications"`
    } `json:"annotation"`
    User string `json:"user"`
}

预分配切片

if element.Data.Niveau2 != nil {
    newAnnot.Annotation.Classif.Niveau2.Labels = make([]Label, len(element.Data.Niveau2))
    for j, annot2 := range element.Data.Niveau2 {
        newAnnot.Annotation.Classif.Niveau2.Labels[j].Value = annot2
    }
}

或使用附加

if element.Data.Niveau2 != nil {
    for _, annot2 := range element.Data.Niveau2 {
        newAnnot.Annotation.Classif.Niveau2.Labels = append(newAnnot.Annotation.Classif.Niveau2.Labels, Label{annot2})
    }
}

【讨论】:

  • 非常感谢!
猜你喜欢
  • 2023-03-29
  • 2015-10-30
  • 1970-01-01
  • 1970-01-01
  • 2020-12-01
  • 1970-01-01
  • 2019-05-29
  • 1970-01-01
  • 2013-11-30
相关资源
最近更新 更多