I tried to append a new element to a slice, just like appending a new element to a list
in Python, but there is an error
package main
import "fmt"
func main() {
s := []int{2, 3, 5, 7, 11, 13}
printSlice(s)
s[len(s)] = 100
printSlice(s)
}
func printSlice(s []int) {
fmt.Printf("%v\n", s)
}
and the output is
[2 3 5 7 11 13]
panic: runtime error: index out of range
How can I append a new element to a slice then?
Is it correct that slice
in Go is the closet data type to list
in Python?
Thanks.