如果它具有相同的对象,则在Golang中初始化嵌套的结构定义 [英] Initialize nested struct definition in Golang if it have same objects
本文介绍了如果它具有相同的对象,则在Golang中初始化嵌套的结构定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ b
type AutoGenerated struct {
Accounting [ ]结构{
名字字符串`json:firstName`
LastName字符串`json:lastName`
年龄int`json:age`
}`json: 会计
销售[]结构{
名字字符串`json:firstName`
姓氏字符串`json:lastName`
年龄int`json:年龄`
}`json:sales`
}
我想为它赋值如下: -
data:= AutoGenerated {}
data.Accounting.LastName =Apple2
data.Accounting.FirstName =Apple1
data.Accounting.Age = 20
data .Sales.LastName =Ball2
data.Sales.FirstName =Ball1
data.Sales.Age = 30
但是错误在于 data.Accounting.LastName未定义
虽然相同的代码工作正常
type AutoGenerated struct {
名字字符串`json:firstName`
LastName字符串`json:lastName`
年龄int`json:age`
}
在这里我赋值如下: -
data := AutoGenerated {}
data.LastName =Apple2
data.FirstName =Apple1
data.Age = 20
请勿手动赋值我必须从其他函数中取值。
解决方案<在您的
data
AutoGenerated
类型的对象中, data.Accounting / code>是一小块结构。在使用 data.Accounting
slice之前,您需要初始化它。一个示例用法是:
type AutoGenerated struct {
Accounting [] Account`json:accounting`
Sales [] struct {
FirstName字符串`json:firstName`
LastName字符串`json:lastName`
年龄int`json:age`
}`json:sales`
}
//单独为更好的可读性定义
类型Account结构{
名字字符串`json:firstName`
LastName字符串`json:lastName`
年龄int`json:age`
}
然后为结构赋值:
a:= AutoGenerated {}
a.Accounting = make([] Account,1)//创建适当长度的切片
//向它追加值
a.Accounting = append(a.Accounting,Account {firstname ,lastname,30})
I have struct like this:
type AutoGenerated struct {
Accounting []struct {
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Age int `json:"age"`
} `json:"accounting"`
Sales []struct {
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Age int `json:"age"`
} `json:"sales"`
}
I want to assign values to it like this:-
data := AutoGenerated{}
data.Accounting.LastName = "Apple2"
data.Accounting.FirstName = "Apple1"
data.Accounting.Age = 20
data.Sales.LastName = "Ball2"
data.Sales.FirstName = "Ball1"
data.Sales.Age = 30
But is is giving error which is data.Accounting.LastName undefined
Although same code is working fine for
type AutoGenerated struct {
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Age int `json:"age"`
}
Where I assigned the values like this:-
data := AutoGenerated{}
data.LastName = "Apple2"
data.FirstName = "Apple1"
data.Age = 20
Please don't assign values manually I have to take values from other function.
解决方案 In your object data
of AutoGenerated
type, data.Accounting
is a slice of structs. Before you can use data.Accounting
slice, you'll need to initialize it. An example usage would be:
type AutoGenerated struct {
Accounting []Account `json:"accounting"`
Sales []struct {
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Age int `json:"age"`
} `json:"sales"`
}
// defined separately for better readability
type Account struct {
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Age int `json:"age"`
}
Then to assign values to the struct:
a := AutoGenerated{}
a.Accounting = make([]Account, 1) // create the slice of appropriate length
// append values to it
a.Accounting = append(a.Accounting, Account{"firstname", "lastname", 30})
这篇关于如果它具有相同的对象,则在Golang中初始化嵌套的结构定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文