Skip to content

Instantly share code, notes, and snippets.

@tamalsaha
Last active August 10, 2017 08:09
Show Gist options
  • Save tamalsaha/cc86951e3ef7ae23de34f2b116814541 to your computer and use it in GitHub Desktop.
Save tamalsaha/cc86951e3ef7ae23de34f2b116814541 to your computer and use it in GitHub Desktop.
GO Template Example
package main
import (
"text/template"
"os"
)
func main() {
type Inner struct {
A string
}
type Outer struct {
Inner
}
o := Outer{
Inner: Inner{A : "123"},
}
tpl := template.Must(template.New("").Parse(`{{ .A }}`))
tpl.Execute(os.Stdout, &o)
}
package main
import (
"text/template"
"os"
)
func main() {
type Inner struct {
A string
}
type Outer struct {
Inner
}
type NA struct {
O []Outer
}
na := NA{
O: []Outer{
{
Inner: Inner{A: "123"},
},
},
}
tpl := template.Must(template.New("").Parse(`
{{ define "t2" }}
{{ printf "%v" .A }}
{{ end }}
{{ range $svc := .O }}
{{ template "t2" $svc }}
{{ end }}
`))
tpl.Execute(os.Stdout, &na)
}
package main
import (
"text/template"
"os"
)
func main() {
type Inner struct {
A string
}
type Outer struct {
Inner
}
type NA struct {
O []Outer
}
na := NA{
O: []Outer{
{
Inner: Inner{A: "1"},
},
{
Inner: Inner{A: "2"},
},
{
Inner: Inner{A: "3"},
},
{
Inner: Inner{A: "4"},
},
},
}
tpl := template.Must(template.New("").Parse(`
{{- range $svc := .O }}
{{ printf "%v" .A -}}
{{ end -}}
{{- range $svc := .O }}
{{ printf "%v" .A -}}
{{ end -}}
`))
tpl.Execute(os.Stdout, &na)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment