登录
首页 >  Golang >  Go问答

切片索引越界错误

来源:stackoverflow

时间:2024-02-18 22:45:26 223浏览 收藏

珍惜时间,勤奋学习!今天给大家带来《切片索引越界错误》,正文内容主要涉及到等等,如果你正在学习Golang,或者是对Golang有疑问,欢迎大家关注我!后面我会持续更新相关内容的,希望都能帮到正在学习的大家!

问题内容

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

解决方案


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

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>