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.