如何访问另一个结构体内部的 Golang 结构体数组?

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

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.

huangapple
  • 本文由 发表于 2016年2月24日 15:47:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/35596164.html
匿名

发表评论

匿名网友

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

确定