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.