```
docs(error): 更新defer机制文档并添加实际代码示例 - 修改panicHandler函数名称替换recoverHandler - 在README.md中添加完整的defer执行流程代码示例 - 展示数据库连接和HTTP服务器关闭的defer应用场景 - 更新drawio图表展示defer hook调用机制和执行顺序 - 添加多个defer语句的实际应用案例 ```
This commit is contained in:
parent
bb8564ba49
commit
2e443239a1
@ -156,4 +156,56 @@ 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,7 +8,8 @@ import (
|
||||
|
||||
var ErrFileNotFound = errors.New("file not found")
|
||||
|
||||
func recoverHandler() {
|
||||
func panicHandler() {
|
||||
fmt.Println("panic handler")
|
||||
if r := recover(); r != nil {
|
||||
fmt.Println("Recovered from panic:", r)
|
||||
os.Exit(1)
|
||||
@ -16,7 +17,7 @@ func recoverHandler() {
|
||||
}
|
||||
|
||||
func main() {
|
||||
defer recoverHandler()
|
||||
defer panicHandler()
|
||||
|
||||
// if r := recover(); r != nil {
|
||||
// fmt.Println("Recovered from panic:", r)
|
||||
@ -37,14 +38,26 @@ 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,86 +1,119 @@
|
||||
<mxfile host="65bd71144e">
|
||||
<diagram id="sOwmlNTTQTTo7f71Gu_9" name="第 1 页">
|
||||
<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">
|
||||
<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">
|
||||
<root>
|
||||
<mxCell id="0"/>
|
||||
<mxCell id="1" parent="0"/>
|
||||
<mxCell id="2" value="CPU(物理核心, 超线程&lt;逻辑处理核心&gt;)" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxCell id="2" value="CPU(物理核心, 超线程&lt;逻辑处理核心&gt;)" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxGeometry x="140" y="180" width="620" height="290" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="3" value="处理核心" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxCell id="3" value="处理核心" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="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;" vertex="1" parent="1">
|
||||
<mxCell id="4" value="<span style="color: rgb(0, 0, 0);">处理核心</span>" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="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;" vertex="1" parent="1">
|
||||
<mxCell id="6" value="OS 进程" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxGeometry x="190" y="210" width="540" height="90" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="7" value="线程" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxCell id="7" value="线程" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxGeometry x="214" y="235" width="100" height="40" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="8" value="线程" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxCell id="8" value="线程" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxGeometry x="510" y="235" width="100" height="40" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="9" value="线程" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxCell id="9" value="线程" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxGeometry x="610" y="235" width="100" height="40" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="10" value="线程" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxCell id="10" value="线程" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="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;" edge="1" parent="1" source="11" target="8">
|
||||
<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">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="19" value="调度到操作系统的线程上去运行" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="18">
|
||||
<mxCell id="19" value="调度到操作系统的线程上去运行" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" parent="18" vertex="1" connectable="0">
|
||||
<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;" vertex="1" parent="1">
|
||||
<mxCell id="11" value="" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="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;" vertex="1" parent="1">
|
||||
<mxCell id="12" value="OS" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" parent="1" vertex="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;" vertex="1" parent="1">
|
||||
<mxCell id="13" value="Runtime" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" parent="1" vertex="1">
|
||||
<mxGeometry x="40" y="70" width="60" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="14" value="协程" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxCell id="14" value="协程" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="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;" vertex="1" parent="1">
|
||||
<mxCell id="15" value="<span style="color: rgb(0, 0, 0);">协程</span>" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="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;" vertex="1" parent="1">
|
||||
<mxCell id="16" value="<span style="color: rgb(0, 0, 0);">协程</span>" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxGeometry x="436" y="70" width="116" height="40" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="17" value="<span style="color: rgb(0, 0, 0);">协程</span>" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxCell id="17" value="<span style="color: rgb(0, 0, 0);">协程(main)</span>" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="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;" vertex="1" parent="1">
|
||||
<mxCell id="20" value="main()" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="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;" edge="1" parent="1" source="21" target="20">
|
||||
<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">
|
||||
<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;" edge="1" parent="1" source="21">
|
||||
<mxCell id="24" style="edgeStyle=none;html=1;exitX=0;exitY=0.75;exitDx=0;exitDy=0;" parent="1" source="21" edge="1">
|
||||
<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=[];" vertex="1" connectable="0" parent="24">
|
||||
<mxCell id="25" value="defer hook" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" parent="24" vertex="1" connectable="0">
|
||||
<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;" vertex="1" parent="1">
|
||||
<mxCell id="21" value="go runtime" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="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;" vertex="1" parent="1">
|
||||
<mxCell id="23" value="recover" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="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>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user