Golang Template Template Custom Function usage Example _golang

Source: Internet
Author: User

The example in this article describes the use of Golang templates template custom functions. Share to everyone for your reference, specific as follows:

The Golang template is very powerful, and the UNIX pipe style function calls are very much like.

There are a number of built-in templates that can be used to refer to the PKG document

You can also implement custom functions.

Examples are as follows:

Copy Code code as follows:
Package Main
Import (
"Text/template"
"Time"
"OS"
)
Type User struct {
Username, Password string
Regtime time. Time
}
Func ShowTime (t time. Time, format string) string {
Return T.format (Format)
}
Func Main () {
U: = user{"Dotcoo", "Dotcoopwd", time. Now ()}
T, err: = template. New ("text"). Funcs (template. Funcmap{"Showtime": Showtime}).
Parse (' <p>{{. username}}| {{. password}}| {{. Regtime.format "2006-01-02 15:04:05"}}</p>
<p>{{. username}}| {{. password}}| {{showtime. Regtime "2006-01-02 15:04:05"}}</p>
`)
If Err!= nil {
Panic (ERR)
}
T.execute (OS. Stdout, U)
}

I hope this article will help you with your go language program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.