我有結(jié)構(gòu),結(jié)構(gòu)如下: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我想獲取字段“消息”和“狀態(tài)”以發(fā)送回響應(yīng)。我的問(wèn)題是如何初始化結(jié)構(gòu)并訪問(wèn)其字段?但是我犯了一個(gè)錯(cuò)誤。
1 回答

largeQ
TA貢獻(xiàn)2039條經(jīng)驗(yàn) 獲得超8個(gè)贊
這可能會(huì)有所幫助。
注意。我的例子是一個(gè)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 關(guān)注
- 103 瀏覽
添加回答
舉報(bào)
0/150
提交
取消