<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                關鍵字 defer 允許我們推遲到函數返回之前(或任意位置執行?`return`?語句之后)一刻才執行某個語句或函數(為什么要在返回之后才執行這些語句?因為?`return`?語句同樣可以包含一些操作,而不是單純地返回某個值)。 關鍵字 defer 的用法類似于面向對象編程語言 Java 和 C# 的?`finally`?語句塊,它一般用于釋放某些已分配的資源。 示例 6.8?[defer.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_6/defer.go): ~~~ package main import "fmt" func main() { Function1() } func Function1() { fmt.Printf("In Function1 at the top\n") defer Function2() fmt.Printf("In Function1 at the bottom!\n") } func Function2() { fmt.Printf("Function2: Deferred until the end of the calling function!") } ~~~ 輸出: ~~~ In Function1 at the top In Function1 at the bottom! Function2: Deferred until the end of the calling function! ~~~ 請將 defer 關鍵字去掉并對比輸出結果。 使用 defer 的語句同樣可以接受參數,下面這個例子就會在執行 defer 語句時打印?`0`: ~~~ func a() { i := 0 defer fmt.Println(i) i++ return } ~~~ 當有多個 defer 行為被注冊時,它們會以逆序執行(類似棧,即后進先出): ~~~ func f() { for i := 0; i < 5; i++ { defer fmt.Printf(“%d “, i) } } ~~~ 上面的代碼將會輸出:`4 3 2 1 0`。 關鍵字 defer 允許我們進行一些函數執行完成后的收尾工作,例如: 1. 關閉文件流: // open a file defer file.Close() (詳見第 12.2 節) 2. 解鎖一個加鎖的資源 mu.Lock() defer mu.Unlock() (詳見第 9.3 節) 3. 打印最終報告 printHeader() defer printFooter() 4. 關閉數據庫鏈接 // open a database connection defer disconnectFromDB() 合理使用 defer 語句能夠使得代碼更加簡潔。 以下代碼模擬了上面描述的第 4 種情況: ~~~ package main import "fmt" func main() { doDBOperations() } func connectToDB() { fmt.Println("ok, connected to db") } func disconnectFromDB() { fmt.Println("ok, disconnected from db") } func doDBOperations() { connectToDB() fmt.Println("Defering the database disconnect.") defer disconnectFromDB() //function called here with defer fmt.Println("Doing some DB operations ...") fmt.Println("Oops! some crash or network error ...") fmt.Println("Returning from function here!") return //terminate the program // deferred function executed here just before actually returning, even if // there is a return or abnormal termination before } ~~~ 輸出: ~~~ ok, connected to db Defering the database disconnect. Doing some DB operations ... Oops! some crash or network error ... Returning from function here! ok, disconnected from db ~~~ **使用 defer 語句實現代碼追蹤** 一個基礎但十分實用的實現代碼執行追蹤的方案就是在進入和離開某個函數打印相關的消息,即可以提煉為下面兩個函數: ~~~ func trace(s string) { fmt.Println("entering:", s) } func untrace(s string) { fmt.Println("leaving:", s) } ~~~ 以下代碼展示了何時調用兩個函數: 示例 6.10?[defer_tracing.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_6/defer_tracing.go): ~~~ package main import "fmt" func trace(s string) { fmt.Println("entering:", s) } func untrace(s string) { fmt.Println("leaving:", s) } func a() { trace("a") defer untrace("a") fmt.Println("in a") } func b() { trace("b") defer untrace("b") fmt.Println("in b") a() } func main() { b() } ~~~ 輸出: ~~~ entering: b in b entering: a win a leaving: a leaving: b ~~~ 上面的代碼還可以修改為更加簡便的版本(示例 6.11?[defer_tracing2.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_6/defer_tracing2.go)): ~~~ package main import "fmt" func trace(s string) string { fmt.Println("entering:", s) return s } func un(s string) { fmt.Println("leaving:", s) } func a() { defer un(trace("a")) fmt.Println("in a") } func b() { defer un(trace("b")) fmt.Println("in b") a() } func main() { b() } ~~~ **使用 defer 語句來記錄函數的參數與返回值** 下面的代碼展示了另一種在調試時使用 defer 語句的手法(示例 6.12?[defer_logvalues.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_6/defer_logvalues.go)): ~~~ package main import ( "io" "log" ) func func1(s string) (n int, err error) { defer func() { log.Printf("func1(%q) = %d, %v", s, n, err) }() return 7, io.EOF } func main() { func1("Go") } ~~~ 輸出: ~~~ Output: 2011/10/04 10:46:11 func1(“Go”) = 7, EOF ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看