我正在尝试将字符串值与切片中的值进行比较,在我的例子中切片是tagsList。我必须在这个比较上做一些功能。请找到我下面的代码。
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
}
执行此代码时出现以下错误:
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]
它在第一次循环中正确执行并在第二次循环中显示错误。请在下面找到我的输出:
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
请帮我解决这个问题。我知道我犯了一些愚蠢的逻辑错误,但我无法弄清楚问题所在。如果我能得到工作代码,那将非常有帮助。
一只斗牛犬
相关分类