第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何設計一個個性化的錯誤,然后對其進行測試(而不是錯誤中的描述)?

如何設計一個個性化的錯誤,然后對其進行測試(而不是錯誤中的描述)?

Go
炎炎設計 2022-08-24 18:45:25
在Python中,使用自己的“錯誤類型”引發(fā)異常(錯誤)的典型方法是class noMoreBeer(Exception):    passtry:    a_function()except noMoreBeer as e:    print("there is no more beer ({e}), go buy some")except Exception as e:    print(f"unexpected exception happened, namely {e}")else:    print("thinks went fine")我想移植到Go哲學的主要部分是,我創(chuàng)建了自己的例外,它可以有可選的解釋文本,但我檢查,而不是錯誤文本。noMoreBeer現(xiàn)在回到Go,我閱讀了幾頁關于如何處理錯誤的內(nèi)容(雖然我首先很生氣,但我現(xiàn)在發(fā)現(xiàn)它可以更好地編寫代碼),其中包括Go博客。為此,我試圖復制上述內(nèi)容,但下面的代碼不起作用(JetBrain的Goland指向和return Error.noMoreBeer()if err == Error.noMoreBeer())package mainimport "fmt"type Error interface {    error    noMoreBeer() bool}func thisFails() Error {    // returns my specific error, but could also percolate some other errors up    return Error.noMoreBeer()}func main() {    err := thisFails()    if err != nil {        if err == Error.noMoreBeer() {            fmt.Println("go buy some beer")        } else {            panic("something unexpected happened")        }    }}在 Go 中,有沒有辦法創(chuàng)建這樣的特定錯誤?在我的情況下,它們的主要驅(qū)動因素之一是我不依賴于錯誤中傳遞的文本,而是依賴于[類|無論什么],如果它有拼寫錯誤,那將是一個錯誤。
查看完整描述

4 回答

?
達令說

TA貢獻1821條經(jīng)驗 獲得超6個贊

您應該研究四個函數(shù)。這是這個主題的官方Go博客文章。它是在Go 1.13中引入的。

  • 騰訊網(wǎng).錯誤:創(chuàng)建一個包含一些詳細信息的新錯誤??梢允褂?包裝錯誤。%w

  • 錯誤。新增:創(chuàng)建一個新的錯誤類型,可以將其包裝并與 Go 1.13 中引入的函數(shù)進行比較。

  • 錯誤。是:將錯誤變量與錯誤類型進行比較。它可以解開錯誤包裝。

  • 錯誤。As:將錯誤變量與錯誤接口實現(xiàn)進行比較。它可以解開錯誤包裝。


查看完整回答
反對 回復 2022-08-24
?
婷婷同學_

TA貢獻1844條經(jīng)驗 獲得超8個贊

我第一次嘗試解決方案:


package main


import (

    "errors"

    "fmt"

)


var noMoreBeer = errors.New("no more beer")


func thisFails() error {

    // returns my specific error, but could also percolate some other errors up

    return noMoreBeer

}


func main() {

    err := thisFails()

    if err != nil {

        if err == noMoreBeer {

            fmt.Println("go buy some beer")

        } else {

            panic("something unexpected happened")

        }

    }

}

我理解閱讀 https://blog.golang.org/go1.13-errors 的關鍵點是,我可以針對類型為 .這在功能上等同于我開始使用的Python示例。error


查看完整回答
反對 回復 2022-08-24
?
猛跑小豬

TA貢獻1858條經(jīng)驗 獲得超8個贊

我們一直在生產(chǎn)中使用錯誤,通過cutome實現(xiàn)錯誤接口,沒有任何問題。這有助于檢測錯誤并更有效地找到根本原因。


package errors


type Error struct {

    err      error // the wrapped error

    errType  ErrorType

    errInfo  ErrorInfo

    op       Op


    // domain specific data

    userID    int

    requestID string

}


type (

    ErrorType string

    ErrorInfo string

    Op        string

)


var NoMoreBeer ErrorType = "NoMoreBeerError"


func (e Error) Error() string {

    return string(e.errInfo)

}


func Encode(op Op, args ...interface{}) error {

    e := Error{}


    for _, arg := range args {

        switch arg := arg.(type) {

        case error:

            e.err = arg

        case ErrorInfo:

            e.errInfo = arg

        case ErrorType:

            e.errType = arg

        case Op:

            e.op = arg

        case int:

            e.userID = arg

        case string:

            e.requestID = arg

        }

    }


    return e

}


func (e Error) GetErrorType() ErrorType {

    return e.errType

}

通過這種方式,您可以使用錯誤,但可以根據(jù)需要添加擴展功能。


e := errors.Encode(

    "pkg.beerservice.GetBeer", 

    errors.NoMoreBeer, 

    errors.ErrorInfo("No more beer available"),

).(errors.Error)


switch e.GetErrorType() {

case errors.NoMoreBeer:

    fmt.Println("no more beer error")

    fmt.Println(e.Error()) // this will return "No more beer available"

}

操場上的工作示例:https://play.golang.org/p/o9QnDOzTwpc


查看完整回答
反對 回復 2022-08-24
?
哈士奇WWW

TA貢獻1799條經(jīng)驗 獲得超6個贊

您可以使用類型開關。


package main


import "fmt"


// the interface

type NoMoreBeerError interface {

    noMoreBeer() bool

}


// two different implementations of the above interface

type noMoreBeerFoo struct { /* ... */ }

type noMoreBeerBar struct { /* ... */ }


func (noMoreBeerFoo) noMoreBeer() bool { return true }

func (noMoreBeerBar) noMoreBeer() bool { return false }


// have the types also implement the standard error interface

func (noMoreBeerFoo) Error() string { return " the foo's error message " }

func (noMoreBeerBar) Error() string { return " the bar's error message " }


func thisFails() error {

    if true {

        return noMoreBeerFoo{}

    } else if false {

        return noMoreBeerFoo{}

    }

    return fmt.Errorf("some other error")

}


func main() {

    switch err := thisFails().(type) {

    case nil: // all good


    case NoMoreBeerError: // my custom error type

        if err.noMoreBeer() { // you can invoke the method in this case block

            // ...

        }


        // if you need to you can inspect farther for the concrete types

        switch err.(type) {

        case noMoreBeerFoo:

            // ...

        case noMoreBeerBar:

            // ...

        }


    default:

        // handle other error type

    }

}


查看完整回答
反對 回復 2022-08-24
  • 4 回答
  • 0 關注
  • 113 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號