数组
数组长度固定,不建议用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| package main import "fmt"
func printArray (array [4]int){ fmt.Println("---------------输出函数--------------") for i := 0; i < len(array); i++ { fmt.Println("array2_value = ", array[i]) } } func main () { var array1 [10]int for i := 0; i < len(array1); i++ { fmt.Println("array1_value = ", array1[i]) } array2 := [10]int{1, 2, 4, 5} array3 := [4]int{1, 2, 4, 5} for index, value := range array2 { fmt.Println("array2_index = ", index, "array2_value = ", value) } printArray(array3) fmt.Printf("array1 type : %T\n", array1) fmt.Printf("array2 type : %T\n", array2) fmt.Printf("array3 type : %T\n", array3) }
|
slice\切片\动态数组
Go 语言切片是对数组的抽象。
Go 数组的长度不可改变,在特定场景中这样的集合就不太适用,Go中提供了一种灵活,功能强悍的内置类型切片("动态数组")
,与数组相比切片的长度是不固定的,可以追加元素,在追加时可能使切片的容量增大。
定义切片四种方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| package main import ( "fmt" ) func main() { slice1 := []int{1, 2, 3} var slice2 []int slice2 = make([]int, 3) var slice3 []int = make([]int, 3) slice4 := make([] int, 3) s := arr[:] fmt.Printf("len = %d , value = %v\n", len(slice1), slice1) if slice1 == nil { fmt.Println("slice1 is null!") } else { fmt.Println("slice2 is not null!!") } }
|
len()
和 cap()
函数
切片是可索引的,并且可以由 len()
方法获取长度。
切片提供了计算容量的方法 cap()
可以测量切片最长可以达到多少。
以下为具体实例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package main
import "fmt"
func main() { var numbers = make([]int,3,5) printSlice(numbers) }
func printSlice(x []int){ fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x) }
|
以上实例运行输出结果为:
1
| len=3 cap=5 slice=[0 0 0]
|
空(nil)切片
一个切片在未初始化之前默认为 nil,长度为 0,实例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package main
import "fmt"
func main() { var numbers []int printSlice(numbers)
if(numbers == nil){ fmt.Printf("切片是空的") } }
func printSlice(x []int){ fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x) }
|
以上实例运行输出结果为:
1 2
| len=0 cap=0 slice=[] 切片是空的
|
切片截取
可以通过设置下限及上限来设置截取切片 [lower-bound: upper-bound]
,实例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| package main
import "fmt"
func main() { numbers := []int{0,1,2,3,4,5,6,7,8} printSlice(numbers)
fmt.Println("numbers ==", numbers)
fmt.Println("numbers[1:4] ==", numbers[1:4])
fmt.Println("numbers[:3] ==", numbers[:3])
fmt.Println("numbers[4:] ==", numbers[4:])
numbers1 := make([]int,0,5) printSlice(numbers1)
number2 := numbers[:2] printSlice(number2)
number3 := numbers[2:5] printSlice(number3) }
func printSlice(x []int){ fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x) }
|
执行以上代码输出结果为:
1 2 3 4 5 6 7 8
| len=9 cap=9 slice=[0 1 2 3 4 5 6 7 8] numbers == [0 1 2 3 4 5 6 7 8] numbers[1:4] == [1 2 3] numbers[:3] == [0 1 2] numbers[4:] == [4 5 6 7 8] len=0 cap=5 slice=[] len=2 cap=9 slice=[0 1] len=3 cap=7 slice=[2 3 4]
|
深拷贝 copy()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package main import "fmt"
func main () { s := [] int{1, 2, 3, 4} s1 := s[0:2] fmt.Println("s1 = ", s1) s2 := s[1:] fmt.Println("s2 = ", s2) s1[0] = 20000 fmt.Println("s = ", s) s3 := make([] int, 3) copy(s3, s2) fmt.Println("s3 = ", s3) }
|
追加 append()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package main
import "fmt"
func main() { var numbers []int printSlice(numbers)
numbers = append(numbers, 0) printSlice(numbers)
numbers = append(numbers, 1) printSlice(numbers)
numbers = append(numbers, 2,3,4) printSlice(numbers) }
func printSlice(x []int){ fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x) }
|
以上代码执行输出结果为:
1 2 3 4
| len=0 cap=0 slice=[] len=1 cap=1 slice=[0] len=2 cap=2 slice=[0 1] len=5 cap=6 slice=[0 1 2 3 4]
|
map
map 和 slice 类似,只不过是数据结构不同,下面是 map 的一些声明方式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| package main import ( "fmt" )
func main() { var test1 map[string]string test1 = make(map[string]string, 10) test1["one"] = "php" test1["two"] = "golang" test1["three"] = "java" fmt.Println(test1)
test2 := make(map[string]string) test2["one"] = "php" test2["two"] = "golang" test2["three"] = "java" fmt.Println(test2)
test3 := map[string]string{ "one" : "php", "two" : "golang", "three" : "java", } fmt.Println(test3)
language := make(map[string]map[string]string) language["php"] = make(map[string]string, 2) language["php"]["id"] = "1" language["php"]["desc"] = "php是世界上最美的语言" language["golang"] = make(map[string]string, 2) language["golang"]["id"] = "2" language["golang"]["desc"] = "golang抗并发非常good" fmt.Println(language)
fmt.Println(language) for key, value := range language { fmt.Println("key = ", key, ", value = ", value) } }
|