-1

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.

kostix
  • 51,517
  • 14
  • 93
  • 176
Priyanka
  • 117
  • 1
  • 5
  • 19
  • 5
    Most likely you should only iterate until `i < taglistlength` (not until `i <= taglistlength`). – icza Dec 04 '18 at 13:50
  • Thank you @icza for the response. I will edit my code accordingly. – Priyanka Dec 04 '18 at 14:37
  • 1
    I'm voting to close this question as off-topic because it's too trivial to be considered a real question. Completion of even a basic introductory material such as https://tour.golang.org would have completely obviated the need to ask it. – kostix Dec 04 '18 at 15:08

1 Answers1

0

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.

atakanyenel
  • 1,367
  • 1
  • 15
  • 20
  • Thank you @atayenel. I will do the changes u have suggested and run my code. – Priyanka Dec 04 '18 at 14:38
  • 3
    `Many people are against using it` - who are these people and why are they against using `range`? – Gavin Dec 04 '18 at 15:28
  • @Gavin https://stackoverflow.com/questions/43031988/javascript-efficiency-for-vs-foreach This question argues that `for` is more efficient in JS. I don't want to put this topic on the center of question though. – atakanyenel Dec 04 '18 at 15:32
  • 2
    @atayenel That questions is comparing `for` and `forEach` in JavaScript. You are talking about `for` and `for range` in Go. I don't see the similarities. – Gavin Dec 04 '18 at 15:34
  • Thank you @atayenel. it worked for me. – Priyanka Dec 04 '18 at 15:54