go - 如何在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)
解决方案
这就是你需要的
fmt.Println(sample.Value.(top).hd)
您的值 "sample" 是 a list.Element
,它是一个包含一些与列表结构相关的隐藏字段的结构,以及一个 field Value
,它是您存储在其中的实际数据。 Value
是 type interface{}
,所以你需要做一个类型断言来使用你的结构字段。
推荐阅读
- python - 使用 Google Sheets API 可以附加多少列是否有限制?
- javascript - 如果菜单链接有子项,如何防止菜单链接在第一次点击时重定向
- html - 如何避免在 HTML 中跨页面拆分 DIV
- xcode - 如何在每个新版本中重置自动递增的内部版本号?
- arrays - C Array:如何移动字符串中的每个字符?
- node.js - 使用 Node.js 和 Electron.js 将 HTML 转为 PDF
- python - 如何为对象中的所有方法起别名?
- javascript - Vuetify 自动完成组件:设置值
- protractor - 预期 '' 等于 '您所做的更改可能不会被保存。
- javascript - 如何使用 javascript 打印棋盘看起来像下面的网格