File tree Expand file tree Collapse file tree 2 files changed +89
-0
lines changed
Expand file tree Collapse file tree 2 files changed +89
-0
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,53 @@ type handle func (str string)
3939
4040```
4141
42+ ###### Type aliases and type definitions (Distinction)
43+
44+ Define built-in type definitions prior to Go 1.9:
45+ ```
46+ type byte uint8
47+ type rune int32
48+ ```
49+
50+ After Go 1.9, it becomes:
51+ ```
52+ type byte = uint8
53+ type rune = int32
54+ ```
55+ This change is made in conjunction with the type alias.
56+
57+ The definition of a type alias is written as:
58+ ```
59+ type TypeAlias = Type
60+ ```
61+ A Type alias specifies that a TypeAlias is an alias to a Type; essentially, a TypeAlias is the same Type as a Type.
62+
63+ ```
64+ package main
65+ import (
66+ "fmt"
67+ )
68+ // Define NewInt as int
69+ type NewInt int
70+ // Use an int as IntAlias
71+ type IntAlias = int
72+ func main() {
73+ // Declare A as type NewInt
74+ var a NewInt
75+ // View the type name of A
76+ fmt.Printf("a type: %T\n", a)
77+ Declare A2 as type IntAlias
78+ var a2 IntAlias
79+ // View the type name of A2
80+ fmt.Printf("a2 type: %T\n", a2)
81+ }
82+
83+ // a type: main.NewInt
84+ // a2 type: int
85+
86+ ```
87+
88+
4289###### Type Switch
4390```
4491func Demo (params ... interface (}) {
Original file line number Diff line number Diff line change @@ -33,12 +33,54 @@ type Demo string
3333
3434```
3535
36+
3637###### 类型定义
3738```
3839type handle func(str string)
3940
4041```
4142
43+ ###### 类型别名和类型定义(区别)
44+
45+ 在 Go 1.9 版本之前定义内建类型的定义:
46+ type byte uint8
47+ type rune int32
48+
49+ 而在 Go 1.9 版本之后变为:
50+ type byte = uint8
51+ type rune = int32
52+ 这个修改就是配合类型别名而进行的修改。
53+
54+ 定义类型别名的写法为:
55+ type TypeAlias = Type
56+ 类型别名规定:TypeAlias 只是 Type 的别名,本质上 TypeAlias 与 Type 是同一个类型
57+
58+ ```
59+ package main
60+ import (
61+ "fmt"
62+ )
63+ // 将NewInt定义为int类型
64+ type NewInt int
65+ // 将int取一个别名叫IntAlias
66+ type IntAlias = int
67+ func main() {
68+ // 将a声明为NewInt类型
69+ var a NewInt
70+ // 查看a的类型名
71+ fmt.Printf("a type: %T\n", a)
72+ // 将a2声明为IntAlias类型
73+ var a2 IntAlias
74+ // 查看a2的类型名
75+ fmt.Printf("a2 type: %T\n", a2)
76+ }
77+
78+ // a type: main.NewInt
79+ // a2 type: int
80+
81+ ```
82+
83+
4284###### 类型开关
4385```
4486func Demo(params ...interface{}) {
You can’t perform that action at this time.
0 commit comments