package main import ( "fmt" "reflect" ) func main() { arr := [3]int{1, 2, 3} fmt.Println("Original array:", arr) // Set the length of the array to 5 v := reflect.ValueOf(&arr).Elem() v.SetLen(5) fmt.Println("New array:", arr) }
Original array: [1 2 3] New array: [1 2 3 0 0]
package main import ( "fmt" "reflect" ) func main() { slice := []int{1, 2, 3} fmt.Println("Original slice:", slice) // Set the length of the slice to 5 v := reflect.ValueOf(&slice).Elem() v.SetLen(5) fmt.Println("New slice:", slice) }
Original slice: [1 2 3] New slice: [1 2 3 0 0]The package library for this functionality is the Go reflection package.