GO:从列表中输入断言
GO: Type assertion from list
我在列表中存储了一组字符串。我遍历列表以与字符串 "[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++
}
}
从 "e.Value" 换一个 "e.Value.(string)"。
由于 Go 的链表实现使用空 interface{}
来存储列表中的值,因此您必须像错误指示的那样使用 type assertion 来访问您的值。
因此,如果您将 string
存储在列表中,当您从列表中检索值时,您必须键入断言该值是一个字符串。
for e := l.Front(); e != nil; e = e.Next() {
if(strings.EqualFold("[the]", e.Value.(string))){
count++
}
}
我在列表中存储了一组字符串。我遍历列表以与字符串 "[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++
}
}
从 "e.Value" 换一个 "e.Value.(string)"。
由于 Go 的链表实现使用空 interface{}
来存储列表中的值,因此您必须像错误指示的那样使用 type assertion 来访问您的值。
因此,如果您将 string
存储在列表中,当您从列表中检索值时,您必须键入断言该值是一个字符串。
for e := l.Front(); e != nil; e = e.Next() {
if(strings.EqualFold("[the]", e.Value.(string))){
count++
}
}