Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
b1d1da40
Commit
b1d1da40
authored
Aug 12, 2011
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
exp/template: add builtin len function
R=golang-dev, dsymonds CC=golang-dev
https://golang.org/cl/4868045
parent
2e394c51
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
24 additions
and
0 deletions
+24
-0
src/pkg/exp/template/doc.go
src/pkg/exp/template/doc.go
+2
-0
src/pkg/exp/template/exec_test.go
src/pkg/exp/template/exec_test.go
+6
-0
src/pkg/exp/template/funcs.go
src/pkg/exp/template/funcs.go
+16
-0
No files found.
src/pkg/exp/template/doc.go
View file @
b1d1da40
...
...
@@ -232,6 +232,8 @@ Predefined global functions are named as follows.
js
Returns the escaped JavaScript equivalent of the textual
representation of its arguments.
len
Returns the integer length of its argument.
not
Returns the boolean negation of its single argument.
or
...
...
src/pkg/exp/template/exec_test.go
View file @
b1d1da40
...
...
@@ -331,6 +331,12 @@ var execTests = []execTest{
{
"map[WRONG]"
,
"{{index .MSI 10}}"
,
""
,
tVal
,
false
},
{
"double index"
,
"{{index .SMSI 1 `eleven`}}"
,
"11"
,
tVal
,
true
},
// Len.
{
"slice"
,
"{{len .SI}}"
,
"3"
,
tVal
,
true
},
{
"map"
,
"{{len .MSI }}"
,
"3"
,
tVal
,
true
},
{
"len of int"
,
"{{len 3}}"
,
""
,
tVal
,
false
},
{
"len of nothing"
,
"{{len .Empty0}}"
,
""
,
tVal
,
false
},
// With.
{
"with true"
,
"{{with true}}{{.}}{{end}}"
,
"true"
,
tVal
,
true
},
{
"with false"
,
"{{with false}}{{.}}{{else}}FALSE{{end}}"
,
"FALSE"
,
tVal
,
true
},
...
...
src/pkg/exp/template/funcs.go
View file @
b1d1da40
...
...
@@ -27,6 +27,7 @@ var builtins = FuncMap{
"html"
:
HTMLEscaper
,
"index"
:
index
,
"js"
:
JSEscaper
,
"len"
:
length
,
"not"
:
not
,
"or"
:
or
,
"print"
:
fmt
.
Sprint
,
...
...
@@ -140,6 +141,21 @@ func index(item interface{}, indices ...interface{}) (interface{}, os.Error) {
return
v
.
Interface
(),
nil
}
// Length
// length returns the length of the item, with an error if it has no defined length.
func
length
(
item
interface
{})
(
int
,
os
.
Error
)
{
v
,
isNil
:=
indirect
(
reflect
.
ValueOf
(
item
))
if
isNil
{
return
0
,
fmt
.
Errorf
(
"len of nil pointer"
)
}
switch
v
.
Kind
()
{
case
reflect
.
Array
,
reflect
.
Chan
,
reflect
.
Map
,
reflect
.
Slice
,
reflect
.
String
:
return
v
.
Len
(),
nil
}
return
0
,
fmt
.
Errorf
(
"len of type %s"
,
v
.
Type
())
}
// Boolean logic.
func
truth
(
a
interface
{})
bool
{
...
...
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