在 Golang 中填充结构体的方法

时间:2021-07-26 16:45:24

标签: go struct

我对 Golang 比较陌生。我有一个结构定义为

type Student struct {
   StudentDetails PersonDetials `json:"studentdetails"`
   Subject CourseDetails `json:Subject"`
}

PersonalDetails 结构定义为

type PerosnalDetails {
   FullName Name `json:"fullname"`
   Age int `json:age`
   Adress AddressStruct `json:"Address"` // AdressStruct is another struct with fields such as house number and city
   Father Name `json:"father"`
}

同样,CourseDetails 结构体也由其他结构体、整数和字符串组成。 我想知道的是,填充这些结构的最简单/最简约的方法是什么。

我现在做的是

newStudent = &Student{}
newStudent.PersonalDetails.Fullname.FirstName = 'Robert'
newStudent.PersonalDetials.FullName.LastName = 'stevenson'
newStudent.Age = 28
NewStudent.Father.FirstName = 'Mark'
NewStudent.Father.LastName = 'Stevenson'
etc....

这看起来并不简约。那么,有没有一种简约的方式来做到这一点?

我期待类似的东西

NewStudent = &Student{
    FullName:{Firstaname:"Robert", LastName:"Stevenson"},
    Age: 28,
    Address:{
       HouseNo:14
       City: Newyork
    }
    etc...
}

0 个答案:

没有答案
相关问题