jpalmieri
jpalmieri

Reputation: 1549

How to iterate a specific number of times in a Go template?

I'm converting an erb template to a Go template (using Hugo), and I'm trying to create a specific number of identical <div>s. Ruby has the times iterator, which worked well in the erb template. I assume there's something similar in Go, but I'm having a hard time finding it.

in Erb:

<% 100.times.each do |i| %>
  <div class='star'></div>
<% end %>

I see that Go templates allow to iterate over a collection using range, but it's unclear how to do the above in a Go template without explicitly creating a collection with 100 items.

This question has some information on iterating a specific number of times in Go, but is not in the context of Go templates: Is there a way to iterate over a range of integers in Golang?

Upvotes: 5

Views: 3348

Answers (1)

jpalmieri
jpalmieri

Reputation: 1549

I found that seq was what I was looking for:

{{ range seq 100 }}
  <div class='star'></div>
{{ end }}

Upvotes: 5

Related Questions