I have stored a set of strings in a list. I iterate through the list to compare with the string "[the]"
.
When I use the function strings.EqualFold
, it presents this error:
Cannot use e.Value (type interface {}) as type string in function argument: need type assertion
The code is as follows:
for e := l.Front(); e != nil; e = e.Next() {
if(strings.EqualFold("[the]", e.Value)){
count++
}
}
Since Go's linked list implementation uses an empty interface{}
to store the values in the list, you have to you use type assertion like the error indicates to access your value.
So if you store a string
in the list, when you retrieve the value from the list you have to type assert that the value is a string.
for e := l.Front(); e != nil; e = e.Next() {
if(strings.EqualFold("[the]", e.Value.(string))){
count++
}
}
Swap a "e.Value.(string)" from "e.Value".