mirror of
https://github.com/golang/go
synced 2024-11-22 05:24:39 -07:00
b89a840d65
To clear map, and zero content of slice. Updates #56351 Change-Id: I5f81dfbc465500f5acadaf2c6beb9b5f0d2c4045 Reviewed-on: https://go-review.googlesource.com/c/go/+/453395 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Keith Randall <khr@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Keith Randall <khr@google.com> Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
48 lines
743 B
Go
48 lines
743 B
Go
// run
|
|
|
|
// Copyright 2022 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package main
|
|
|
|
import "math"
|
|
|
|
func checkClearSlice() {
|
|
s := []int{1, 2, 3}
|
|
clear(s)
|
|
for i := range s {
|
|
if s[i] != 0 {
|
|
panic("clear not zeroing slice elem")
|
|
}
|
|
}
|
|
|
|
clear([]int{})
|
|
}
|
|
|
|
func checkClearMap() {
|
|
m1 := make(map[int]int)
|
|
m1[0] = 0
|
|
m1[1] = 1
|
|
clear(m1)
|
|
if len(m1) != 0 {
|
|
panic("m1 is not cleared")
|
|
}
|
|
|
|
// map contains NaN keys is also cleared.
|
|
m2 := make(map[float64]int)
|
|
m2[math.NaN()] = 1
|
|
m2[math.NaN()] = 1
|
|
clear(m2)
|
|
if len(m2) != 0 {
|
|
panic("m2 is not cleared")
|
|
}
|
|
|
|
clear(map[int]int{})
|
|
}
|
|
|
|
func main() {
|
|
checkClearSlice()
|
|
checkClearMap()
|
|
}
|