Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
0bb8ce29
Commit
0bb8ce29
authored
Jan 24, 2012
by
Andrew Gerrand
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
text/template: add example for Template
R=golang-dev, r CC=golang-dev
https://golang.org/cl/5564050
parent
2c2c2022
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
69 additions
and
0 deletions
+69
-0
example_test.go
src/pkg/text/template/example_test.go
+69
-0
No files found.
src/pkg/text/template/example_test.go
0 → 100644
View file @
0bb8ce29
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package
template_test
import
(
"log"
"os"
"text/template"
)
// Dear Aunt Mildred,
//
// It was a pleasure to see you at the wedding.
// Thank you for the lovely bone china tea set.
//
// Best wishes,
// Josie
//
// Dear Uncle John,
//
// It is a shame you couldn't make it to the wedding.
// Thank you for the lovely moleskin pants.
//
// Best wishes,
// Josie
//
// Dear Cousin Rodney,
//
// It is a shame you couldn't make it to the wedding.
//
// Best wishes,
// Josie
func
ExampleTemplate
()
{
// Define a template.
const
letter
=
`
Dear {{.Name}},
{{if .Attended}}
It was a pleasure to see you at the wedding.{{else}}
It is a shame you couldn't make it to the wedding.{{end}}
{{with .Gift}}Thank you for the lovely {{.}}.
{{end}}
Best wishes,
Josie
`
// Prepare some data to insert into the template.
type
Recipient
struct
{
Name
,
Gift
string
Attended
bool
}
var
recipients
=
[]
Recipient
{
{
"Aunt Mildred"
,
"bone china tea set"
,
true
},
{
"Uncle John"
,
"moleskin pants"
,
false
},
{
"Cousin Rodney"
,
""
,
false
},
}
// Create a new template and parse the letter into it.
t
:=
template
.
Must
(
template
.
New
(
"letter"
)
.
Parse
(
letter
))
// Execute the template for each recipient.
for
_
,
r
:=
range
recipients
{
err
:=
t
.
Execute
(
os
.
Stdout
,
r
)
if
err
!=
nil
{
log
.
Println
(
"executing template:"
,
err
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment