I would like to call and print the result of Format on Date directly within the template without writing a boiler plate method for the Foo struct.
package main
import (
"html/template"
"os"
"time"
)
type Foo struct {
Date time.Time
}
func main() {
foo := Foo{time.Now()}
tmpl, err := template.New("test").Parse("{{.Date}}")
if err != nil {
panic(err)
}
err = tmpl.Execute(os.Stdout, foo)
if err != nil {
panic(err)
}
}
You can call .Format on the Date Object:
"{{.Date.Format \"Jan 2, 2006 at 3:04pm (MST)\" }}"
http://play.golang.org/p/P4kKfZ5UN5
package main
import (
"html/template"
"os"
"time"
)
type Foo struct {
Date time.Time
}
func main() {
foo := Foo{time.Now()}
tmpl, err := template.New("test").Parse("{{.Date.Format \"Jan 2, 2006 at 3:04pm (MST)\" }}")
if err != nil {
panic(err)
}
err = tmpl.Execute(os.Stdout, foo)
if err != nil {
panic(err)
}
}