英文:
Why am I getting "interface conversion: interface is int32" for converting a list integer element to a string?
问题
新手学习Go语言...我写了一个程序来移除存储在列表中的重复整数。当我运行removeDuplicates函数的以下测试时,我得到了以下错误,指向linked_test.go中的这一行:testString += strconv.Itoa(e.Value.(int))。为什么会出现这个错误,我该如何修复它?我在testList中存储整数,并使用e.Value获取它们,并使用*.(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
}
英文:
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
}
答案1
得分: 1
rune
被别名为int32
,
for _, c := range dupString {
newList.PushBack(c) // c是一个rune,也就是int32类型
}
在这里推入的是int32
类型的值,而在64位CPU上,int
被别名为int64
,所以一种方法是强制类型转换:
for _, c := range dupString {
newList.PushBack(int(c))
}
英文:
rune
is aliased to int32
,
for _, c := range dupString {
newList.PushBack(c) // c is a rune aka int32
}
is pushing int32
s, while int
is aliased to int64
on 64bit CPUs, so one way to do it is to just force the type:
for _, c := range dupString {
newList.PushBack(int(c))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论