英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论