1

Given the following:

var positionTitles []string
var positionRelationships []string
var positionInstitutions []string

positionTitles = ["Director" "Provost" "Assistant Provost"]
positionRelationships = ["Tenured Professor" "Lecturer" "Adjunct Professor"]
positionInstitutions = ["UCSC" "UCB" "USC"]

How would I construct an array that looks like such:

Positions :=
 [{
   PositionTitle: "Director",
   PositionRelationships: "Tenured Professor",
   PositionInstitution: "UCSC",
  },
  {
   PositionTitle: "Provost",
   PositionRelationships: "Lecturer",
   PositionInstitution: "UCB",
  },
  {
   PositionTitle: "Assistant Provost",
   PositionRelationships: "Adjunct Professor",
   PositionInstitution: "USC",
  }]

The goal is to iterate over the Positions.

Go Playground I've started: http://play.golang.org/p/za_9U7eHHT

2 Answers 2

2

You can create a type that would hold all the pieces and iterate over the slices such that

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],
    }
}

However, if you need it only to iterate, you don't need to create a type. See body of the for.

https://play.golang.org/p/1P604WWRGd

Sign up to request clarification or add additional context in comments.

Comments

0

I would create a Position struct containing the informations you need :

type Position struct {
PositionTitle         string
PositionRelationships string
PositionInstitution   string
}

and create an array (or slice) of those structs to iterate over them. Here is a working example : http://play.golang.org/p/s02zfeNJ63

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.