5

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?

jpalmieri
  • 1,539
  • 1
  • 15
  • 25
  • Related / possible duplicate of [Golang code to repeat an html code n times](https://stackoverflow.com/questions/46112679/golang-code-to-repeat-an-html-code-n-times/46112802#46112802). – icza Mar 13 '18 at 06:10

1 Answers1

5

I found that seq was what I was looking for:

{{ range seq 100 }}
  <div class='star'></div>
{{ end }}
jpalmieri
  • 1,539
  • 1
  • 15
  • 25