Pārlūkot izejas kodu

2022-03-29 22:56

陈超 3 gadus atpakaļ
vecāks
revīzija
b1df3ba0c4
2 mainītis faili ar 42 papildinājumiem un 11 dzēšanām
  1. 21
    4
      chapter1-why-generics.slide
  2. 21
    7
      codes/print.go

+ 21
- 4
chapter1-why-generics.slide Parādīt failu

1
 # Go 泛型介绍
1
 # Go 泛型介绍
2
 
2
 
3
 清和
3
 清和
4
-2022-03-31
5
-qinghe.chen@tuya.com
4
+2022-03-29
5
+chenqh721@foxmail.com
6
+https://github.com/chenqinghe
7
+
8
+
9
+## About the share
10
+
11
+原文地址: [Type Parameters Proposal](https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md)
12
+
13
+https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md
14
+
15
+- 当前版本的Go(1.18)并没有完全实现提案内容,可能有些许出入,会在后续版本中实现
16
+
17
+本分享适用人群:
18
+- 英文阅读有障碍的人群
19
+- 喜欢看视频学习知识的人
20
+- 晚上睡不着想找点白噪音的人
21
+
6
 
22
 
7
 
23
 
8
 ## Agenda
24
 ## Agenda
9
 - Why generic
25
 - Why generic
10
 - Type parameter
26
 - Type parameter
11
 - Constraint
27
 - Constraint
12
-- Some cases
28
+- More about type sets
13
 
29
 
14
 
30
 
15
 ## Why generic
31
 ## Why generic
69
 - 需指定类型参数,类型参数写在中括号内
85
 - 需指定类型参数,类型参数写在中括号内
70
 - 有多个类型参数时,使用逗号分隔
86
 - 有多个类型参数时,使用逗号分隔
71
 
87
 
88
+.code codes/print.go /CALL MULTI PARAMS OMIT/,/CALL MULTI PARAMS OMIT/
72
 
89
 
73
 ## Constraint
90
 ## Constraint
74
 
91
 
112
 
129
 
113
 ## Constraint
130
 ## Constraint
114
 
131
 
115
-首先看一个特殊的constraint:**any**,表示类型参数允许是任何类型。any约束下各类型(T)允许的操作有:
132
+在继续深入讨论约束之前,首先看一个特殊的constraint:**any**,表示类型参数允许是任何类型。any约束下各类型(T)允许的操作有:
116
 
133
 
117
 - 申明对应类型的变量
134
 - 申明对应类型的变量
118
 - 给此类型变量重新赋一个相同类型的值
135
 - 给此类型变量重新赋一个相同类型的值

+ 21
- 7
codes/print.go Parādīt failu

26
 func main(){
26
 func main(){
27
 	// CALL OMIT
27
 	// CALL OMIT
28
 	Print[int]([]int{1,2,3,4})
28
 	Print[int]([]int{1,2,3,4})
29
-	Print[int32](int32{1,2,3,4})
29
+	Print[int32]([]int32{1,2,3,4})
30
 	// CALL OMIT
30
 	// CALL OMIT
31
-	// CALL TYPE OMIT
32
-	Print([]int{1,2,3})
33
-	// CALL TYPE OMIT
34
-	// COMPLETE CALL OMIT
35
-	Print[int]([]int{1,2,3})
36
-	// COMPLETE CALL OMIT
31
+}
32
+
33
+
34
+/**
35
+// CALL MULTI PARAMS OMIT
36
+Map[string,int](map[string]int{"1":1})
37
+
38
+// CALL MULTI PARAMS OMIT
39
+
40
+
41
+*/
42
+
43
+
44
+func dummy(){
45
+		// CALL TYPE OMIT
46
+		Print([]int{1,2,3})
47
+		// CALL TYPE OMIT
48
+		// COMPLETE CALL OMIT
49
+		Print[int]([]int{1,2,3})
50
+		// COMPLETE CALL OMIT
37
 }
51
 }