<p>I have a working template that shows the "role" a person plays in an organization but that same person may have more than one role. I would like to insert the word "and" between his two roles using an "if" conditional if that's the proper method.</p>
<p>This is what I have:</p>
<pre><code>Role: []string {
"Architect",
"Engineer",
}
{{range .Role}}<span>{{.}}</span>{{end}}
</code></pre>
<p>I tried doing this:</p>
<pre><code>{{range .Role}}<span>{{.}}</span>{{if .}}and{{end}}{{end}}
</code></pre>
<p>but that adds "and" after both roles.</p>
<p>How do I get it to put "and" only between each role when there's more than one?</p>
<hr/>**评论:**<br/><br/>egonelbre: <pre><p><a href="https://play.golang.org/p/hDmc2rszJ-" rel="nofollow">https://play.golang.org/p/hDmc2rszJ-</a></p></pre>dhdfdh: <pre><p>Not quite but I see where you're going. You show </p>
<pre><code>roles := []string{
"Architect",
"Engineer",
}
</code></pre>
<p>which may be why I get </p>
<blockquote>
<p>at <.>: range can't iterate over {[Architect Engineer] company-name 2014}</p>
</blockquote>
<p>Possibly because "Role" is part of an outer slice? </p>
<pre><code>People: []AllPeople {
Role: []string {
"Architect",
"Engineer",
}
}
</code></pre></pre>IntellectualReserve: <pre><p>Try posting your revision on the playground?</p></pre>dhdfdh: <pre><p>I should really remember to do that when I ask these questions. Working on it now but getting a simplified version is ... difficult.</p></pre>mc_hammerd: <pre><p>yea i think that works, you just have to put another loop for '.Role'</p>
<p>ala <a href="https://play.golang.org/p/YlQPrEjfp6" rel="nofollow">https://play.golang.org/p/YlQPrEjfp6</a></p></pre>dhdfdh: <pre><p>You're a lot closer. My code may need adjusting for your example. The output now is</p>
<blockquote>
<p>Architect Architect Engineer and Engineer</p>
</blockquote>
<p>EDIT: Whoops! It works. I just needed to remove one line of my code. Thanks!</p></pre>aarondl: <pre><p><a href="https://play.golang.org/p/ad_RpNApZq" rel="nofollow">https://play.golang.org/p/ad_RpNApZq</a></p>
<p>Alternative way of doing the same.</p></pre>dhdfdh: <pre><p>Cool. Thanks.</p></pre>