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
abae8471
Commit
abae8471
authored
Jul 10, 2011
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
exp/template: add functions print and println.
R=golang-dev, adg CC=golang-dev
https://golang.org/cl/4687041
parent
4acddca4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
10 deletions
+22
-10
exec_test.go
src/pkg/exp/template/exec_test.go
+4
-2
funcs.go
src/pkg/exp/template/funcs.go
+18
-8
No files found.
src/pkg/exp/template/exec_test.go
View file @
abae8471
...
...
@@ -207,8 +207,10 @@ var execTests = []execTest{
{
"if emptymap"
,
"{{if .MSIEmpty}}NON-EMPTY{{else}}EMPTY{{end}}"
,
"EMPTY"
,
tVal
,
true
},
{
"if map"
,
"{{if .MSI}}NON-EMPTY{{else}}EMPTY{{end}}"
,
"NON-EMPTY"
,
tVal
,
true
},
// Printf.
{
"printf"
,
`{{printf "hello, printf"}}`
,
"hello, printf"
,
tVal
,
true
},
// Print etc.
{
"print"
,
`{{print "hello, print"}}`
,
"hello, print"
,
tVal
,
true
},
{
"print"
,
`{{print 1 2 3}}`
,
"1 2 3"
,
tVal
,
true
},
{
"println"
,
`{{println 1 2 3}}`
,
"1 2 3
\n
"
,
tVal
,
true
},
{
"printf int"
,
`{{printf "%04x" 127}}`
,
"007f"
,
tVal
,
true
},
{
"printf float"
,
`{{printf "%g" 3.5}}`
,
"3.5"
,
tVal
,
true
},
{
"printf complex"
,
`{{printf "%g" 1+7i}}`
,
"(1+7i)"
,
tVal
,
true
},
...
...
src/pkg/exp/template/funcs.go
View file @
abae8471
...
...
@@ -17,17 +17,27 @@ import (
// FuncMap is the type of the map defining the mapping from names to functions.
// Each function must have either a single return value, or two return values of
// which the second has type os.Error.
// which the second has type os.Error. If the second argument evaluates to non-nil
// during execution, execution terminates and the error is returned by Execute.
type
FuncMap
map
[
string
]
interface
{}
var
funcs
=
map
[
string
]
reflect
.
Value
{
"and"
:
reflect
.
ValueOf
(
and
),
"html"
:
reflect
.
ValueOf
(
HTMLEscaper
),
"index"
:
reflect
.
ValueOf
(
index
),
"js"
:
reflect
.
ValueOf
(
JSEscaper
),
"not"
:
reflect
.
ValueOf
(
not
),
"or"
:
reflect
.
ValueOf
(
or
),
"printf"
:
reflect
.
ValueOf
(
fmt
.
Sprintf
),
"and"
:
reflect
.
ValueOf
(
and
),
"html"
:
reflect
.
ValueOf
(
HTMLEscaper
),
"index"
:
reflect
.
ValueOf
(
index
),
"js"
:
reflect
.
ValueOf
(
JSEscaper
),
"not"
:
reflect
.
ValueOf
(
not
),
"or"
:
reflect
.
ValueOf
(
or
),
"print"
:
reflect
.
ValueOf
(
fmt
.
Sprint
),
"printf"
:
reflect
.
ValueOf
(
fmt
.
Sprintf
),
"println"
:
reflect
.
ValueOf
(
fmt
.
Sprintln
),
}
// Funcs adds to the global function map the elements of the
// argument map. It panics if a value in the map is not a function
// with appropriate return type.
func
Funcs
(
funcMap
FuncMap
)
{
addFuncs
(
funcs
,
funcMap
)
}
// addFuncs adds to values the functions in funcs, converting them to reflect.Values.
...
...
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