Golang中Slice在特定位置高效插入元素的方法

2,301 阅读1分钟

当想在slice的一个特定位置插入元素时,golang没有官方方法,这里记录一种较为高效的插入方法

s = append(s, zero_value)

copy(s[i+1:], s[i:])

s[i] = x