mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 01:55:35 +08:00
update book code
This commit is contained in:
19
eBook/exercises/chapter_7/remove_slice.go
Executable file
19
eBook/exercises/chapter_7/remove_slice.go
Executable file
@@ -0,0 +1,19 @@
|
||||
// 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))
|
||||
at := copy(result, slice[:start])
|
||||
copy(result[at:], slice[end:])
|
||||
return result
|
||||
}
|
Reference in New Issue
Block a user