panic: runtime error: slice bounds out of range

go 切片越界怎么办?

 

var test []int = []int{1, 2, 3, 4}
length := len(test)
	if length < 12{
		tmp:=make([]int, 12-length)
		test = append(test, tmp...)
	}
	test[11] = 2
	fmt.Println(test)

 

封装了一下,如下:

//扩展m长度
func Cm_append_num(a []byte, m int) []byte{

	tmp:= (make([]byte, m))
	glog.Debugf("buf append %d byte", m)
	a = append(a,tmp...)

	return a
}

 

你可能感兴趣的:(go)