Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pinzolo/fe10e30faf995eff940b1c396c93e076 to your computer and use it in GitHub Desktop.
Save pinzolo/fe10e30faf995eff940b1c396c93e076 to your computer and use it in GitHub Desktop.
ポインタ経由でメソッドを呼び出す
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) int {
return p.GetAge() + yearAfter
}
func main() {
tmpl, err := template.New("test").Parse("{{.Name}}({{.GetAge}}) lives in {{.City}}. 2 years later he is {{.GetFutureAge 2}}.")
if err != nil {
panic(err)
}
p := &Person{"pinzolo", "Kyoto"}
err = tmpl.Execute(os.Stdout, p)
if err != nil {
panic(err)
}
}
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) int {
return p.GetAge() + yearAfter
}
func main() {
tmpl, err := template.New("test").Parse("{{.Name}}({{.GetAge}}) lives in {{.City}}. 2 years later he is {{.GetFutureAge 2}}.")
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