Golang clear function
last modified May 8, 2025
This tutorial explains how to use the clear
built-in function in Go.
We'll cover its usage with slices and maps through practical examples.
The clear function was introduced in Go 1.21 to zero out elements in slices and delete all entries from maps. It provides a clean way to reset collections to their zero state.
In Go, clear
operates differently based on the type it's applied to.
For slices, it sets elements to their zero value. For maps, it removes all keys.
Clearing a slice
The simplest use of clear
resets all elements in a slice to their
zero values. This example demonstrates basic slice clearing.
Note: The slice length remains unchanged; only elements are zeroed.
package main import "fmt" func main() { numbers := []int{1, 2, 3, 4, 5} fmt.Println("Before clear:", numbers) clear(numbers) // Zero out all elements fmt.Println("After clear:", numbers) fmt.Println("Slice length:", len(numbers)) }
The clear
function sets all integer elements to 0. The slice's
length and capacity remain unchanged after the operation.
Clearing a map
When applied to maps, clear
removes all key-value pairs. This
example shows map clearing in action.
package main import "fmt" func main() { userRoles := map[string]string{ "alice": "admin", "bob": "editor", "eve": "viewer", } fmt.Println("Before clear:", userRoles) fmt.Println("Map length:", len(userRoles)) clear(userRoles) // Remove all entries fmt.Println("After clear:", userRoles) fmt.Println("Map length:", len(userRoles)) }
The map becomes empty after clearing. Unlike slice clearing, this actually removes all entries rather than zeroing values.
Clearing a slice of structs
clear
works with complex types by setting all fields to their zero
values. This example demonstrates clearing a slice of structs.
package main import "fmt" type Point struct { X, Y int Name string } func main() { points := []Point{ {1, 2, "A"}, {3, 4, "B"}, {5, 6, "C"}, } fmt.Println("Before clear:", points) clear(points) fmt.Println("After clear:", points) }
All fields of each struct in the slice are set to their zero values. Numeric fields become 0, and string fields become empty strings.
Clearing a map with interface values
clear
handles maps with interface values by completely removing
all entries. This example shows clearing a map with mixed value types.
package main import "fmt" func main() { data := map[string]interface{}{ "name": "Alice", "age": 30, "active": true, "scores": []int{90, 85, 95}, } fmt.Println("Before clear:", data) fmt.Println("Map length:", len(data)) clear(data) fmt.Println("After clear:", data) fmt.Println("Map length:", len(data)) }
The map is completely emptied regardless of the value types it contained. This is different from slice behavior where elements are zeroed.
Clearing a portion of a slice
We can clear specific portions of a slice using slicing. This example shows how to clear a range of elements.
package main import "fmt" func main() { data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} fmt.Println("Original:", data) // Clear elements 3 through 7 (inclusive) clear(data[3:8]) fmt.Println("After clearing portion:", data) }
Only the specified slice portion is cleared. The rest of the slice remains unchanged. This technique is useful for partial resets.
Source
This tutorial covered the clear
function in Go with practical
examples of clearing slices and maps in various scenarios.
Author
List all Golang tutorials.