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

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

Accessing method of an object within a slice throws panic

问题

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

  1. package RGTree
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. type RGTree interface {
  7. GetParent() RGTree
  8. SetParent(RGTree)
  9. GetID() int
  10. GetParentID() int
  11. }
  12. type ItemTest struct {
  13. RGTree
  14. ID int
  15. ParentId int
  16. }
  17. func (it ItemTest) SetParent(item RGTree) {
  18. }
  19. func (it ItemTest) GetParent() RGTree {
  20. return it
  21. }
  22. func (it ItemTest) GetId() int {
  23. return it.ID
  24. }
  25. func (it ItemTest) GetParentId() int {
  26. return it.ParentId
  27. }
  28. func TestMakeTreeMap(t *testing.T) {
  29. var plain []RGTree
  30. root := ItemTest{ID: 1}
  31. plain = append(plain, root)
  32. plain = append(plain, ItemTest{ID: 2, ParentId: 1})
  33. plain = append(plain, ItemTest{ID: 3, ParentId: 1})
  34. plain = append(plain, ItemTest{ID: 4, ParentId: 2})
  35. plain = append(plain, ItemTest{ID: 5, ParentId: 2})
  36. plain = append(plain, ItemTest{ID: 6, ParentId: 2})
  37. plain = append(plain, ItemTest{ID: 7, ParentId: 3})
  38. plain = append(plain, ItemTest{ID: 8, ParentId: 3})
  39. plain = append(plain, ItemTest{ID: 9, ParentId: 4})
  40. plain = append(plain, ItemTest{ID: 10, ParentId: 4})
  41. fmt.Println(plain[0].GetID())
  42. /*
  43. panic: runtime error: invalid memory address or nil pointer dereference
  44. [recovered]
  45. panic: runtime error: invalid memory address or nil pointer dereference
  46. [signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x4f707e]
  47. */
  48. //fmt.Println(MakeTree(root, plain))
  49. }
英文:

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.

  1. package RGTree
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. type RGTree interface {
  7. GetParent () RGTree
  8. SetParent (RGTree)
  9. GetID () int
  10. GetParentID () int
  11. }
  12. type ItemTest struct {
  13. RGTree
  14. ID int
  15. ParentId int
  16. }
  17. func (it ItemTest) SetParent(item RGTree) {
  18. }
  19. func (it ItemTest) GetParent() RGTree {
  20. return it
  21. }
  22. func (it ItemTest) GetId() int {
  23. return it.ID
  24. }
  25. func (it ItemTest) GetParentId() int {
  26. return it.ParentId
  27. }
  28. func TestMakeTreeMap(t *testing.T) {
  29. var plain []RGTree
  30. root := ItemTest{ID: 1}
  31. plain = append(plain, root)
  32. plain = append(plain, ItemTest{ID: 2, ParentId: 1})
  33. plain = append(plain, ItemTest{ID: 3, ParentId: 1})
  34. plain = append(plain, ItemTest{ID: 4, ParentId: 2})
  35. plain = append(plain, ItemTest{ID: 5, ParentId: 2})
  36. plain = append(plain, ItemTest{ID: 6, ParentId: 2})
  37. plain = append(plain, ItemTest{ID: 7, ParentId: 3})
  38. plain = append(plain, ItemTest{ID: 8, ParentId: 3})
  39. plain = append(plain, ItemTest{ID: 9, ParentId: 4})
  40. plain = append(plain, ItemTest{ID: 10, ParentId: 4})
  41. fmt.Println(plain[0].GetID())
  42. /*
  43. panic: runtime error: invalid memory address or nil pointer dereference
  44. [recovered]
  45. panic: runtime error: invalid memory address or nil pointer dereference
  46. [signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x4f707e]
  47. */
  48. //fmt.Println(MakeTree(root, plain))
  49. }

答案1

得分: 2

根据mkopriva的建议:

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type RGTree interface {
  6. GetParent() RGTree
  7. SetParent(RGTree)
  8. GetID() int
  9. GetParentID() int
  10. }
  11. type ItemTest struct {
  12. ID int
  13. ParentId int
  14. }
  15. func (it ItemTest) SetParent(item RGTree) {
  16. }
  17. func (it ItemTest) GetParent() RGTree {
  18. return it
  19. }
  20. func (it ItemTest) GetID() int {
  21. return it.ID
  22. }
  23. func (it ItemTest) GetParentID() int {
  24. return it.ParentId
  25. }
  26. func main() {
  27. var plain []RGTree
  28. root := ItemTest{ID: 1}
  29. plain = append(plain, root)
  30. plain = append(plain, ItemTest{ID: 2, ParentId: 1})
  31. plain = append(plain, ItemTest{ID: 3, ParentId: 1})
  32. plain = append(plain, ItemTest{ID: 4, ParentId: 2})
  33. plain = append(plain, ItemTest{ID: 5, ParentId: 2})
  34. plain = append(plain, ItemTest{ID: 6, ParentId: 2})
  35. plain = append(plain, ItemTest{ID: 7, ParentId: 3})
  36. plain = append(plain, ItemTest{ID: 8, ParentId: 3})
  37. plain = append(plain, ItemTest{ID: 9, ParentId: 4})
  38. plain = append(plain, ItemTest{ID: 10, ParentId: 4})
  39. fmt.Println(plain[0].GetID())
  40. /*
  41. panic: runtime error: invalid memory address or nil pointer dereference
  42. [recovered]
  43. panic: runtime error: invalid memory address or nil pointer dereference
  44. [signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x4f707e]
  45. */
  46. //fmt.Println(MakeTree(root, plain))
  47. }

输出:

  1. 1
英文:

Based on suggestion of mkopriva :

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type RGTree interface {
  6. GetParent() RGTree
  7. SetParent(RGTree)
  8. GetID() int
  9. GetParentID() int
  10. }
  11. type ItemTest struct {
  12. ID int
  13. ParentId int
  14. }
  15. func (it ItemTest) SetParent(item RGTree) {
  16. }
  17. func (it ItemTest) GetParent() RGTree {
  18. return it
  19. }
  20. func (it ItemTest) GetID() int {
  21. return it.ID
  22. }
  23. func (it ItemTest) GetParentID() int {
  24. return it.ParentId
  25. }
  26. func main() {
  27. var plain []RGTree
  28. root := ItemTest{ID: 1}
  29. plain = append(plain, root)
  30. plain = append(plain, ItemTest{ID: 2, ParentId: 1})
  31. plain = append(plain, ItemTest{ID: 3, ParentId: 1})
  32. plain = append(plain, ItemTest{ID: 4, ParentId: 2})
  33. plain = append(plain, ItemTest{ID: 5, ParentId: 2})
  34. plain = append(plain, ItemTest{ID: 6, ParentId: 2})
  35. plain = append(plain, ItemTest{ID: 7, ParentId: 3})
  36. plain = append(plain, ItemTest{ID: 8, ParentId: 3})
  37. plain = append(plain, ItemTest{ID: 9, ParentId: 4})
  38. plain = append(plain, ItemTest{ID: 10, ParentId: 4})
  39. fmt.Println(plain[0].GetID())
  40. /*
  41. panic: runtime error: invalid memory address or nil pointer dereference
  42. [recovered]
  43. panic: runtime error: invalid memory address or nil pointer dereference
  44. [signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x4f707e]
  45. */
  46. //fmt.Println(MakeTree(root, plain))
  47. }

Output:

  1. 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:

确定