如何在golang中创建struct类型的队列

how to make queue of type struct in golang

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 未定义(类型 *list.Element 没有字段或方法 hd)

这就是你需要的

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

您的值“示例”是 list.Element, which is a struct containing some hidden fields related to the list structure, as well as a field Value, which is the actual data that you stored in it. Value is of type interface{}, so you need to do a type assertion 以使用您的结构字段。