英文:
How to access golang Struct Array inside another Struct?
问题
我可以通过以下方式访问嵌套在另一个结构体中的结构体数组的字段:
首先,你可以创建一个Company结构体的实例,并为其中的Process字段分配一个空的ProcessItem切片。然后,你可以通过索引访问ProcessItem切片中的元素,并设置其字段的值。
以下是一个示例代码:
package main
import (
"fmt"
"gopkg.in/mgo.v2/bson"
)
type UserMinimal struct {
Id bson.ObjectId `bson:"_id,omitempty"`
Name string
}
type ProcessItemMinimal struct {
Id bson.ObjectId `bson:"_id,omitempty"`
Process_name string
Processtype int64
}
type ProcessItem struct {
ProcessItemMinimal `bson:",inline"`
Sortorder int64
}
type Company struct {
Id bson.ObjectId `bson:"_id,omitempty"`
Company_name string
Admin UserMinimal
Process []ProcessItem
}
func main() {
company := Company{
Company_name: "ABC Company",
Admin: UserMinimal{
Name: "John Doe",
},
Process: make([]ProcessItem, 3),
}
company.Process[0] = ProcessItem{
ProcessItemMinimal: ProcessItemMinimal{
Process_name: "Enquiry",
Processtype: 0,
},
Sortorder: 0,
}
company.Process[1] = ProcessItem{
ProcessItemMinimal: ProcessItemMinimal{
Process_name: "Converted",
Processtype: 1,
},
Sortorder: 1,
}
company.Process[2] = ProcessItem{
ProcessItemMinimal: ProcessItemMinimal{
Process_name: "Enquiry",
Processtype: 1,
},
Sortorder: 2,
}
fmt.Println(company)
}
在上面的示例中,我们创建了一个Company结构体的实例,并为其中的Process字段分配了一个长度为3的ProcessItem切片。然后,我们通过索引访问ProcessItem切片中的元素,并设置其字段的值。最后,我们打印出Company结构体的内容,以验证数据是否正确存储。
请注意,上述示例中使用了gopkg.in/mgo.v2/bson
包中的bson.ObjectId
类型,你可能需要根据你的实际情况进行调整。
英文:
How can I access the fields of a Struct Array which is inside another Struct?
My Structs as follows:-
type Company struct {
Id bson.ObjectId `bson:"_id,omitempty"`
Company_name string
Admin UserMinimal
Process []ProcessItem
}
type ProcessItemMinimal struct {
Id bson.ObjectId `bson:"_id,omitempty"`
Process_name string
Processtype int64
}
type ProcessItem struct{
ProcessItemMinimal `bson:",inline"`
Sortorder int64
}
I need to store some data in []ProcessItem inside Company struct.
The data will be like this.
ProcessItem[0]=Process_name:"Enquiry",Processtype:0,Sortorder:0}
ProcessItem[1]=Process_name:"Converted",Processtype:1,Sortorder:1}
ProcessItem[2]={Process_name:"Enquiry",Processtype:1,Sortorder:2}
答案1
得分: 2
如果你想使用复合结构字面量来设置数据,可以像这样操作:
company.Process = []ProcessItem{
ProcessItem{
ProcessItemMinimal: ProcessItemMinimal{
Process_name: "Enquiry",
Processtype: 0,
},
Sortorder: 0,
},
ProcessItem{
ProcessItemMinimal: ProcessItemMinimal{
Process_name: "Converted",
Processtype: 1,
},
Sortorder: 1,
},
}
在创建结构字面量时,你必须指定ProcessItemMinimal
,因为根据规范的说明:
提升的字段(Promoted fields)在结构体中的行为类似于普通字段,但不能在结构体的复合字面量中用作字段名。
英文:
If you are looking for setting the data using composite struct literals, it can be done like this:
company.Process = []ProcessItem{
ProcessItem{
ProcessItemMinimal: ProcessItemMinimal{
Process_name: "Enquiry",
Processtype: 0,
},
Sortorder: 0,
},
ProcessItem{
ProcessItemMinimal: ProcessItemMinimal{
Process_name: "Converted",
Processtype: 1,
},
Sortorder: 1,
},
}
You must specify ProcessItemMinimal
when creating a struct literal because, as the Specification says:
> Promoted fields act like ordinary fields of a struct except that they
> cannot be used as field names in composite literals of the struct.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论