Skip to content

Instantly share code, notes, and snippets.

@dmitshur
Last active August 28, 2017 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitshur/5f9e93c38f6b75421060 to your computer and use it in GitHub Desktop.
Save dmitshur/5f9e93c38f6b75421060 to your computer and use it in GitHub Desktop.
Trying to do reverse range in html/template... Is there a better way?
<ul>
{{/* So simple... */}}
{{range .Commits}}<li>{{.Commit.Message}}</li>
{{end}}
{{/* Is this really the shortest/best way to to do reverse range? */}}
{{range $i, $v := .Commits}}<li>{{(index $.Commits (revIndex $i (len $.Commits))).Commit.Message}}</li>
{{end}}
</ul>
funcMap := template.FuncMap{
"revIndex": func(index, length int) (revIndex int) { return (length - 1) - index },
}
@tutley
Copy link

tutley commented Aug 28, 2017

I don't know if there's a better way, but I used yours and it worked. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment