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
6ed27d47
Commit
6ed27d47
authored
Aug 01, 2011
by
Roger Peppe
Committed by
Rob Pike
Aug 01, 2011
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
exp/template: make index function on maps return zero value when key not present.
R=r CC=golang-dev
https://golang.org/cl/4808065
parent
5c5ce6d2
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
5 additions
and
4 deletions
+5
-4
src/pkg/exp/template/exec_test.go
src/pkg/exp/template/exec_test.go
+1
-1
src/pkg/exp/template/funcs.go
src/pkg/exp/template/funcs.go
+4
-3
No files found.
src/pkg/exp/template/exec_test.go
View file @
6ed27d47
...
...
@@ -297,7 +297,7 @@ var execTests = []execTest{
{
"slice[WRONG]"
,
"{{index .SI `hello`}}"
,
""
,
tVal
,
false
},
{
"map[one]"
,
"{{index .MSI `one`}}"
,
"1"
,
tVal
,
true
},
{
"map[two]"
,
"{{index .MSI `two`}}"
,
"2"
,
tVal
,
true
},
{
"map[NO]"
,
"{{index .MSI `XXX`}}"
,
""
,
tVal
,
fals
e
},
{
"map[NO]"
,
"{{index .MSI `XXX`}}"
,
""
,
tVal
,
tru
e
},
{
"map[WRONG]"
,
"{{index .MSI 10}}"
,
""
,
tVal
,
false
},
{
"double index"
,
"{{index .SMSI 1 `eleven`}}"
,
"11"
,
tVal
,
true
},
...
...
src/pkg/exp/template/funcs.go
View file @
6ed27d47
...
...
@@ -109,9 +109,10 @@ func index(item interface{}, indices ...interface{}) (interface{}, os.Error) {
if
!
index
.
Type
()
.
AssignableTo
(
v
.
Type
()
.
Key
())
{
return
nil
,
fmt
.
Errorf
(
"%s is not index type for %s"
,
index
.
Type
(),
v
.
Type
())
}
v
=
v
.
MapIndex
(
index
)
if
!
v
.
IsValid
()
{
return
nil
,
fmt
.
Errorf
(
"index %v not present in map"
,
index
.
Interface
())
if
x
:=
v
.
MapIndex
(
index
);
x
.
IsValid
()
{
v
=
x
}
else
{
v
=
reflect
.
Zero
(
v
.
Type
()
.
Key
())
}
default
:
return
nil
,
fmt
.
Errorf
(
"can't index item of type %s"
,
index
.
Type
())
...
...
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