首页 > 解决方案 > 如何在golang中制作struct类型的队列

问题描述

type top struct {
    node *tree
    hd   int
}

func (t *bt) topview() {
    if t.root == nil {
        return
    }
    qu := list.New()
    qu.PushBack(top{t.root, 0})
    sample := qu.Front()
    fmt.Println(sample.hd)```

失败,错误 sample.hd undefined(类型 *list.Element 没有字段或方法 hd)

标签: goqueue

解决方案


这就是你需要的

fmt.Println(sample.Value.(top).hd)

您的值 "sample" 是 a list.Element,它是一个包含一些与列表结构相关的隐藏字段的结构,以及一个 field Value,它是您存储在其中的实际数据。 Value是 type interface{},所以你需要做一个类型断言来使用你的结构字段。


推荐阅读