1024programmer Java How to add current year to Go template?

How to add current year to Go template?

1> zzn..:

You can add functionality to the template, try this:

package main

 import (

 func main() {
     funcMap := template.FuncMap{
         "now": time.Now,

     templateText := "The current year is {{now.UTC.Year}}"
     tmpl, err := template.New("titleTest").Funcs(funcMap).Parse(templateText)
     if err != nil {
         log.Fatalf("parsing: %s", err)

     // Run the template to verify the output.
     err = tmpl.Execute(os.Stdout, nil)
     if err != nil {
         log.Fatalf("execution: %s", err)

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/763139

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us


Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top