Why am I getting "interface conversion: interface is int32" for converting a list integer element to a string?

huangapple go评论87阅读模式
英文:

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 int32s, 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))
}

huangapple
  • 本文由 发表于 2015年7月19日 12:30:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/31498004.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定