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.

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.