Compare commits
No commits in common. "fef15daccb66452daa366be6828529e8de78467e" and "bb8564ba49efb45419967aa0e4101acb7b6b75b3" have entirely different histories.
fef15daccb
...
bb8564ba49
@ -6,6 +6,6 @@
|
||||
|
||||
+ [Go语言函数](./function/README.md)
|
||||
+ [defer与异常](./error/README.md)
|
||||
+ [Go语言结构体](./struct/README.md)
|
||||
+ Go语言结构体
|
||||
+ Go语言指针
|
||||
+ 扩展反射与内存对齐
|
||||
@ -2,8 +2,6 @@
|
||||
|
||||
程序运行过程中,可能会有异常,比如: 输入参数错误,文件不存在,数据库连接失败等等
|
||||
|
||||
[defer与异常课件](https://gitee.com/infraboard/go-course/blob/master/zh-cn/base/error.md#defer%E7%9A%84%E5%BA%94%E7%94%A8)
|
||||
|
||||
## error (程序异常)
|
||||
|
||||
### 处理 error
|
||||
@ -158,56 +156,4 @@ func main() {
|
||||
|
||||
函数执行完成后,调用你的defer 函数进行 资源清理或者崩溃恢复
|
||||
|
||||
一个 流程(协程)里面,可以有个defer语句,声明 函数执行完成后的Hook调用
|
||||
|
||||
```go
|
||||
func main() {
|
||||
defer panicHandler()
|
||||
|
||||
// if r := recover(); r != nil {
|
||||
// fmt.Println("Recovered from panic:", r)
|
||||
// os.Exit(1)
|
||||
// }
|
||||
|
||||
// 变量目录, 打印了文件名称
|
||||
err := walkDir("./xxxx", func(filePath string) {
|
||||
fmt.Println(filePath)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
fmt.Println("not exist", err)
|
||||
} else {
|
||||
fmt.Printf("%s", err)
|
||||
}
|
||||
// os.Exit(1)
|
||||
}
|
||||
|
||||
// 初始化数据库,连接数据库
|
||||
defer func() {
|
||||
fmt.Println("关闭数据库连接")
|
||||
}()
|
||||
|
||||
// pannic
|
||||
// arrs := []int{1, 2, 3}
|
||||
// fmt.Println(arrs[4])
|
||||
|
||||
// if r := recover(); r != nil {
|
||||
// fmt.Println("Recovered from panic:", r)
|
||||
// os.Exit(1)
|
||||
// }
|
||||
|
||||
// 启动API服务器 HTTP Server
|
||||
defer func() {
|
||||
fmt.Println("关闭HTTP服务器")
|
||||
}()
|
||||
}
|
||||
|
||||
// not exist walkDir error
|
||||
// open ./xxxx: no such file or directory
|
||||
// 关闭HTTP服务器
|
||||
// 关闭数据库连接
|
||||
// panic handler
|
||||
```
|
||||
|
||||
|
||||
|
||||
@ -8,8 +8,7 @@ import (
|
||||
|
||||
var ErrFileNotFound = errors.New("file not found")
|
||||
|
||||
func panicHandler() {
|
||||
fmt.Println("panic handler")
|
||||
func recoverHandler() {
|
||||
if r := recover(); r != nil {
|
||||
fmt.Println("Recovered from panic:", r)
|
||||
os.Exit(1)
|
||||
@ -17,7 +16,7 @@ func panicHandler() {
|
||||
}
|
||||
|
||||
func main() {
|
||||
defer panicHandler()
|
||||
defer recoverHandler()
|
||||
|
||||
// if r := recover(); r != nil {
|
||||
// fmt.Println("Recovered from panic:", r)
|
||||
@ -38,26 +37,14 @@ func main() {
|
||||
// os.Exit(1)
|
||||
}
|
||||
|
||||
// 初始化数据库,连接数据库
|
||||
defer func() {
|
||||
// db.Close()
|
||||
fmt.Println("关闭数据库连接")
|
||||
}()
|
||||
|
||||
// pannic
|
||||
// arrs := []int{1, 2, 3}
|
||||
// fmt.Println(arrs[4])
|
||||
arrs := []int{1, 2, 3}
|
||||
fmt.Println(arrs[4])
|
||||
|
||||
// if r := recover(); r != nil {
|
||||
// fmt.Println("Recovered from panic:", r)
|
||||
// os.Exit(1)
|
||||
// }
|
||||
|
||||
// 启动API服务器 HTTP Server
|
||||
defer func() {
|
||||
// httpServer.Shutdown()
|
||||
fmt.Println("关闭HTTP服务器")
|
||||
}()
|
||||
}
|
||||
|
||||
// 最常见的就是 遍历目录里面的文件
|
||||
|
||||
@ -1,119 +1,86 @@
|
||||
<mxfile host="65bd71144e">
|
||||
<diagram id="sOwmlNTTQTTo7f71Gu_9" name="第 1 页">
|
||||
<mxGraphModel dx="1134" dy="527" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
|
||||
<mxGraphModel dx="1134" dy="585" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
|
||||
<root>
|
||||
<mxCell id="0"/>
|
||||
<mxCell id="1" parent="0"/>
|
||||
<mxCell id="2" value="CPU(物理核心, 超线程&lt;逻辑处理核心&gt;)" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxCell id="2" value="CPU(物理核心, 超线程&lt;逻辑处理核心&gt;)" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="140" y="180" width="620" height="290" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="3" value="处理核心" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxCell id="3" value="处理核心" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="190" y="350" width="130" height="80" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="4" value="<span style="color: rgb(0, 0, 0);">处理核心</span>" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxCell id="4" value="<span style="color: rgb(0, 0, 0);">处理核心</span>" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="580" y="350" width="130" height="80" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="6" value="OS 进程" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxCell id="6" value="OS 进程" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="190" y="210" width="540" height="90" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="7" value="线程" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxCell id="7" value="线程" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="214" y="235" width="100" height="40" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="8" value="线程" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxCell id="8" value="线程" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="510" y="235" width="100" height="40" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="9" value="线程" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxCell id="9" value="线程" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="610" y="235" width="100" height="40" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="10" value="线程" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxCell id="10" value="线程" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="314" y="235" width="100" height="40" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="18" style="edgeStyle=none;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" parent="1" source="17" target="8" edge="1">
|
||||
<mxCell id="18" style="edgeStyle=none;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" edge="1" parent="1" source="11" target="8">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="19" value="调度到操作系统的线程上去运行" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" parent="18" vertex="1" connectable="0">
|
||||
<mxCell id="19" value="调度到操作系统的线程上去运行" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="18">
|
||||
<mxGeometry x="-0.2478" y="1" relative="1" as="geometry">
|
||||
<mxPoint as="offset"/>
|
||||
</mxGeometry>
|
||||
</mxCell>
|
||||
<mxCell id="11" value="" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxCell id="11" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="140" y="30" width="620" height="110" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="12" value="OS" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" parent="1" vertex="1">
|
||||
<mxCell id="12" value="OS" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="40" y="275" width="60" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="13" value="Runtime" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" parent="1" vertex="1">
|
||||
<mxCell id="13" value="Runtime" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="40" y="70" width="60" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="14" value="协程" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxCell id="14" value="协程" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="204" y="70" width="116" height="40" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="15" value="<span style="color: rgb(0, 0, 0);">协程</span>" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxCell id="15" value="<span style="color: rgb(0, 0, 0);">协程</span>" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="320" y="70" width="116" height="40" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="16" value="<span style="color: rgb(0, 0, 0);">协程</span>" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxCell id="16" value="<span style="color: rgb(0, 0, 0);">协程</span>" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="436" y="70" width="116" height="40" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="17" value="<span style="color: rgb(0, 0, 0);">协程(main)</span>" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxCell id="17" value="<span style="color: rgb(0, 0, 0);">协程</span>" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="552" y="70" width="116" height="40" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="20" value="main()" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxCell id="20" value="main()" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="286" y="590" width="150" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="22" style="edgeStyle=none;html=1;exitX=0;exitY=0.5;exitDx=0;exitDy=0;entryX=1;entryY=0.5;entryDx=0;entryDy=0;" parent="1" source="21" target="20" edge="1">
|
||||
<mxCell id="22" style="edgeStyle=none;html=1;exitX=0;exitY=0.5;exitDx=0;exitDy=0;entryX=1;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="21" target="20">
|
||||
<mxGeometry relative="1" as="geometry">
|
||||
<mxPoint x="450" y="620" as="targetPoint"/>
|
||||
</mxGeometry>
|
||||
</mxCell>
|
||||
<mxCell id="24" style="edgeStyle=none;html=1;exitX=0;exitY=0.75;exitDx=0;exitDy=0;" parent="1" source="21" edge="1">
|
||||
<mxCell id="24" style="edgeStyle=none;html=1;exitX=0;exitY=0.75;exitDx=0;exitDy=0;" edge="1" parent="1" source="21">
|
||||
<mxGeometry relative="1" as="geometry">
|
||||
<mxPoint x="440" y="690" as="targetPoint"/>
|
||||
</mxGeometry>
|
||||
</mxCell>
|
||||
<mxCell id="25" value="defer hook" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" parent="24" vertex="1" connectable="0">
|
||||
<mxCell id="25" value="defer hook" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="24">
|
||||
<mxGeometry x="0.1663" y="4" relative="1" as="geometry">
|
||||
<mxPoint as="offset"/>
|
||||
</mxGeometry>
|
||||
</mxCell>
|
||||
<mxCell id="21" value="go runtime" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxCell id="21" value="go runtime" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="570" y="570" width="200" height="120" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="23" value="recover" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxCell id="23" value="recover" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="289" y="660" width="150" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="26" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="106" y="870" width="214" height="240" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="27" value="main" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="40" y="940" width="60" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="28" value="defer 处理崩溃" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="166" y="910" width="120" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="29" value="defer 处理数据库关闭" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="166" y="960" width="120" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="30" value="defer 处理Api Server关闭(http server)" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="166" y="1010" width="120" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="31" value="" style="shape=flexArrow;endArrow=classic;html=1;" edge="1" parent="1">
|
||||
<mxGeometry width="50" height="50" relative="1" as="geometry">
|
||||
<mxPoint x="380" y="870" as="sourcePoint"/>
|
||||
<mxPoint x="380" y="1100" as="targetPoint"/>
|
||||
</mxGeometry>
|
||||
</mxCell>
|
||||
<mxCell id="36" value="声明" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="379" y="975" width="60" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="38" value="" style="shape=flexArrow;endArrow=classic;html=1;" edge="1" parent="1">
|
||||
<mxGeometry width="50" height="50" relative="1" as="geometry">
|
||||
<mxPoint x="470" y="1090" as="sourcePoint"/>
|
||||
<mxPoint x="470" y="870" as="targetPoint"/>
|
||||
</mxGeometry>
|
||||
</mxCell>
|
||||
<mxCell id="39" value="调用顺序" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="492" y="975" width="60" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
</root>
|
||||
</mxGraphModel>
|
||||
</diagram>
|
||||
|
||||
@ -6,134 +6,26 @@
|
||||
|
||||
结构体就是用于解决这个问题的, 结构体是由一系列具有相同类型或不同类型的数据构成的数据集合, 方便容量我们的任意类型的数据
|
||||
|
||||
[课件地址](https://gitee.com/infraboard/go-course/blob/master/zh-cn/base/struct.md)
|
||||
|
||||
## 结构体的定义
|
||||
|
||||
|
||||
## 数据结构
|
||||
|
||||
+ 基础数据结构: int, float, bool, string, byte(int8), rune
|
||||
+ 复合数据结构: array, sice, map
|
||||
|
||||
处理人员相关信息
|
||||
```sh
|
||||
单独变量方式:
|
||||
├── name (string) - 姓名
|
||||
├── age (int) - 年龄
|
||||
├── gender (string) - 性别
|
||||
├── weight (uint) - 体重
|
||||
└── favoriteColor ([]string) - 喜欢的颜色
|
||||
```
|
||||
## 指针数组(重要: 95%)
|
||||
|
||||
指针数组: 创建一个数组,里面存放指针,指针指向数组的某一个元素
|
||||
|
||||
盒子(数组)里面的插槽,嵌套盒子(指针)的模式
|
||||
|
||||
|
||||
```go
|
||||
var (
|
||||
name string
|
||||
age int
|
||||
gender string
|
||||
weight uint
|
||||
favoriteColor []string
|
||||
)
|
||||
type Person struct {}
|
||||
|
||||
func handlePerson(name string, age int, gender string, weight uint, favoriteColor []string) error
|
||||
//
|
||||
[]*Person{}
|
||||
```
|
||||
|
||||
根据业务需要自己进行 数据定义,这就是结构体(struct)
|
||||
|
||||
```go
|
||||
type Person struct {
|
||||
Name string
|
||||
Age int
|
||||
Gender string
|
||||
Weight uint
|
||||
FavoriteColor []string
|
||||
}
|
||||
|
||||
|
||||
func handlePerson(*Person) error
|
||||
```
|
||||
|
||||
```go
|
||||
// person 结构体类型的初始化
|
||||
// 把struct上这些变量 赋值的过程 就叫实例化
|
||||
p1 := Person{
|
||||
Name: "张三",
|
||||
Age: 18,
|
||||
Gender: GenderMale,
|
||||
Weight: 150,
|
||||
FavoriteColor: []string{
|
||||
"红色",
|
||||
"绿色",
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
## 声明与初始化
|
||||
|
||||
```go
|
||||
// 定义类型(struct)
|
||||
type Person struct {
|
||||
Name string
|
||||
Age int
|
||||
Gender string
|
||||
Weight uint
|
||||
FavoriteColor []string
|
||||
}
|
||||
|
||||
// 声明并初始化
|
||||
// var Person
|
||||
p2 := Person{
|
||||
Name: "李四",
|
||||
Age: 20,
|
||||
Gender: GenderFemale,
|
||||
Weight: 120,
|
||||
FavoriteColor: []string{
|
||||
"蓝色",
|
||||
"黑色",
|
||||
},
|
||||
}
|
||||
// var *Person
|
||||
p3 := &Person{
|
||||
Name: "李四",
|
||||
Age: 20,
|
||||
Gender: GenderFemale,
|
||||
Weight: 120,
|
||||
FavoriteColor: []string{
|
||||
"蓝色",
|
||||
"黑色",
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
## 使用结构体
|
||||
|
||||
可以参考map的使用方式, 或者一组带名称空间的变量
|
||||
|
||||
```go
|
||||
// 访问结构体字段
|
||||
fmt.Println(p2.age)
|
||||
// 修改结构体字段
|
||||
p2.age = 22
|
||||
fmt.Println(p2.age)
|
||||
```
|
||||
|
||||
## 指针
|
||||
|
||||
自动解开引用
|
||||
```go
|
||||
// ChangePersonAge 修改Person的年龄
|
||||
func ChangePersonAge(p *Person, newAge int) {
|
||||
// 自动解开引用(*p)
|
||||
// * -> 解引用(Unbox)
|
||||
// (*p).age = newAge
|
||||
// p 指针类型, 自动解引用
|
||||
// . 访问字段
|
||||
// 自动解引用
|
||||
// p.age == (*p).age
|
||||
// 所以可以直接写成下面这样
|
||||
p.age = newAge
|
||||
}
|
||||
|
||||
// func ChangeAge(age *int, newAge int) {
|
||||
// *age = newAge
|
||||
// }
|
||||
```
|
||||
## 指针数组的拷贝
|
||||
|
||||
|
||||
@ -1,94 +0,0 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
|
||||
// slice 类型的初始化
|
||||
arr := []int{1, 2, 3}
|
||||
fmt.Println(arr)
|
||||
|
||||
// map类型的初始化
|
||||
m1 := map[string]int{
|
||||
"张三": 18,
|
||||
"李四": 20,
|
||||
}
|
||||
fmt.Println(m1)
|
||||
|
||||
// person 结构体类型的初始化
|
||||
p1 := Person{
|
||||
Name: "张三",
|
||||
age: 18,
|
||||
Gender: GenderMale,
|
||||
Weight: 150,
|
||||
FavoriteColor: []string{
|
||||
"红色",
|
||||
"绿色",
|
||||
},
|
||||
}
|
||||
fmt.Println(p1)
|
||||
|
||||
p2 := Person{
|
||||
Name: "李四",
|
||||
age: 20,
|
||||
Gender: GenderFemale,
|
||||
Weight: 120,
|
||||
FavoriteColor: []string{
|
||||
"蓝色",
|
||||
"黑色",
|
||||
},
|
||||
}
|
||||
// p3 := Person{}
|
||||
// p3 := new(Person)
|
||||
fmt.Println(p2)
|
||||
|
||||
// 访问结构体字段
|
||||
fmt.Println(p2.age)
|
||||
// 修改结构体字段
|
||||
p2.age = 22
|
||||
fmt.Println(p2.age)
|
||||
|
||||
ChangePersonAge(&p2, 30)
|
||||
fmt.Println("ChangePersonAge", p2.age)
|
||||
}
|
||||
|
||||
// Person 是一个表示人的结构体,包含姓名、年龄、性别、体重和喜欢的颜色等字段。
|
||||
// 也是一个复合数据类型。
|
||||
type Person struct {
|
||||
Name string
|
||||
// 不对外暴露的字段
|
||||
age int
|
||||
Gender Gender
|
||||
Weight uint
|
||||
FavoriteColor []string
|
||||
}
|
||||
|
||||
// GenderXXX 表示枚举值
|
||||
// Gender 是一种自定义类型,底层类型是 int
|
||||
// Gender != int
|
||||
type Gender int
|
||||
|
||||
// Sex == Gender
|
||||
type Sex = Gender
|
||||
|
||||
const (
|
||||
GenderMale Gender = iota
|
||||
GenderFemale
|
||||
)
|
||||
|
||||
// ChangePersonAge 修改Person的年龄
|
||||
func ChangePersonAge(p *Person, newAge int) {
|
||||
// 自动解开引用(*p)
|
||||
// * -> 解引用(Unbox)
|
||||
// (*p).age = newAge
|
||||
// p 指针类型, 自动解引用
|
||||
// . 访问字段
|
||||
// 自动解引用
|
||||
// p.age == (*p).age
|
||||
// 所以可以直接写成下面这样
|
||||
p.age = newAge
|
||||
}
|
||||
|
||||
// func ChangeAge(age *int, newAge int) {
|
||||
// *age = newAge
|
||||
// }
|
||||
Loading…
x
Reference in New Issue
Block a user