Files
the-way-to-go_ZH_CN/eBook/exercises/chapter_7/remove_slice.go
Allen.Guo 695188fe0d 修复代码bug--> exercises/chapter_7/remove_slice.go (#433)
* 修复代码bug, 切片取值错误导致结果偏差,丢失最后一个元素,优化创建切片的容量,避免每次自动扩容1个大小

* 使用切片完成操作
2018-03-05 03:54:44 -05:00

20 lines
384 B
Go
Executable File

// remove_slice.go
package main
import (
"fmt"
)
func main() {
s := []string{"M", "N", "O", "P", "Q", "R"}
res := RemoveStringSlice(s, 2, 4)
fmt.Println(res) // [M N Q R]
}
func RemoveStringSlice(slice []string, start, end int) []string {
result := make([]string, len(slice)-(end-start)-1)
at := copy(result, slice[:start])
copy(result[at:], slice[end+1:])
return result
}