Go Template Use

Source: Internet
Author: User
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

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.