[英]i am trying to put a slice of structs in a struct in golang
我正在嘗試在 golang 中放置一個未知長度的切片,但我不斷收到錯誤消息
這是結構
type Table struct {
ID string `json:"id"`
Name string `json:"name"`
Year int `json:"year"`
Grades []struct {
subject string `json:"first"`
Grade int `json:"last"`
} `json:"grades"`
}
我試圖分離結構,但我沒有改變問題。
這是這個結構的實現
var tables []Table
func main() {
tables = append(tables, Table{ID: "1", Name: "Yair", Year: 2022, Grades: {Grade{subject: "math", number: 95}, {subject: "heb", number: 95}}})
}
package main
type Grade struct {
Subject string `json:"first"`
Number int `json:"last"`
}
type Table struct {
ID string `json:"id"`
Name string `json:"name"`
Year int `json:"year"`
Grades []Grade `json:"grades"`
}
var tables []Table
func main() {
tables = append(tables, Table{ID: "1", Name: "Yair", Year: 2022, Grades: []Grade{Grade{Subject: "math", Number: 95}, {Subject: "heb", Number: 95}}})
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.