Skip to content

Commit aa85fae

Browse files
committed
数组切片 映射
1 parent cbebd5c commit aa85fae

3 files changed

Lines changed: 126 additions & 2 deletions

File tree

‎chapter4/README.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@
44

55
## 数组
66

7-
是切片和映射的基本结构
7+
数组是切片和映射的基础,切片建立在底层的数组之上,映射使用散列
8+
切片使用时主要长度的容量,因为是共享底层数组所以最好使用
9+
相等长度容量之后使用append来扩容,操作时会创建一个新的数组
10+
不会对原有切片造成影响。
11+
映射长度容量没有限制
12+
len可以获得切片映射长度
13+
cap只能获取切片容量

‎chapter4/sliccode/mapexp.go‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package sliccode
2+
3+
//Map 映射是一种数据结构 用于存储一系列无序的键值对
4+
//映射基于键来存储值 基于键快速检索数据
5+
//映射是一个集合 映射是无序的集合
6+
//散列表 包含一组桶
7+
//操作时 都要选择一个桶
8+
// 操作映射时指定的键值传给映射的散列函数 散列函数生成一个索引
9+
10+
func init(){
11+
//创建一个 键是string 值是int的映射
12+
dict := make(map[string]int)
13+
//键值都是string的映射
14+
dict :=map[string]string{}
15+
//切片 函数 包含切结构的类型的类型 因为包含引用语义
16+
//不能作为映射的键
17+
//dict := map[[]string]int{}
18+
//声明一个包含字符串切片的映射
19+
dict :=map[int][]string{}
20+
21+
colors := map[string]string{}
22+
colors["red"]="#da1337"
23+
24+
//nil 映射 不能用来存储键值 会报错
25+
// var colors map[string]string
26+
// colors["red"] ="da1228"
27+
28+
29+
}
30+
31+
func isSet(){
32+
vaule ,exists := colors["blue"]
33+
if exists {
34+
fmt.Println(value)
35+
}
36+
value := colors["blue"]
37+
if value !=""{
38+
fmt.Println(value)
39+
}
40+
}
41+
42+
func forLoop(){
43+
colors :=map[string]string{
44+
"AliceBule" : "#f0f8ff",
45+
"Coral" : "#ff7F50",
46+
"DarKray": "#a9a9a9",
47+
"ForestGreen": "#23b22",
48+
}
49+
for key ,value := range colors{
50+
fmt.Printf("keys %s -value :%s\n",key,value)
51+
}
52+
delete(colors,"Coral")
53+
removeColor(colors,"DarKary")
54+
for key ,value := range colors{
55+
fmt.Printf("keys %s -value :%s\n",key,value)
56+
}
57+
}
58+
59+
func removeColor(colors map[string]string, key string){
60+
delete(colors,key)
61+
}

‎chapter4/sliccode/slicexp.go‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,61 @@ func appendValue(){
5555
fmt.Println("appendlen:",cap(newSlice),"slice",slice,newSlice)
5656
fmt.Println("增加容量")
5757

58-
}
58+
}
59+
60+
func threeSlice(){
61+
slice :=[]string{"Apple","Orange","Plum","Bannane","Grape"}
62+
//长度为1个元素 容量为2个元素
63+
newSlice :=slice[2:3:4]
64+
//slice [i:j:k] 第一个表示开始的索引位置,
65+
//第二个表示索引位置加上元素的个数 1 结果是3 为了设置容量,
66+
//从索引位置开始 加上希望容量包含的元素个2 结果是4
67+
//长度j-i
68+
//容量k-i
69+
//容量大于已有容量会报错 就是超过原来的容量
70+
fmt.Println(newSlice)
71+
//panic: runtime error: slice bounds out of range
72+
// f :=slice[2:3:6]
73+
// fmt.Println(f)
74+
}
75+
76+
func sameLens(){
77+
source := []string{"Apple","Orange","Plum","Bannane","Grape"}
78+
slice := source[2:3:3]
79+
//长度和容量一样
80+
//现在长度和容量一致,还是共享着底层数组但是后面再进行append操作时创建新的数组
81+
slice=append(slice,"232332")//最佳字符串
82+
slice=append(slice,"333")//最佳字符串
83+
84+
s1 :=[]int{1,2}
85+
s2 :=[]int{3,4}
86+
fmt.Printf("%v\n",append(s1,s2...))
87+
for index, value := range slice{
88+
fmt.Printf("Index:%d value :%s\n",index,value)
89+
}
90+
for index ,value := range slice{
91+
fmt.Printf("value %s vlaue-addr%X elemAddr %X\n",
92+
value,&value,&slice[index])
93+
//range 返回的是副本 不是原始值 value 地址不变 要使用index 的方式访问
94+
}
95+
for _,val :=range s1{
96+
fmt.Printf("value%d:",$val)
97+
}
98+
99+
for index:=1;index <len(slice);index++{
100+
fmt.Printf("Index: %d Value :%d\n",index ,slice[index])
101+
}
102+
}
103+
104+
func twoSlice(){
105+
slice :=[][]int{{10}, {100,200}}
106+
slice[0] =append(slice[0],20)
107+
//append 先增长切片 再将新的整型切片赋值为外发切片的第一个元素
108+
fmt.Println(slice)
109+
slice :make([]int, le6)
110+
newslice := foo(slice)
111+
fmt.Println(len(newslice),cap(newslice))
112+
}
113+
func foo(slice []int) []int{
114+
return slice
115+
}//一个切片大小是24个字节 地址8个长度8容量8个字节

0 commit comments

Comments
 (0)