一、數組
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)
}
二、Slice
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]
切片是對數組的view,可以修改數組原值,len是當前取值範圍大小,cap是實際的數組大小。
s1:len(s1)=4, cap(s1)=6,s2 = s1[3:5]取的cap範圍的數據。
三、Slice的操作
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)//len和cap不斷擴大,cap一般會比len大些,且是2的n次方
}
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)
}
輸出:
len=0, cap=0
len=1, cap=1
len=2, cap=2
len=3, cap=4
len=4, cap=4
len=5, cap=8
len=6, cap=8
len=7, cap=8
len=8, cap=8
len=9, cap=16
len=10, cap=16
len=11, cap=16
len=12, cap=16
len=13, cap=16
len=14, cap=16
len=15, cap=16
len=16, cap=16
len=17, cap=32
len=18, cap=32
len=19, cap=32
len=20, cap=32
[2 4 6 8], len=4, cap=4
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0], len=16, cap=16
[0 0 0 0 0 0 0 0 0 0], len=10, cap=32
Copying slice
[2 4 6 8 0 0 0 0 0 0 0 0 0 0 0 0], len=16, cap=16
Deleting elements from slice
[2 4 6 0 0 0 0 0 0 0 0 0 0 0 0], len=15, cap=16
Popping from front
2
[4 6 0 0 0 0 0 0 0 0 0 0 0 0], len=14, cap=15
Popping from back
0
[4 6 0 0 0 0 0 0 0 0 0 0 0], len=13, cap=15
四、Map
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[course:golang name:ccmouse quality:notbad 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
五、string
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) (E4 B8 AD) (E5 9B BD) 21
(0 59) (1 65) (2 73) (3 6211) (6 7231) (9 4E2D) (12 56FD) (15 21)
Rune count: 8
Y e s 我 愛 中 國 !
(0 Y) (1 e) (2 s) (3 我) (4 愛) (5 中) (6 國) (7 !)
type byte = uint8
// rune is an alias for int32 and is equivalent to int32 in all ways. It is
// used, by convention, to distinguish character values from integer values.
type rune = int32
在上面的例子中中文佔3個字節,英文佔1個字節。
https://learnku.com/articles/23411/the-difference-between-rune-and-byte-of-go
rune和byte的區別這篇文章講的很清楚。