Go内建容器篇
数组、切片、Map、字符串
数组
http://www.cnblogs.com/ycx95/p/9383409.html
数组:值类型、必须规定类型长度且一旦定义不可更改、len函数获得数组的长度
切片
https://www.cnblogs.com/ycx95/p/9383721.html
切片与数组相比,不需要设定长度,在[]中不用设定值,相对来说比较自由。
Map
https://www.cnblogs.com/ycx95/p/9385867.html
Go中内置类型,其使用hash表实现,为引用类型。
无序键值对集合,通过key(类似索引)快速检索数据。
String
Go中字符串是一个字节切片,可通过将其内容封装在""中创建字符串(Unicode兼容、UTF-8编码)
目录:
数组学习arrays.go
package main import "fmt" func printArray(arr [5]int) { arr[0] = 100 for i, v := range arr { fmt.Println(i, v) } } func main() { var arr1 [5]int arr2 := [3]int{1, 3, 5} arr3 := [...]int{2, 4, 6, 8, 10} var grid [4][5]int fmt.Println("array definitions:") fmt.Println(arr1, arr2, arr3) fmt.Println(grid) fmt.Println("printArray(arr1)") printArray(arr1) fmt.Println("printArray(arr3)") printArray(arr3) fmt.Println("arr1 and arr3") fmt.Println(arr1, arr3) }
输出是:
array definitions: [0 0 0 0 0] [1 3 5] [2 4 6 8 10] [[0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0]] printArray(arr1) 0 100 1 0 2 0 3 0 4 0 printArray(arr3) 0 100 1 4 2 6 3 8 4 10 arr1 and arr3 [0 0 0 0 0] [2 4 6 8 10] Process finished with exit code 0
切片学习slices.go sliceops.go
package main import "fmt" func updateSlice(s []int) { s[0] = 100 } func main() { arr := [...]int{0, 1, 2, 3, 4, 5, 6, 7} fmt.Println("arr[2:6] =", arr[2:6]) fmt.Println("arr[:6] =", arr[:6]) s1 := arr[2:] fmt.Println("s1 =", s1) s2 := arr[:] fmt.Println("s2 =", s2) fmt.Println("After updateSlice(s1)") updateSlice(s1) fmt.Println(s1) fmt.Println(arr) fmt.Println("After updateSlice(s2)") updateSlice(s2) fmt.Println(s2) fmt.Println(arr) fmt.Println("Reslice") fmt.Println(s2) s2 = s2[:5] fmt.Println(s2) s2 = s2[2:] fmt.Println(s2) fmt.Println("Extending slice") arr[0], arr[2] = 0, 2 fmt.Println("arr =", arr) s1 = arr[2:6] s2 = s1[3:5] // [s1[3], s1[4]] fmt.Printf("s1=%v, len(s1)=%d, cap(s1)=%d\n", s1, len(s1), cap(s1)) fmt.Printf("s2=%v, len(s2)=%d, cap(s2)=%d\n", s2, len(s2), cap(s2)) s3 := append(s2, 10) s4 := append(s3, 11) s5 := append(s4, 12) fmt.Println("s3, s4, s5 =", s3, s4, s5) // s4 and s5 no longer view arr. fmt.Println("arr =", arr) // Uncomment to run sliceOps demo. // If we see undefined: sliceOps // please try go run slices.go sliceops.go fmt.Println("Uncomment to see sliceOps demo") // sliceOps() }
输出:
arr[2:6] = [2 3 4 5] arr[:6] = [0 1 2 3 4 5] s1 = [2 3 4 5 6 7] s2 = [0 1 2 3 4 5 6 7] After updateSlice(s1) [100 3 4 5 6 7] [0 1 100 3 4 5 6 7] After updateSlice(s2) [100 1 100 3 4 5 6 7] [100 1 100 3 4 5 6 7] Reslice [100 1 100 3 4 5 6 7] [100 1 100 3 4] [100 3 4] Extending slice arr = [0 1 2 3 4 5 6 7] s1=[2 3 4 5], len(s1)=4, cap(s1)=6 s2=[5 6], len(s2)=2, cap(s2)=3 s3, s4, s5 = [5 6 10] [5 6 10 11] [5 6 10 11 12] arr = [0 1 2 3 4 5 6 10] Uncomment to see sliceOps demo Process finished with exit code 0
package main import "fmt" func printSlice(s []int) { fmt.Printf("%v, len=%d, cap=%d\n", s, len(s), cap(s)) } func sliceOps() { fmt.Println("Creating slice") var s []int // Zero value for slice is nil for i := 0; i < 100; i++ { printSlice(s) s = append(s, 2*i+1) } fmt.Println(s) s1 := []int{2, 4, 6, 8} printSlice(s1) s2 := make([]int, 16) s3 := make([]int, 10, 32) printSlice(s2) printSlice(s3) fmt.Println("Copying slice") copy(s2, s1) printSlice(s2) fmt.Println("Deleting elements from slice") s2 = append(s2[:3], s2[4:]...) printSlice(s2) fmt.Println("Popping from front") front := s2[0] s2 = s2[1:] fmt.Println(front) printSlice(s2) fmt.Println("Popping from back") tail := s2[len(s2)-1] s2 = s2[:len(s2)-1] fmt.Println(tail) printSlice(s2) }
输出:将程序入口改为main执行即可
map.go学习
package main import "fmt" func main() { m := map[string]string{ "name": "ccmouse", "course": "golang", "site": "imooc", "quality": "notbad", } m2 := make(map[string]int) // m2 == empty map var m3 map[string]int // m3 == nil fmt.Println("m, m2, m3:") fmt.Println(m, m2, m3) fmt.Println("Traversing map m") for k, v := range m { fmt.Println(k, v) } fmt.Println("Getting values") courseName := m["course"] fmt.Println(`m["course"] =`, courseName) if causeName, ok := m["cause"]; ok { fmt.Println(causeName) } else { fmt.Println("key 'cause' does not exist") } fmt.Println("Deleting values") name, ok := m["name"] fmt.Printf("m[%q] before delete: %q, %v\n", "name", name, ok) delete(m, "name") name, ok = m["name"] fmt.Printf("m[%q] after delete: %q, %v\n", "name", name, ok) }
输出是:
m, m2, m3: map[quality:notbad name:ccmouse course:golang site:imooc] map[] map[] Traversing map m name ccmouse course golang site imooc quality notbad Getting values m["course"] = golang key 'cause' does not exist Deleting values m["name"] before delete: "ccmouse", true m["name"] after delete: "", false
strings.go学习
package main import ( "fmt" "unicode/utf8" ) func main() { s := "Yes我爱慕课网!" // UTF-8 fmt.Println(s) for _, b := range []byte(s) { fmt.Printf("%X ", b) } fmt.Println() for i, ch := range s { // ch is a rune fmt.Printf("(%d %X) ", i, ch) } fmt.Println() fmt.Println("Rune count:", utf8.RuneCountInString(s)) bytes := []byte(s) for len(bytes) > 0 { ch, size := utf8.DecodeRune(bytes) bytes = bytes[size:] fmt.Printf("%c ", ch) } fmt.Println() for i, ch := range []rune(s) { fmt.Printf("(%d %c) ", i, ch) } fmt.Println() }
输出:
Yes我爱慕课网! 59 65 73 E6 88 91 E7 88 B1 E6 85 95 E8 AF BE E7 BD 91 21 (0 59) (1 65) (2 73) (3 6211) (6 7231) (9 6155) (12 8BFE) (15 7F51) (18 21) Rune count: 9 Y e s 我 爱 慕 课 网 ! (0 Y) (1 e) (2 s) (3 我) (4 爱) (5 慕) (6 课) (7 网) (8 !)
寻找最长字符串操作学习
package main import ( "fmt" ) func lengthOfNonRepeatingSubStr(s string) int { lastOccurred := make(map[rune]int) start := 0 maxLength := 0 for i, ch := range []rune(s) { if lastI, ok := lastOccurred[ch]; ok && lastI >= start { start = lastI + 1 } if i-start+1 > maxLength { maxLength = i - start + 1 } lastOccurred[ch] = i } return maxLength } func main() { fmt.Println( lengthOfNonRepeatingSubStr("abcabcbb")) fmt.Println( lengthOfNonRepeatingSubStr("bbbbb")) fmt.Println( lengthOfNonRepeatingSubStr("pwwkew")) fmt.Println( lengthOfNonRepeatingSubStr("")) fmt.Println( lengthOfNonRepeatingSubStr("b")) fmt.Println( lengthOfNonRepeatingSubStr("abcdef")) fmt.Println( lengthOfNonRepeatingSubStr("这里是慕课网")) fmt.Println( lengthOfNonRepeatingSubStr("一二三二一")) fmt.Println( lengthOfNonRepeatingSubStr( "黑化肥挥发发灰会花飞灰化肥挥发发黑会飞花")) }
输出是:
3 1 3 0 1 6 6 3 8
test:
package main import "testing" func TestSubstr(t *testing.T) { tests := []struct { s string ans int }{ // Normal cases {"abcabcbb", 3}, {"pwwkew", 3}, // Edge cases {"", 0}, {"b", 1}, {"bbbbbbbbb", 1}, {"abcabcabcd", 4}, // Chinese support {"这里是慕课网", 6}, {"一二三二一", 3}, {"黑化肥挥发发灰会花飞灰化肥挥发发黑会飞花", 8}, } for _, tt := range tests { actual := lengthOfNonRepeatingSubStr(tt.s) if actual != tt.ans { t.Errorf("got %d for input %s; "+ "expected %d", actual, tt.s, tt.ans) } } } func BenchmarkSubstr(b *testing.B) { s := "黑化肥挥发发灰会花飞灰化肥挥发发黑会飞花" for i := 0; i < 13; i++ { s = s + s } b.Logf("len(s) = %d", len(s)) ans := 8 b.ResetTimer() for i := 0; i < b.N; i++ { actual := lengthOfNonRepeatingSubStr(s) if actual != ans { b.Errorf("got %d for input %s; "+ "expected %d", actual, s, ans) } } }
输出是:
笔记,万变不离。