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
361c5ace
Commit
361c5ace
authored
Aug 29, 2011
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
template: range over channel
R=golang-dev, dsymonds CC=golang-dev
https://golang.org/cl/4951046
parent
77f0bdce
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
18 deletions
+48
-18
exec.go
src/pkg/template/exec.go
+25
-17
exec_test.go
src/pkg/template/exec_test.go
+23
-1
No files found.
src/pkg/template/exec.go
View file @
361c5ace
...
...
@@ -196,41 +196,49 @@ func (s *state) walkRange(dot reflect.Value, r *parse.RangeNode) {
val
,
_
:=
indirect
(
s
.
evalPipeline
(
dot
,
r
.
Pipe
))
// mark top of stack before any variables in the body are pushed.
mark
:=
s
.
mark
()
switch
val
.
Kind
()
{
case
reflect
.
Array
,
reflect
.
Slice
:
if
val
.
Len
()
==
0
{
break
}
for
i
:=
0
;
i
<
val
.
Len
();
i
++
{
elem
:=
val
.
Index
(
i
)
oneIteration
:=
func
(
index
,
elem
reflect
.
Value
)
{
// Set top var (lexically the second if there are two) to the element.
if
len
(
r
.
Pipe
.
Decl
)
>
0
{
s
.
setVar
(
1
,
elem
)
}
// Set next var (lexically the first if there are two) to the index.
if
len
(
r
.
Pipe
.
Decl
)
>
1
{
s
.
setVar
(
2
,
reflect
.
ValueOf
(
i
)
)
s
.
setVar
(
2
,
index
)
}
s
.
walk
(
elem
,
r
.
List
)
s
.
pop
(
mark
)
}
switch
val
.
Kind
()
{
case
reflect
.
Array
,
reflect
.
Slice
:
if
val
.
Len
()
==
0
{
break
}
for
i
:=
0
;
i
<
val
.
Len
();
i
++
{
oneIteration
(
reflect
.
ValueOf
(
i
),
val
.
Index
(
i
))
}
return
case
reflect
.
Map
:
if
val
.
Len
()
==
0
{
break
}
for
_
,
key
:=
range
val
.
MapKeys
()
{
elem
:=
val
.
MapIndex
(
key
)
// Set top var (lexically the second if there are two) to the element.
if
len
(
r
.
Pipe
.
Decl
)
>
0
{
s
.
setVar
(
1
,
elem
)
oneIteration
(
key
,
val
.
MapIndex
(
key
))
}
// Set next var (lexically the first if there are two) to the key.
if
len
(
r
.
Pipe
.
Decl
)
>
1
{
s
.
setVar
(
2
,
key
)
return
case
reflect
.
Chan
:
if
val
.
IsNil
()
{
break
}
s
.
walk
(
elem
,
r
.
List
)
s
.
pop
(
mark
)
i
:=
0
for
;
;
i
++
{
elem
,
ok
:=
val
.
Recv
()
if
!
ok
{
break
}
oneIteration
(
reflect
.
ValueOf
(
i
),
elem
)
}
if
i
==
0
{
break
}
return
case
reflect
.
Invalid
:
...
...
src/pkg/template/exec_test.go
View file @
361c5ace
...
...
@@ -391,6 +391,8 @@ var execTests = []execTest{
{
"range $x $y MSIone"
,
"{{range $x, $y := .MSIone}}<{{$x}}={{$y}}>{{end}}"
,
"<one=1>"
,
tVal
,
true
},
{
"range $x PSI"
,
"{{range $x := .PSI}}<{{$x}}>{{end}}"
,
"<21><22><23>"
,
tVal
,
true
},
{
"declare in range"
,
"{{range $x := .PSI}}<{{$foo:=$x}}{{$x}}>{{end}}"
,
"<21><22><23>"
,
tVal
,
true
},
{
"range count"
,
`{{range $i, $x := count 5}}[{{$i}}]{{$x}}{{end}}`
,
"[0]a[1]b[2]c[3]d[4]e"
,
tVal
,
true
},
{
"range nil count"
,
`{{range $i, $x := count 0}}{{else}}empty{{end}}`
,
"empty"
,
tVal
,
true
},
// Cute examples.
{
"or as if true"
,
`{{or .SI "slice is empty"}}`
,
"[3 4 5]"
,
tVal
,
true
},
...
...
@@ -424,9 +426,29 @@ func oneArg(a string) string {
return
"oneArg="
+
a
}
// count returns a channel that will deliver n sequential 1-letter strings starting at "a"
func
count
(
n
int
)
chan
string
{
if
n
==
0
{
return
nil
}
c
:=
make
(
chan
string
)
go
func
()
{
for
i
:=
0
;
i
<
n
;
i
++
{
c
<-
"abcdefghijklmnop"
[
i
:
i
+
1
]
}
close
(
c
)
}()
return
c
}
func
testExecute
(
execTests
[]
execTest
,
set
*
Set
,
t
*
testing
.
T
)
{
b
:=
new
(
bytes
.
Buffer
)
funcs
:=
FuncMap
{
"zeroArgs"
:
zeroArgs
,
"oneArg"
:
oneArg
,
"typeOf"
:
typeOf
}
funcs
:=
FuncMap
{
"count"
:
count
,
"oneArg"
:
oneArg
,
"typeOf"
:
typeOf
,
"zeroArgs"
:
zeroArgs
,
}
for
_
,
test
:=
range
execTests
{
tmpl
:=
New
(
test
.
name
)
.
Funcs
(
funcs
)
_
,
err
:=
tmpl
.
ParseInSet
(
test
.
input
,
set
)
...
...
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