go - 嵌套方法可以访问父级的值吗?
问题描述
我正在尝试创建状态的字符串视图,因此在调用时:
fmt.Printf("%s", panel.State)
它将打印出如下内容:
Hi
⚪️⚪️⚪️⚪️⚪️⚪️⚪️⚪️⚪️⚪️
⚪️⚫️⚪️⚫️⚪️⚪️⚫️⚪️⚪️⚪️
⚪️⚫️⚪️⚫️⚪️⚪️⚪️⚪️⚪️⚪️
⚪️⚫️⚫️⚫️⚪️⚪️⚫️⚪️⚪️⚪️
⚪️⚫️⚪️⚫️⚪️⚪️⚫️⚪️⚪️⚪️
⚪️⚫️⚪️⚫️⚪️⚪️⚫️⚪️⚪️⚪️
⚪️⚪️⚪️⚪️⚪️⚪️⚪️⚪️⚪️⚪️
是否可以在嵌套字段上添加方法,然后可以访问它的父结构,例如:
type Panel struct {
Width int
Height int
Message string
State [][]bool
}
func (state Panel.State) String() string {
line := state.parent.Message + "\n"
for x := 0; x < state.parent.Width; x++ {
for y := 0; y < state.parent.Height; y++ {
cell = state[x][y]
if cell {
line += "⚫️"
} else {
line += "⚪️"
}
}
line += "\n"
}
return line
}
解决方案
推荐阅读
- json - 如何通过主键将嵌套的 JSON 索引连接到 SQL 中的多行
- php - Woocommerce 下拉屏幕,添加到购物车挂钩
- kubernetes - Kubernetes 提供垃圾外部 ip
- logstash - 从谷歌云存储有选择地下载堆栈驱动程序日志
- python - 为什么 Anaconda 发行版不能在 Windows 10 64 位上运行?
- firebase - 部署调度器云函数(function Failed to create function "function_name"--- HTTP Error: 400, The request has errors)
- python - 如何更改图像位深?还有什么其他包替换了不推荐使用的函数 imageop.grey2grey4?
- android - java.nio.file.InvalidPathException: Trailing char < > at index 10: res/raw/?
- c# - 在 C# 中使用 Jquery 正则表达式
- android - 在 Xml 中声明的 Fragment 中的 Fragment