Built-in function
Go clear()
Removes every entry from a map or sets every element in a slice’s current length to its zero value. Slice length and capacity do not change. Clearing a nil map or nil slice is harmless. Example prints [0 0] 0. Requires Go 1.21.
Formal Syntax & Structure
go syntaxgo
clear(t)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| t | map or slice | Container to clear. |
Working Example
Example Snippetgo
package main
import "fmt"
func main() { s := []int{5, 9} m := map[string]int{"x": 1} clear(s) clear(m) fmt.Println(s, len(m))}