亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如何在 Go 中創建多級錯誤子類型

如何在 Go 中創建多級錯誤子類型

Go
慕少森 2023-06-26 15:32:06
我試圖在 GO 中創建錯誤的子類型。現在我面臨著多種類型的問題。以下代碼顯示了錯誤類型定義:/* Interfaces */type UniversalError interface {? ? CommonError1}type CommonError1 interface {? ? error? ? CommonError1()}/* Structs */type Error1 struct {? ? reason string}type Error2 struct {? ? reason string}type Error3 struct {? ? reason string}/* Interface function implementations */func (error1 Error1) Error() string {? ? return fmt.Sprintf(error1.reason)}func (error2 Error2) Error() string {? ? return fmt.Sprintf(error2.reason)}func (error3 Error3) Error() string {? ? return fmt.Sprintf(error3.reason)}func (Error1) CommonError1() {}func (Error2) CommonError1() {}func (Error3) UniversalError() {}當我嘗試運行以下代碼時:func main() {? ? var err1 = Error1{reason: "Error reason 1"}? ? var err2 = Error2{reason: "Error reason 2"}? ? var err3 = Error3{reason: "Error reason 3"}? ? fmt.Println("\n**** Types *****")? ? printType(err1)? ? printType(err2)? ? printType(err3)}func printType(param error) {? ? switch param.(type) {? ? case UniversalError:? ? ? ? switch param.(type) {? ? ? ? case CommonError1:? ? ? ? ? ? switch param.(type) {? ? ? ? ? ? case Error1:? ? ? ? ? ? ? ? fmt.Println("Error1 found")? ? ? ? ? ? case Error2:? ? ? ? ? ? ? ? fmt.Println("Error2 found")? ? ? ? ? ? default:? ? ? ? ? ? ? ? fmt.Println("CommonError1 found, but Does not belong to Error1 or Error2")? ? ? ? ? ? }? ? ? ? default:? ? ? ? ? ? fmt.Println("Error3 Found")? ? ? ? }? ? default:? ? ? ? fmt.Println("Error belongs to an unidentified type")? ? }}該printType()函數打印以下內容:**** Types *****Error1 foundError2 foundCommonError1 found, but Does not belong to Error1 or Error2我需要將 的類型Error3識別為 an UniversalError,而不是 a CommonError1。我怎樣才能做到這一點?我的做法有什么問題嗎?
查看完整描述

1 回答

?
HUH函數

TA貢獻1836條經驗 獲得超4個贊

您使用該UniversalError()方法,但沒有將其添加到接口“定義”中,因此請執行以下操作:


type UniversalError interface {

    CommonError1

    UniversalError()

}

而你想Error3成為一個UniversalError. 要Error3成為UniversalError,它必須實現其所有方法:UniversalError()和CommonError1()。所以你必須添加這兩個方法:


func (Error3) CommonError1()   {}

func (Error3) UniversalError() {}

通過這些更改,輸出將是(在Go Playground上嘗試):


**** Types *****

Error belongs to an unidentified type

Error belongs to an unidentified type

CommonError1 found, but Does not belong to Error1 or Error2

提示:如果您希望編譯時保證某些具體類型實現某些接口,請使用空白變量聲明,如下所示:


var _ UniversalError = Error3{}

上面的聲明將 的值賦給Error3類型為 的變量UniversalError。不應該Error3滿足UniversalError,您會收到編譯時錯誤。上面的聲明不會引入新變量,因為使用了空白標識符,這只是編譯時檢查。


如果您要刪除該Error3.CommonError1()方法:


//func (Error3) CommonError1()   {}

func (Error3) UniversalError() {}

然后你會立即得到一個編譯時錯誤:


./prog.go:49:5: cannot use Error3 literal (type Error3) as type UniversalError in assignment:

    Error3 does not implement UniversalError (missing CommonError1 method)



查看完整回答
反對 回復 2023-06-26
  • 1 回答
  • 0 關注
  • 174 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號