mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 01:21:38 +08:00
fix: coding style and file format for chapter 7.
This commit is contained in:
@@ -1,23 +1,23 @@
|
||||
// insert_slice.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := []string{"M", "N", "O", "P", "Q", "R"}
|
||||
in := []string{"A", "B", "C"}
|
||||
res := InsertStringSlice(s, in, 0) // at the front
|
||||
fmt.Println(res) // [A B C M N O P Q R]
|
||||
res = InsertStringSlice(s, in, 3) // [M N O A B C P Q R]
|
||||
fmt.Println(res)
|
||||
}
|
||||
|
||||
func InsertStringSlice(slice, insertion []string, index int) []string {
|
||||
result := make([]string, len(slice) + len(insertion))
|
||||
at := copy(result, slice[:index])
|
||||
at += copy(result[at:], insertion)
|
||||
copy(result[at:], slice[index:])
|
||||
return result
|
||||
}
|
||||
// insert_slice.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := []string{"M", "N", "O", "P", "Q", "R"}
|
||||
in := []string{"A", "B", "C"}
|
||||
res := InsertStringSlice(s, in, 0) // at the front
|
||||
fmt.Println(res) // [A B C M N O P Q R]
|
||||
res = InsertStringSlice(s, in, 3) // [M N O A B C P Q R]
|
||||
fmt.Println(res)
|
||||
}
|
||||
|
||||
func InsertStringSlice(slice, insertion []string, index int) []string {
|
||||
result := make([]string, len(slice)+len(insertion))
|
||||
at := copy(result, slice[:index])
|
||||
at += copy(result[at:], insertion)
|
||||
copy(result[at:], slice[index:])
|
||||
return result
|
||||
}
|
||||
|
Reference in New Issue
Block a user