鉴于以下情况:var positionTitles []stringvar positionRelationships []stringvar positionInstitutions []stringpositionTitles = ["Director" "Provost" "Assistant Provost"]positionRelationships = ["Tenured Professor" "Lecturer" "Adjunct Professor"]positionInstitutions = ["UCSC" "UCB" "USC"]我将如何构造一个看起来像这样的数组:Positions := [{ PositionTitle: "Director", PositionRelationships: "Tenured Professor", PositionInstitution: "UCSC", }, { PositionTitle: "Provost", PositionRelationships: "Lecturer", PositionInstitution: "UCB", }, { PositionTitle: "Assistant Provost", PositionRelationships: "Adjunct Professor", PositionInstitution: "USC", }]目标是迭代位置。去游乐场我已经开始了:http : //play.golang.org/p/za_9U7eHHT
2 回答
噜噜哒
TA贡献1784条经验 获得超7个赞
你可以创建一个类型来保存所有的片段并迭代切片,这样
type Position struct {
Title, Relationship, Institution string
}
positions := make([]Position, len(positionTitles))
for i, title := range positionTitles {
positions[i] = Position{
Title: title,
Relationship: positionRelationships[i],
Institution: positionInstitutions[i],
}
}
但是,如果您只需要它进行迭代,则不需要创建类型。见正文for。
https://play.golang.org/p/1P604WWRGd
DIEA
TA贡献1820条经验 获得超2个赞
我会创建一个 Position 结构,其中包含您需要的信息:
type Position struct {
PositionTitle string
PositionRelationships string
PositionInstitution string
}
并创建这些结构的数组(或切片)以迭代它们。这是一个工作示例:http : //play.golang.org/p/s02zfeNJ63
- 2 回答
- 0 关注
- 160 浏览
添加回答
举报
0/150
提交
取消