Skip to content

Instantly share code, notes, and snippets.

@gregworley
Created October 31, 2011 19:15
Show Gist options
  • Save gregworley/1328550 to your computer and use it in GitHub Desktop.
Save gregworley/1328550 to your computer and use it in GitHub Desktop.
golang r60 template package example using range on a slice of maps and accessing keys
package main
import (
"template"
"os"
)
const levelsT = `levels: [
{{range $h := .}} {{"{"}}bitrate:{{$h.bitrate}}, file:"{{$h.file}}", width:{{$h.width}}, height:{{$h.height}}{{"}"}}
{{end}}]
`
func main() {
file1 := map[string]string{"bitrate": "896", "file": "small.mp4", "width": "480", "height": "272"}
file2 := map[string]string{"bitrate": "1000", "file": "medium.mp4", "width": "1280", "height": "720"}
file3 := map[string]string{"bitrate": "2128", "file": "large.mp4", "width":"1920", "height": "1080"}
levels := []map[string]string{file1, file2, file3}
t1 := template.Must(template.New("levels").Parse(levelsT))
t1.Execute(os.Stdout, levels)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment