英文:
Golang Using Type Struct List
问题
以下是你提供的代码的中文翻译:
package main
import (
"encoding/json"
"fmt"
)
type Student struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
Email string `json:"email"`
Languages []string `json:"languages"`
Profile []Profile
}
type Profile struct {
Username string `json:"username"`
Followers int `json:"followers"`
Grades map[string]string `json:"grades"`
}
func main() {
var John Student
// 定义结构体
John = Student{
Firstname: "John",
Lastname: "Miller",
Email: "johnmiller@gmail.com",
Profile: []Profile{
{
Username: "Miller_267",
Followers: 1988,
Grades: map[string]string{"Education Level": "master", "University": ""},
},
{
Username: "John Miller",
Followers: 1997,
Grades: map[string]string{"Education Level": "master", "University": "Leicsheter University"},
},
},
Languages: []string{"Eng", "Esp"},
}
res, err := json.MarshalIndent(John, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(res), "\n", err)
}
在上述代码中,你的错误是在定义 Profile
结构体时,没有使用 []
将其包裹起来,导致编译错误。你可以将 Profile
的定义修改为以下形式:
type Student struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
Email string `json:"email"`
Languages []string `json:"languages"`
Profile []Profile `json:"profile"`
}
这样就可以正确使用 Struct List
了。
英文:
I wanna using my Struct like List but IDE cannot give a chance for using.
I know, have a syntax problem but I cannot find true syntax.
I don't where is my mistake in code.
What is the correct syntax when using Struct List ?
package main
import (
"encoding/json"
"fmt"
)
type Student struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
Email string `json:"email"`
Languages []string `json:"languages"`
Profile []Profile
}
type Profile struct {
Username string `json:"username"`
Followers int `json:"followers"`
Grades map[string]string `json:"grades"`
}
func main() {
var John Student
// defining struct
John = Student{
Firstname: "John",
Lastname: "Miller",
Email: "johnmiller@gmail.com",
Profile: Profile{
{
Username: "Miller_267",
Followers: 1988,
Grades: map[string]string{"Education Level": "master", "University": ""},
},
{
Username: "John Miller",
Followers: 1997,
Grades: map[string]string{"Education Level": "master", "University": "Leicsheter University"},
},
},
Languages: []string{"Eng", "Esp"},
}
res, err := json.MarshalIndent(John, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(res), "\n", err)
}
In above syntax what is my mistake ?
答案1
得分: 1
你需要在字面量中使用切片,以匹配类型定义:
package main
import (
"encoding/json"
"os"
)
type profile struct {
Followers int
Username string
}
type student struct {
Firstname string
Profile []profile
}
func main() {
john := student{
Profile: []profile{
{1988, "Miller_267"}, {1997, "John Miller"},
},
}
json.NewEncoder(os.Stdout).Encode(john)
}
英文:
You need to use a slice in the literal, to match the type definition:
package main
import (
"encoding/json"
"os"
)
type profile struct {
Followers int
Username string
}
type student struct {
Firstname string
Profile []profile
}
func main() {
john := student{
Profile: []profile{
{1988, "Miller_267"}, {1997, "John Miller"},
},
}
json.NewEncoder(os.Stdout).Encode(john)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论