doujiong3146 2018-12-04 13:46
浏览 177
已采纳

for循环中的索引超出范围(重复)

I am trying to compare a string value with the value in a slice, in my case slice is tagsList. I have to do some functionality on this comparison. Please find my below code.

        var taglistlength = len(tagsList)
        var tagFlag bool
        var i int
        var reEmplKey string
        type saveDetails struct {
            BankID           string  `json:"bankID"`    
            LocalGradeDescr  string  `json:"localGradeDescr"`   
            RegularTemporary string  `json:"regularTemporary"`
        }
        var tagsList = make([]saveDetails, 0) 

        reEmplKey = "ID00001"
        tagsList = [{ID00001 Band 9 B PERMANENT}{ID00002 Band 8 C PERMANENT}{ID00003 Band 7 C Temporary}] 

        fmt.Println("taglistlength : ",taglistlength)
        for i = 0; i <= taglistlength; i++{ 
            fmt.Println("tagsList : ",tagsList)
            if (taglistlength == 0){
                tagFlag = true
                fmt.Println("1st condition : ",tagFlag)
            } else if (taglistlength > 0 && tagsList[i].BankID ==reEmplKey){
                tagFlag = false
                fmt.Println("2nd condition : ",tagFlag)
            } else if (taglistlength > 0 && tagsList[i].BankID !=reEmplKey){
                tagFlag = false
                fmt.Println("3rd condition : ",tagFlag)
            }else{
                fmt.Println("error")
            }
        }

        if (tagFlag == true){
         //do some operation
        }

on executing this code i am getting the below error:

    panic: runtime error: index out of range
    panic: runtime error: invalid memory address or nil pointer dereference
    [signal SIGSEGV: segmentation violation code=0x1 addr=0x30 pc=0x8ca606]

It is executing properly for the 1st loop and showing error in the 2nd time loop. Please find my output below:

      taglistlength :  0
      tagsList : []
      1st condition :  true

      taglistlength :  1
      tagsList :  [{1000000 Band 9 B PERMANENT }]
      3rd condition :  false
      tagsList :  [{1000000 Band 9 B PERMANENT }]
      panic: runtime error: index out of range
      panic: runtime error: invalid memory address or nil pointer dereference

Please help me in solving this issue. I know i am doing some silly logical mistake but i couldn't figure out the issue. It will be very much helpful if i could get the working code.

  • 写回答

1条回答 默认 最新

  • duanmu2015 2018-12-04 14:22
    关注

    There are couple of errors in this codeblock, so let's start:

    • First you need to define your taglistlength again after this line tagsList = [{ID00001 Band 9 B PERMANENT}{ID00002 Band 8 C PERMANENT}{ID00003 Band 7 C Temporary}]. I believe that line is in the wrong place because you even define tagList after that line, if so , please edit your question.

    • As @icza stated in the comment you have to do i<taglistLength, not <=. In Go, arrays are 0-indexed , so the index equals to the array length is out of bounds.

    • If you want to be on the safe side you can do this for _,tag:= range tagsList{ ...} . This is a foreach block. Now you can use tag instead of tagList[i].Many people are against using it , but it is still an option. It also prevents out of bound errors.

    • You don't need to check tagListLength>0 in every else if block. First one is enough for your logic. This is just a suggestion.

    So in the end, either use foreach version of golang, or fix your condition to i<taglistLength, and make sure that you are initalizing taglistLength at the correct line.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作