[英]i am trying to put a slice of structs in a struct in golang
i am trying to put a slice of an unknown length in golang, but I keep receiving errors我正在尝试在 golang 中放置一个未知长度的切片,但我不断收到错误消息
this is the struct这是结构
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"`
}
I tried to separate the structs but I didn't change the issue.我试图分离结构,但我没有改变问题。
this is the implementation of this struct这是这个结构的实现
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.