Last active
June 3, 2016 02:04
-
-
Save pinzolo/2c109073b228d5b65544ad03caa06b96 to your computer and use it in GitHub Desktop.
複数の引数が必要なメソッドをテンプレート内で呼び出す
This file contains 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 ( | |
"os" | |
"text/template" | |
) | |
type Person struct { | |
Name string | |
City string | |
} | |
func (p Person) GetAge() int { | |
return 36 | |
} | |
func (p Person) GetFutureAge(yearAfter int, other int) int { | |
return p.GetAge() + yearAfter + other | |
} | |
func main() { | |
tmpl, err := template.New("test").Parse("{{.Name}}({{.GetAge}}) lives in {{.City}}. 5 years later he is {{.GetFutureAge 2 3}}.") | |
if err != nil { | |
panic(err) | |
} | |
p := Person{"pinzolo", "Kyoto"} | |
err = tmpl.Execute(os.Stdout, p) | |
if err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment