0
我嘗試了新的元素添加到切片,就像追加一個新的元素在Python list
,但有一個錯誤如何將新元素添加到切片?
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)
}
和輸出
[2 3 5 7 11 13]
panic: runtime error: index out of range
哪有然後我在片上添加一個新元素?
在Python中,Go中的slice
是closet數據類型到list
是否正確?
謝謝。
https://gobyexample.com/slices – abhink
另外在[有效圍棋](https://golang.org/doc /effective_go.html#append),[Go Language Specification](https://golang.org/ref/spec#Appending_and_copying_slices)以及[Go of Tour](https://tour.golang.org/moretypes/) 7) – JimB
另請參閱:http://stackoverflow.com/questions/20195296/golang-append-an-item-to-a-slice/20197469#20197469 – gavv