使用Golang的结构体列表(Type Struct List)

huangapple go评论80阅读模式
英文:

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)
}

huangapple
  • 本文由 发表于 2021年6月28日 01:53:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/68153912.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定