首页 > 解决方案 > 使用切片时索引超出范围

问题描述

我正在尝试将旧版本的 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"`
}

标签: gorangesliceindexoutofboundsexception

解决方案


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})
    }
}

推荐阅读