1
0
mirror of https://github.com/golang/go synced 2024-11-20 00:44:45 -07:00

reflect: empty slice/map is not DeepEqual to nil

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/5373095
This commit is contained in:
Russ Cox 2011-11-14 16:11:15 -05:00
parent a7f1e10d24
commit 4e65478cbd
2 changed files with 14 additions and 0 deletions

View File

@ -651,6 +651,14 @@ var deepEqualTests = []DeepEqualTest{
{nil, 1, false},
{1, nil, false},
// Nil vs empty: not the same.
{[]int{}, []int(nil), false},
{[]int{}, []int{}, true},
{[]int(nil), []int(nil), true},
{map[int]int{}, map[int]int(nil), false},
{map[int]int{}, map[int]int{}, true},
{map[int]int(nil), map[int]int(nil), true},
// Mismatched types
{1, 1.0, false},
{int32(1), int64(1), false},

View File

@ -69,6 +69,9 @@ func deepValueEqual(v1, v2 Value, visited map[uintptr]*visit, depth int) (b bool
}
return true
case Slice:
if v1.IsNil() != v2.IsNil() {
return false
}
if v1.Len() != v2.Len() {
return false
}
@ -93,6 +96,9 @@ func deepValueEqual(v1, v2 Value, visited map[uintptr]*visit, depth int) (b bool
}
return true
case Map:
if v1.IsNil() != v2.IsNil() {
return false
}
if v1.Len() != v2.Len() {
return false
}