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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

正確處理golang中的context.deadlineExceededError

正確處理golang中的context.deadlineExceededError

Go
FFIVE 2022-12-19 20:33:27
有沒(méi)有辦法在異常處理開關(guān)中按類型匹配 golang 上下文超時(shí)錯(cuò)誤?switch err.(type)case context.deadlineExceededError:  fmt.Println("deadline exceeded")}不會(huì)編譯,由于cannot refer to unexported name context.deadlineExceededErrorAFAICS 由于類型未導(dǎo)出,因此只能通過(guò)字符串比較來(lái)處理。package mainimport (        "context"        "fmt"        "time")const shortDuration = 1 * time.Secondfunc main() {        ctx, cancel := context.WithTimeout(context.Background(), shortDuration)        defer cancel()        select {        case <-time.After(shortDuration*2):                fmt.Println("overslept")        case <-ctx.Done():                err := ctx.Err()                switch err.(type) {                case context.deadlineExceededError:                        fmt.Println("deadline")                }                fmt.Printf("error %T: %v\n", err, err)        }}$ go build main.go# command-line-arguments./main.go:21:8: cannot refer to unexported name context.deadlineExceededErrorvs 字符串比較        case <-ctx.Done():                err := ctx.Err()                errType := fmt.Sprintf("%T",err)                switch errType {                case "context.deadlineExceededError":                        fmt.Println("deadline")                }                fmt.Printf("error %T: %v\n", err, err)        }$ go build main.go && ./maindeadlineerror context.deadlineExceededError: context deadline exceeded
查看完整描述

1 回答

?
紅顏莎娜

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超13個(gè)贊

您可以使用該context.DeadlineExceeded值(文檔):

  • 無(wú)論是直接err == context.DeadlineExceeded比較,

  • 或使用errors.Is(err, context.DeadlineExceeded).


查看完整回答
反對(duì) 回復(fù) 2022-12-19
  • 1 回答
  • 0 關(guān)注
  • 821 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)