Last active
August 10, 2017 08:09
-
-
Save tamalsaha/cc86951e3ef7ae23de34f2b116814541 to your computer and use it in GitHub Desktop.
GO Template Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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