New to Go...I wrote a program to remove duplicate integers stored in a list. When I run the following test for the removeDuplicates function, I get the following error which points to this line: testString += strconv.Itoa(e.Value.(int)) in linked_test.go. Why is this and how do I fix it? I store integers in testList and fetch them with e.Value and typecast with .(int).
panic: interface conversion: interface is int32, not int [recovered]
panic: interface conversion: interface is int32, not int
linked_test.go
package linked
import (
"container/list"
"strconv"
"testing"
)
func TestDuplicates(t *testing.T) {
var (
testList = list.New()
exampleList = list.New()
testString string = ""
)
testList.PushBack(1)
testList.PushBack(2)
testList.PushBack(3)
testList.PushBack(2)
exampleList = removeDuplicates(testList)
for e := exampleList.Front(); e.Next() != nil; e = e.Next() {
testString += strconv.Itoa(e.Value.(int))
}
if testString != "123" {
t.Fatalf("removeDuplicates failed")
}
}
linked.go
package linked
import (
"container/list"
"strconv"
"strings"
)
func removeDuplicates(l *list.List) *list.List {
var newList = list.New()
var dupString string = ""
for e := l.Front(); e.Next() != nil; e = e.Next() {
if strings.Index(dupString, strconv.Itoa(e.Value.(int))) == -1 {
dupString += strconv.Itoa(e.Value.(int))
}
}
for _, c := range dupString {
newList.PushBack(c)
}
return newList
}