當前位置:
首頁 >
Go语言笔记3
發布時間:2025/3/15
23
豆豆
正文
package main //資源管理與出錯處理 //CACHT ALL THE ERRORS import( "fmt" )func tryDefer(){defer fmt.Println(1)fmt.Println(2)panic("error occurred")} func writerFile(){ file,err:=os.Create(filename,os.O_EXCL,0666) if err!=nil{if pathError,ok:=err.(*os.PathError);!ok{panic(err)}else{fmt.Println(pathError.Op,PathError.Path,PathError.Err)}return } defer file.Close() //lock unlockwriter:=bufio.NewWriter(file)//這個時刻創建副本保存參數//defe的執行順序類似于棧,現金后出。defer writer.Flush()for i:=0;i<20;i++{fmt.Fprintln(writer,[]byte("1"))}}func main(){tryDefer() }總結
- 上一篇: mmd python error_pyt
- 下一篇: GO语言笔记4