我在列表中存储了一组字符串。我遍历列表以与字符串 "[the]"
进行比较。
当我使用函数 strings.EqualFold
时,它出现了这个错误:
Cannot use e.Value (type interface {}) as type string in function argument: need type assertion
代码如下:
for e := l.Front(); e != nil; e = e.Next() {
if(strings.EqualFold("[the]", e.Value)){
count++
}
}
最佳答案
由于 Go 的链表实现使用空的接口(interface){}
来存储列表中的值,因此您必须使用 type assertion如错误指示访问您的值。
因此,如果您在列表中存储一个 string
,当您从列表中检索值时,您必须键入断言该值是一个字符串。
for e := l.Front(); e != nil; e = e.Next() {
if(strings.EqualFold("[the]", e.Value.(string))){
count++
}
}
关于GO:从列表中输入断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28184251/