我有结构,结构如下:package mainimport "fmt"type Data [][]struct { Message string `json:"message"` Status string `json:"status"`}func main() { d := Data{ {{message, "Halo"}, {status, "Active"}}, } fmt.Println(d)}$ go run arr.go# command-line-arguments.\arr.go:12:5: undefined: message.\arr.go:12:24: undefined: status我想获取字段“消息”和“状态”以发送回响应。我的问题是如何初始化结构并访问其字段?但是我犯了一个错误。
1 回答
largeQ
TA贡献2039条经验 获得超8个赞
这可能会有所帮助。
注意。我的例子是一个2x2维度,你可以有任何维度。
package main
import "fmt"
type Data [][]struct {
Message string `json:"message"`
Status string `json:"status"`
}
func main() {
d := Data{
{{"message1", "one"},{"message2", "two"}},
{{"message3", "three"},{"message4", "four"}},
}
fmt.Println(d)
}
输出。
[[{message1 one} {message2 two}] [{message3 three} {message4 four}]]
- 1 回答
- 0 关注
- 140 浏览
添加回答
举报
0/150
提交
取消
