鉴于以下内容:
var positionTitles []string
var positionRelationships []string
var positionInstitutions []string
positionTitles = ["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
答案 0 :(得分:2)
您可以创建一个类型来保存所有部分并迭代切片,以便
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
的正文。
答案 1 :(得分:0)
我会创建一个包含所需信息的位置结构:
var menuItems = [ // text is not valid text
{ route: 'test1', text: <div className="sideMainNav User"><i><img src="/static/images/icons/test1.svg" alt="test1" /></i><span>test1 </span></div>, type: MenuItem.Types.LINK, payload: '#/test1' },
{ route: 'test2', text: <div className="sideMainNav"><i><img src="/static/images/icons/test2.svg" alt="test2" /></i><span>test2</span></div>, type: MenuItem.Types.LINK, payload: '#/test2' },
{ route: 'test3', text: <div className="sideMainNav"><i><img src="/static/images/icons/test3.svg" alt="test3" /></i><span>test3</span></div>, type: MenuItem.Types.LINK, payload: '#/test3' },
{ route: 'test4', text: <div style={{margin:'0px -8px',fontWeight:'bold',color:'#58595B',fontSize:'14px'}}>test4</div>, type: MenuItem.Types.LINK, payload: '#/test4' },
{ route: 'test5', text: <div style={{margin:'0px -8px',fontWeight:'bold',color:'#58595B',fontSize:'14px'}}>test5</div>, type: MenuItem.Types.LINK, payload: '#/test5' },
{ route: 'test6', text: <div style={{margin:'0px -8px',fontWeight:'bold',color:'#58595B',fontSize:'14px'}}>test6</div>, type: MenuItem.Types.LINK, payload: '#/' },
{ route: 'test7', text: <div className="sideLogout" onClick={this.logout}>test7</div> },
{ route: 'test8', text: <div className="sidenavPlink"> <a href="#/">test8</a></div> },
{ route: 'test9', text: <div className="sidenavHelplink"> <a href="#/"><span>Help</span><i><img src="/static/images/icons/test9.svg" width="16px"/></i></a></div> }
];
并创建这些结构的数组(或切片)以迭代它们。这是一个工作示例:http://play.golang.org/p/s02zfeNJ63