<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                這是所有自定義包實現者應該遵守的最佳實踐: 1)在包內部,總是應該從 panic 中 recover:不允許顯式的超出包范圍的 panic() 2)向包的調用者返回錯誤值(而不是 panic)。 在包內部,特別是在非導出函數中有很深層次的嵌套調用時,對主調函數來說用 panic 來表示應該被翻譯成錯誤的錯誤場景是很有用的(并且提高了代碼可讀性)。 這在下面的代碼中被很好地闡述了。我們有一個簡單的 parse 包(示例 13.4)用來把輸入的字符串解析為整數切片;這個包有自己特殊的?`ParseError`。 當沒有東西需要轉換或者轉換成整數失敗時,這個包會 panic(在函數 fields2numbers 中)。但是可導出的 Parse 函數會從 panic 中 recover 并用所有這些信息返回一個錯誤給調用者。為了演示這個過程,在 panic_recover.go 中 調用了 parse 包(示例 13.4);不可解析的字符串會導致錯誤并被打印出來。 示例 13.4?[parse.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_13/parse/parse.go): ~~~ // parse.go package parse import ( "fmt" "strings" "strconv" ) // A ParseError indicates an error in converting a word into an integer. type ParseError struct { Index int // The index into the space-separated list of words. Word string // The word that generated the parse error. Err error // The raw error that precipitated this error, if any. } // String returns a human-readable error message. func (e *ParseError) String() string { return fmt.Sprintf("pkg parse: error parsing %q as int", e.Word) } // Parse parses the space-separated words in in put as integers. func Parse(input string) (numbers []int, err error) { defer func() { if r := recover(); r != nil { var ok bool err, ok = r.(error) if !ok { err = fmt.Errorf("pkg: %v", r) } } }() fields := strings.Fields(input) numbers = fields2numbers(fields) return } func fields2numbers(fields []string) (numbers []int) { if len(fields) == 0 { panic("no words to parse") } for idx, field := range fields { num, err := strconv.Atoi(field) if err != nil { panic(&ParseError{idx, field, err}) } numbers = append(numbers, num) } return } ~~~ 示例 13.5?[panic_package.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_13/panic_package.go): ~~~ // panic_package.go package main import ( "fmt" "./parse/parse" ) func main() { var examples = []string{ "1 2 3 4 5", "100 50 25 12.5 6.25", "2 + 2 = 4", "1st class", "", } for _, ex := range examples { fmt.Printf("Parsing %q:\n ", ex) nums, err := parse.Parse(ex) if err != nil { fmt.Println(err) // here String() method from ParseError is used continue } fmt.Println(nums) } } ~~~ 輸出: ~~~ Parsing "1 2 3 4 5": [1 2 3 4 5] Parsing "100 50 25 12.5 6.25": pkg parse: error parsing "12.5" as int Parsing "2 + 2 = 4": pkg parse: error parsing "+" as int Parsing "1st class": pkg parse: error parsing "1st" as int Parsing "": pkg: no words to parse ~~~
                  <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>

                              哎呀哎呀视频在线观看