<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國際加速解決方案。 廣告
                ## 錯誤接口 Error接口看起來非常簡潔: ``` type Error interface{ Error() string } ``` 這個接口很容易滿足,不幸的是,這也很容易導致需要根據收到的錯誤與采取操作的包的混淆。 在Go中有很多方法可以創建錯誤,本節將探討基本錯誤的創建,已分配值或類型的錯誤,以及使用結構的自定義錯誤。 ### 實踐 1. 創建basicerrors.go: ``` package basicerrors import ( "errors" "fmt" ) // ErrorValue創建了包級錯誤 // 可以采用這樣的方式判斷: if err == ErrorValue var ErrorValue = errors.New("this is a typed error") // TypedError創建了包含錯誤類型的匿名字段 // 可以采用斷言的方式判斷:err.(type) == ErrorValue type TypedError struct { error } //BasicErrors 演示了錯誤的創建 func BasicErrors() { err := errors.New("this is a quick and easy way to create an error") fmt.Println("errors.New: ", err) err = fmt.Errorf("an error occurred: %s", "something") fmt.Println("fmt.Errorf: ", err) err = ErrorValue fmt.Println("value error: ", err) err = TypedError{errors.New("typed error")} fmt.Println("typed error: ", err) } ``` 2. 創建custom.go: ``` package basicerrors import ( "fmt" ) // CustomError 實現了Error接口 type CustomError struct { Result string } func (c CustomError) Error() string { return fmt.Sprintf("there was an error; %s was the result", c.Result) } // SomeFunc 返回一個 error func SomeFunc() error { c := CustomError{Result: "this"} return c } ``` 3. 創建 main.go: ``` package main import ( "fmt" "github.com/agtorre/go-cookbook/chapter4/basicerrors" ) func main() { basicerrors.BasicErrors() err := basicerrors.SomeFunc() fmt.Println("custom error: ", err) } ``` 4. 這會輸出: ``` errors.New: this is a quick and easy way to create an error fmt.Errorf: an error occurred: something typed error: this is a typed error custom error: there was an error; this was the result ``` ### 說明 無論你是使用errors.New,fmt.Errorf還是自定義錯誤,最重要的是你應該永遠不要在代碼中忽略錯誤。這些不同的錯誤定義方法提供了很大的靈活性。例如,你可以在結構中添加額外的函數,以進一步查看可能發生的錯誤并將接口轉換為調用函數中的錯誤類型,以獲得一些額外的功能。 錯誤接口本身非常簡單,唯一的要求是返回一個有效的字符串。將它連接到一個結構可能對某些高級應用程序很有用,這些應用程序在整個過程中都有統一的錯誤處理,同時也希望能夠與其他應用程序進行良好地協作。 * * * * 學識淺薄,錯誤在所難免。歡迎在群中就本書提出修改意見,以饗后來者,長風拜謝。 Golang中國(211938256) beego實戰(258969317) Go實踐(386056972)
                  <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>

                              哎呀哎呀视频在线观看