I am afraid you are not iterating over the test_slice
array. If you did, your code would look something like this:
package main
import (
"os"
"html/template"
)
const templateString = `{{range $i, $test_slice := .}}
{{$i}}
{{end}}`
func main() {
t, err := template.New("foo").Parse(templateString)
if err != nil {
panic(err)
}
test_slice := []string{"t", "e", "s", "t"}
err = t.Execute(os.Stdout, test_slice)
if err != nil {
panic(err)
}
}
Output:
0
1
2
3
Your code is rather iterating over a map where test_slice
is just one of the values. What you see is the key names of this map, where test_slice
is one of them. To make it work, you should change your template to:
{{range $i, $test_slice := .test_slice}}
{{$i}}
{{end}}
Consider this Playground example: http://play.golang.org/p/are5JNPXt1