go切片默认初始化len为0,既元素无法直接赋值为[0]=xx

package main

import "fmt"

func main() {
var s =[]string{} //默认元素是【0】 应该为make([]string,3)
s[0]="test" //无法向【0】元素添加数据
fmt.Println(s)

}


//

panic: runtime error: index out of range [0] with length 0

goroutine 1 [running]:
main.main()
D:/awesomeProject/src/test/test25.go:7 +0x18

posted @ 2022-02-23 20:58  技术颜良  阅读(163)  评论(0)    收藏  举报