This is a creation in Article, where the information may have evolved or changed.
What is a template
You must have heard of a design pattern called MVC, Model processing data, view presentation results, controller control of user requests, and the processing of the view layer, in many dynamic languages by inserting dynamic language generated data in static HTML, such as in JSP by inserting <%=....=%>,php is implemented by inserting.
The following diagram illustrates the mechanism of the template
Most of the information that the Web app feeds back to the client is static, unchanging, and a few are dynamically generated based on the user's request, for example to display a list of user access records. Only the record data between users is different, and the style of the list is fixed, at this time the template can be used to reuse a lot of static code
Two Template use
Package Main
Import
"Html/template"
"OS"
)
Type Person struct{
UserName string//must be capitalized, otherwise it cannot be displayed
Emails []string
Friends []*friend
}
Func Main () {
T: = template. New ("fieldname Example")
T,_ = T.parse ("Hello {{. username}}! ")
P: = person{username: "Astaxie"}
T.execute (OS. STDOUT,P)
Foo ()
}
The Go Language template contains the fields that need to be replaced at render time by {{}}}. Represents the current object, which is similar to this in Java or C + + if you want to access the field of the current object through {{. FieldName}}, but one thing to note: This field must be exported (the first letter of the field must be in uppercase), or it will be error-
Https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/07.4.md