1// run 2 3// Copyright 2022 The Go Authors. All rights reserved. 4// Use of this source code is governed by a BSD-style 5// license that can be found in the LICENSE file. 6 7package main 8 9import "math" 10 11func checkClearSlice() { 12 s := []int{1, 2, 3} 13 clear(s) 14 for i := range s { 15 if s[i] != 0 { 16 panic("clear not zeroing slice elem") 17 } 18 } 19 20 clear([]int{}) 21} 22 23func checkClearMap() { 24 m1 := make(map[int]int) 25 m1[0] = 0 26 m1[1] = 1 27 clear(m1) 28 if len(m1) != 0 { 29 panic("m1 is not cleared") 30 } 31 32 // map contains NaN keys is also cleared. 33 m2 := make(map[float64]int) 34 m2[math.NaN()] = 1 35 m2[math.NaN()] = 1 36 clear(m2) 37 if len(m2) != 0 { 38 panic("m2 is not cleared") 39 } 40 41 clear(map[int]int{}) 42} 43 44func main() { 45 checkClearSlice() 46 checkClearMap() 47} 48