如何将自定义结构放入堆栈中,并能够访问所有字段?

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

How to put custom structure to stack and then be able to access all fields?

问题

我有一个名为Foo的结构体,其中包含Field_1和Field_2两个字段。

  1. package foo
  2. type Custom struct {
  3. start_row int
  4. start_column int
  5. move_row int
  6. move_column int
  7. }
  8. type Foo struct {
  9. Field_1 [100]Custom
  10. Field_2 stack.Stack
  11. }

我该如何初始化Foo?类似这样的方式:

  1. new_element := &foo.Foo{[100]foo.Custom{}, stack.Stack{}}

但是我需要将stack指定为foo.Custom结构体的容器,因为我之后需要像这样访问start_row和start_column:

  1. Element := Field_2.Pop()
  2. fmt.Printf("%d \n", Element.start_row)

以下是stack的实现:

  1. package stack
  2. type Stack struct {
  3. top *Element
  4. size int
  5. }
  6. type Element struct {
  7. value interface{}
  8. next *Element
  9. }
  10. // 获取栈的长度
  11. func (s *Stack) Length() int {
  12. return s.size
  13. }
  14. // 将新元素推入栈中
  15. func (s *Stack) Push(value interface{}) {
  16. s.top = &Element{value, s.top}
  17. s.size += 1
  18. }
  19. // 从栈中移除顶部元素并返回其值
  20. // 如果栈为空,则返回nil
  21. func (s *Stack) Pop() (value interface{}) {
  22. if s.size > 0 {
  23. value, s.top = s.top.value, s.top.next
  24. s.size -= 1
  25. return
  26. }
  27. return nil
  28. }
英文:

I have struct Foo with fields Field_1 and Field_2.

  1. package foo
  2. type Custom struct {
  3. start_row int
  4. start_column int
  5. move_row int
  6. move_column int
  7. }
  8. type Foo struct{
  9. Field_1 [100]Custom
  10. Field_2 stack.Stack
  11. }

How can I initialize Foo? Something like this,

  1. new_element := &foo.Foo { [100]foo.Custom{}, stack.Stack {} }

But I need specify stack as container for foo.Custom struct, because I need to access later start_row, start_column like this

  1. Element: = Field_2.Pop()
  2. fmt.Printf("%d \n", Element.start_row)

Here is stack implementation

  1. package stack
  2. type Stack struct {
  3. top *Element
  4. size int
  5. }
  6. type Element struct {
  7. value interface{}
  8. next *Element
  9. }
  10. // Get length of the stack
  11. func (s *Stack) Length() int {
  12. return s.size
  13. }
  14. // Push a new element into the stack
  15. func (s *Stack) Push(value interface{}) {
  16. s.top = &Element{value, s.top}
  17. s.size += 1
  18. }
  19. // Remove the top element from the stack and return value
  20. // If stack is empty return nil
  21. func (s *Stack) Pop() (value interface{}) {
  22. if s.size > 0 {
  23. value, s.top = s.top.value, s.top.next
  24. s.size -= 1
  25. return
  26. }
  27. return nil
  28. }

答案1

得分: 0

几点说明:

  1. Custom 中的所有字段都没有被导出,你不能直接从不同的包中修改它们。

  2. 你不能以那种方式创建 Foo,但是由于它是一个数组,你可以简单地使用 new_element := &foo.Foo{Field_2: Stack{}}

英文:

Few points:

  1. all fields in Custom are not exported, you can't modify them directly from a different package.

  2. You can't create Foo that way, however since it's an array you can simply just use new_element := &foo.Foo{Field_2: Stack{}}.

答案2

得分: 0

当前的stack包的实现中,没有办法强制规定存储在Element结构体内的value的类型。

英文:

With the current implementation of package stack, there is no way to enforce the type of value that gets stored inside struct Element.

huangapple
  • 本文由 发表于 2015年5月9日 06:36:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/30133844.html
匿名

发表评论

匿名网友

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

确定