在切片中访问对象的方法会引发恐慌。

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

Accessing method of an object within a slice throws panic

问题

我正在尝试创建一个实现接口的结构体,以便可以通过包代码从外部访问。但是当一个对象包含在切片中时,一些在接口中定义的结构体方法却丢失了。如果对象在切片之外,它就能正常工作。

package RGTree

import (
	"fmt"
	"testing"
)

type RGTree interface {
	GetParent() RGTree
	SetParent(RGTree)
	GetID() int
	GetParentID() int
}

type ItemTest struct {
	RGTree
	ID       int
	ParentId int
}

func (it ItemTest) SetParent(item RGTree) {

}

func (it ItemTest) GetParent() RGTree {
	return it
}

func (it ItemTest) GetId() int {
	return it.ID
}

func (it ItemTest) GetParentId() int {
	return it.ParentId
}

func TestMakeTreeMap(t *testing.T) {
	var plain []RGTree

	root := ItemTest{ID: 1}

	plain = append(plain, root)
	plain = append(plain, ItemTest{ID: 2, ParentId: 1})
	plain = append(plain, ItemTest{ID: 3, ParentId: 1})
	plain = append(plain, ItemTest{ID: 4, ParentId: 2})
	plain = append(plain, ItemTest{ID: 5, ParentId: 2})
	plain = append(plain, ItemTest{ID: 6, ParentId: 2})
	plain = append(plain, ItemTest{ID: 7, ParentId: 3})
	plain = append(plain, ItemTest{ID: 8, ParentId: 3})
	plain = append(plain, ItemTest{ID: 9, ParentId: 4})
	plain = append(plain, ItemTest{ID: 10, ParentId: 4})

	fmt.Println(plain[0].GetID())

    /*
        panic: runtime error: invalid memory address or nil pointer dereference 
        [recovered]
        panic: runtime error: invalid memory address or nil pointer dereference
        [signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x4f707e]

     */

	//fmt.Println(MakeTree(root, plain))
}
英文:

I'm trying to make a struct that implementing an interface in order to be accessible by the package code from outside. Though somehow the methods of the struct that was defined in the interface and defined for the struct are missing when an object contains in a slice. If object is outside the slice it works fine.

package RGTree
import (
"fmt"
"testing"
)
type RGTree interface {
GetParent () RGTree
SetParent (RGTree)
GetID () int
GetParentID () int
}
type ItemTest struct {
RGTree
ID int
ParentId int
}
func (it ItemTest) SetParent(item RGTree) {
}
func (it ItemTest) GetParent() RGTree {
return it
}
func (it ItemTest) GetId() int {
return it.ID
}
func (it ItemTest) GetParentId() int {
return it.ParentId
}
func TestMakeTreeMap(t *testing.T) {
var plain []RGTree
root := ItemTest{ID: 1}
plain = append(plain, root)
plain = append(plain, ItemTest{ID: 2, ParentId: 1})
plain = append(plain, ItemTest{ID: 3, ParentId: 1})
plain = append(plain, ItemTest{ID: 4, ParentId: 2})
plain = append(plain, ItemTest{ID: 5, ParentId: 2})
plain = append(plain, ItemTest{ID: 6, ParentId: 2})
plain = append(plain, ItemTest{ID: 7, ParentId: 3})
plain = append(plain, ItemTest{ID: 8, ParentId: 3})
plain = append(plain, ItemTest{ID: 9, ParentId: 4})
plain = append(plain, ItemTest{ID: 10, ParentId: 4})
fmt.Println(plain[0].GetID())
/*
panic: runtime error: invalid memory address or nil pointer dereference 
[recovered]
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x4f707e]
*/
//fmt.Println(MakeTree(root, plain))
}

答案1

得分: 2

根据mkopriva的建议:

package main

import (
	"fmt"
)

type RGTree interface {
	GetParent() RGTree
	SetParent(RGTree)
	GetID() int
	GetParentID() int
}

type ItemTest struct {
	ID       int
	ParentId int
}

func (it ItemTest) SetParent(item RGTree) {

}

func (it ItemTest) GetParent() RGTree {
	return it
}

func (it ItemTest) GetID() int {
	return it.ID
}

func (it ItemTest) GetParentID() int {
	return it.ParentId
}

func main() {
	var plain []RGTree

	root := ItemTest{ID: 1}

	plain = append(plain, root)
	plain = append(plain, ItemTest{ID: 2, ParentId: 1})
	plain = append(plain, ItemTest{ID: 3, ParentId: 1})
	plain = append(plain, ItemTest{ID: 4, ParentId: 2})
	plain = append(plain, ItemTest{ID: 5, ParentId: 2})
	plain = append(plain, ItemTest{ID: 6, ParentId: 2})
	plain = append(plain, ItemTest{ID: 7, ParentId: 3})
	plain = append(plain, ItemTest{ID: 8, ParentId: 3})
	plain = append(plain, ItemTest{ID: 9, ParentId: 4})
	plain = append(plain, ItemTest{ID: 10, ParentId: 4})

	fmt.Println(plain[0].GetID())

	/*
	   panic: runtime error: invalid memory address or nil pointer dereference
	   [recovered]
	   panic: runtime error: invalid memory address or nil pointer dereference
	   [signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x4f707e]

	*/

	//fmt.Println(MakeTree(root, plain))
}

输出:

1
英文:

Based on suggestion of mkopriva :

package main
import (
"fmt"
)
type RGTree interface {
GetParent() RGTree
SetParent(RGTree)
GetID() int
GetParentID() int
}
type ItemTest struct {
ID       int
ParentId int
}
func (it ItemTest) SetParent(item RGTree) {
}
func (it ItemTest) GetParent() RGTree {
return it
}
func (it ItemTest) GetID() int {
return it.ID
}
func (it ItemTest) GetParentID() int {
return it.ParentId
}
func main() {
var plain []RGTree
root := ItemTest{ID: 1}
plain = append(plain, root)
plain = append(plain, ItemTest{ID: 2, ParentId: 1})
plain = append(plain, ItemTest{ID: 3, ParentId: 1})
plain = append(plain, ItemTest{ID: 4, ParentId: 2})
plain = append(plain, ItemTest{ID: 5, ParentId: 2})
plain = append(plain, ItemTest{ID: 6, ParentId: 2})
plain = append(plain, ItemTest{ID: 7, ParentId: 3})
plain = append(plain, ItemTest{ID: 8, ParentId: 3})
plain = append(plain, ItemTest{ID: 9, ParentId: 4})
plain = append(plain, ItemTest{ID: 10, ParentId: 4})
fmt.Println(plain[0].GetID())
/*
panic: runtime error: invalid memory address or nil pointer dereference
[recovered]
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x4f707e]
*/
//fmt.Println(MakeTree(root, plain))
}

Output:

1

huangapple
  • 本文由 发表于 2021年7月5日 12:57:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/68250892.html
匿名

发表评论

匿名网友

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

确定