Go:从二叉搜索树(BST)和接口转换中引发的恐慌错误

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

Go : panic errors from BST and interface conversion

问题

我正在为您翻译以下内容:

我遇到了错误

   panic: 接口转换: 接口是 *main.Node,而不是 *main.Player

出错的代码行是

l := new_linked_list.GetLength()
for i:=0; i < l; i++ {
	fmt.Printf("Removing %v\n", new_linked_list.Pop().(*Player).name)
}

但是在我看来,代码是正确的,如果我将 (*Player) 改为 (*Node),我会得到另一个错误。我应该如何修复这段代码,接口转换的 panic 是什么意思?

谢谢您的帮助。

英文:

http://play.golang.org/p/v1rp9-GN69

I am getting errors

   panic: interface conversion: interface is *main.Node, not *main.Player

from this line

l := new_linked_list.GetLength()
for i:=0; i &lt; l; i++ {
	fmt.Printf(&quot;Removing %v\n&quot;, new_linked_list.Pop().(*Player).name)
}

But it looks correct to me and if I change (*Player) to (*Node), I am getting another error. How should I fix this code and what does it mean by interface conversion panic?

http://play.golang.org/p/v1rp9-GN69

Thanks in advance.

答案1

得分: 1

例如,

package main

import "fmt"

type Node struct {
    value interface{}
    next  *Node
}

func NewNode(input_value interface{}, input_next *Node) *Node {
    return &Node{value: input_value, next: input_next}
}

func (A *Node) GetNext() *Node {
    if A == nil {
        return nil
    }
    return A.next
}

type LinkedList struct {
    head   *Node
    length int
}

func (A *LinkedList) GetLength() int {
    return A.length
}

func NewLinkedList() *LinkedList {
    return new(LinkedList)
}

func (A *LinkedList) Push(input_value interface{}) {
    A.head = NewNode(input_value, A.head)
    A.length++
}

func (A *LinkedList) Pop() interface{} {
    if A.head != nil {
        head_node := A.head
        A.head = A.head.GetNext()
        A.length--
        return head_node
    }
    return nil
}

func (A *LinkedList) eachNode(f func(*Node)) {
    for head_node := A.head; head_node != nil; head_node = head_node.GetNext() {
        f(head_node)
    }
}

func (A *LinkedList) TraverseL(f func(interface{})) {
    A.eachNode(func(input_node *Node) {
        f(input_node.value)
    })
}

func main() {

    type Player struct {
        name   string
        salary int
    }

    new_linked_list := NewLinkedList()
    new_linked_list.Push(&Player{name: "A", salary: 999999})
    new_linked_list.Push(&Player{name: "B", salary: 99999999})
    new_linked_list.Push(&Player{name: "C", salary: 1452})
    new_linked_list.Push(&Player{name: "D", salary: 312412})
    new_linked_list.Push(&Player{name: "E", salary: 214324})
    new_linked_list.Push(&Player{name: "EFFF", salary: 77528})

    fmt.Println(new_linked_list.Pop())

    new_linked_list.TraverseL(func(input_value interface{}) {
        //to tell the type of interface value
        if player, exist := input_value.(*Player); exist {
            fmt.Printf("\t%v: %v\n", player.name, player.salary)
        }
    })

    l := new_linked_list.GetLength()
    for i := 0; i < l; i++ {
        fmt.Printf("Removing %v\n", new_linked_list.Pop().(*Node).value.(*Player).name)
    }
}

输出:

&{0xc0100371e0 0xc0100371c0}
    E: 214324
    D: 312412
    C: 1452
    B: 99999999
    A: 999999
Removing E
Removing D
Removing C
Removing B
Removing A
英文:

For example,

package main

import &quot;fmt&quot;

type Node struct {
	value interface{}
	next  *Node
}

func NewNode(input_value interface{}, input_next *Node) *Node {
	return &amp;Node{value: input_value, next: input_next}
}

func (A *Node) GetNext() *Node {
	if A == nil {
		return nil
	}
	return A.next
}

type LinkedList struct {
	head   *Node
	length int
}

func (A *LinkedList) GetLength() int {
	return A.length
}

func NewLinkedList() *LinkedList {
	return new(LinkedList)
}

func (A *LinkedList) Push(input_value interface{}) {
	A.head = NewNode(input_value, A.head)
	A.length++
}

func (A *LinkedList) Pop() interface{} {
	if A.head != nil {
		head_node := A.head
		A.head = A.head.GetNext()
		A.length--
		return head_node
	}
	return nil
}

func (A *LinkedList) eachNode(f func(*Node)) {
	for head_node := A.head; head_node != nil; head_node = head_node.GetNext() {
		f(head_node)
	}
}

func (A *LinkedList) TraverseL(f func(interface{})) {
	A.eachNode(func(input_node *Node) {
		f(input_node.value)
	})
}

func main() {

	type Player struct {
		name   string
		salary int
	}

	new_linked_list := NewLinkedList()
	new_linked_list.Push(&amp;Player{name: &quot;A&quot;, salary: 999999})
	new_linked_list.Push(&amp;Player{name: &quot;B&quot;, salary: 99999999})
	new_linked_list.Push(&amp;Player{name: &quot;C&quot;, salary: 1452})
	new_linked_list.Push(&amp;Player{name: &quot;D&quot;, salary: 312412})
	new_linked_list.Push(&amp;Player{name: &quot;E&quot;, salary: 214324})
	new_linked_list.Push(&amp;Player{name: &quot;EFFF&quot;, salary: 77528})

	fmt.Println(new_linked_list.Pop())

	new_linked_list.TraverseL(func(input_value interface{}) {
		//to tell the type of interface value
		if player, exist := input_value.(*Player); exist {
			fmt.Printf(&quot;\t%v: %v\n&quot;, player.name, player.salary)
		}
	})

	l := new_linked_list.GetLength()
	for i := 0; i &lt; l; i++ {
		fmt.Printf(&quot;Removing %v\n&quot;, new_linked_list.Pop().(*Node).value.(*Player).name)
	}
}

Output:

&amp;{0xc0100371e0 0xc0100371c0}
	E: 214324
	D: 312412
	C: 1452
	B: 99999999
	A: 999999
Removing E
Removing D
Removing C
Removing B
Removing A

huangapple
  • 本文由 发表于 2013年11月10日 09:28:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/19885243.html
匿名

发表评论

匿名网友

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

确定