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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog