This is a creation in Article, where the information may have evolved or changed.
Using the parse
Package Mainimport ( "Html/template" " net/http"*http. Request) { name:= "Krebs" Tmpl, _:= template. New ("TXT"). Parse ("Hello everyone, I am {{.}}" ) Tmpl. Execute (w, name)}func main () { http. Handlefunc ("/", SayHello) http. Listenandserve (":", nil)}
Using Parsefiles
Go code
Package Mainimport ( "html/template" "net/http") type Info struct { Title string Name string Site *http. Request) { info:= info{"personal site", "Krebs", "http://www.sample.com/"} Tmpl, _:= template. Parsefiles ("home.html") Tmpl. Execute (W, info)}func main () { http. Handlefunc ("/", SayHello) http. Listenandserve (":", nil)}
home.html Code
<!DOCTYPE HTML><HTML><Head> <MetaCharSet= "Utf-8" /> <title>{{. Title}}</title></Head><Body>Hello everyone, I am {{. Name}}. This is my site {{. Site}}</Body></HTML>
Shown below:
Let the template not escape HTML tags
// x here is the string