funcmain() { slice1 := make([]int, 0, 10) // load the slice, cap(slice1) is 10: for i := 0; i < cap(slice1); i++ { slice1 = slice1[0:i+1] slice1[i] = i fmt.Printf("The length of slice is %d\n", len(slice1)) }
// print the slice: for i := 0; i < len(slice1); i++ { fmt.Printf("Slice at %d is %d\n", i, slice1[i]) } } /*output The length of slice is 1 The length of slice is 2 The length of slice is 3 The length of slice is 4 The length of slice is 5 The length of slice is 6 The length of slice is 7 The length of slice is 8 The length of slice is 9 The length of slice is 10 Slice at 0 is 0 Slice at 1 is 1 Slice at 2 is 2 Slice at 3 is 3 Slice at 4 is 4 Slice at 5 is 5 Slice at 6 is 6 Slice at 7 is 7 Slice at 8 is 8 Slice at 9 is 9 */
再举一个例子
1 2
var ar = [10]int{0,1,2,3,4,5,6,7,8,9} var a = ar[5:7] // reference to subarray {5,6} - len(a) is 2 and cap(a) is 5
我们将a重新分片
1
a = a[0:4] // ref of subarray {5,6,7,8} - len(a) is now 4 but cap(a) is still 5
func append(s[]T, x ...T)[]T,其中append()方法将0个或多个具有相同类型的s的元素追加到切片后面并返回新的切片;追加的元素必须和原本切片的元素类型相同。如果s的容量不足以存储新增元素,append()会分配新的切片来保证已有切片元素和新增元素的存储。在分配后切片的指针、长度和容量都会被更新。
funcmain() { s = []int{1, 2, 3} fmt.Println("The length of s before enlarging is:", len(s)) fmt.Println(s) s = enlarge(s, 5) fmt.Println("The length of s after enlarging is:", len(s)) fmt.Println(s) }
funcenlarge(s []int, factor int) []int { ns := make([]int, len(s)*factor) // fmt.Println("The length of ns is:", len(ns)) copy(ns, s) //fmt.Println(ns) s = ns //fmt.Println(s) //fmt.Println("The length of s after enlarging is:", len(s)) return s } /*output The length of s before enlarging is: 3 [1 2 3] The length of s after enlarging is: 15 [1 2 3 0 0 0 0 0 0] */
funcmain() { s := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} s = Filter(s, even) fmt.Println(s) }
// Filter returns a new slice holding only // the elements of s that satisfy f() funcFilter(s []int, fn func(int)bool) []int { var p []int// == nil for _, i := range s { if fn(i) { p = append(p, i) } } return p }
funcmain() { 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) fmt.Println(res) // [M N O A B C P Q R] }
funcInsertStringSlice(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 }
练习4: 写一个函数 RemoveStringSlice() 将从 start 到 end 索引的元素从切片中移除。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// remove_slice.go package main
import ( "fmt" )
funcmain() { s := []string{"M", "N", "O", "P", "Q", "R"} res := RemoveStringSlice(s, 2, 4) fmt.Println(res) // [M N Q R] }
funcRemoveStringSlice(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 }
funcmain() { // count number of characters: str1 := "asSASA ddd dsjkdsjs dk" fmt.Printf("The number of bytes in string str1 is %d\n", len(str1)) fmt.Printf("The number of characters in string str1 is %d\n", utf8.RuneCountInString(str1)) str2 := "asSASA ddd dsjkdsjsこん dk" fmt.Printf("The number of bytes in string str2 is %d\n", len(str2)) fmt.Printf("The number of characters in string str2 is %d", utf8.RuneCountInString(str2)) }
/* Output: The number of bytes in string str1 is 22 The number of characters in string str1 is 22 The number of bytes in string str2 is 28 The number of characters in string str2 is 24 */
funcFindDigits(filename string) []byte { b, _ := ioutil.ReadFile(filename) b = digitRegexp.Find(b) c := make([]byte, len(b)) copy(c, b) return c }
上面这个只找到了第一个匹配正则表达式的字符串,要找到全部的用下面这个
1 2 3 4 5 6 7 8 9
funcFindFileDigits(filename string) []byte { fileBytes, _ := ioutil.ReadFile(filename) b := digitRegexp.FindAll(fileBytes, len(fileBytes)) c := make([]byte, 0) for _, bytes := range b { c = append(c, bytes...) } return c }