我在列表中存储了一组字符串.我遍历列表以与字符串进行比较"[the]".
当我使用该函数时strings.EqualFold,它会出现此错误:
不能在函数参数中使用e.Value(type interface {})作为类型字符串:need type assertion
代码如下:
for e := l.Front(); e != nil; e = e.Next() {
if(strings.EqualFold("[the]", e.Value)){
count++
}
}
Run Code Online (Sandbox Code Playgroud)
由于Go的链表实现使用空interface{}来存储列表中的值,因此您必须使用类型断言,如错误指示访问您的值.
因此,如果string在列表中存储a ,则从列表中检索值时,必须键入断言值为字符串.
for e := l.Front(); e != nil; e = e.Next() {
if(strings.EqualFold("[the]", e.Value.(string))){
count++
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
438 次 |
| 最近记录: |