1 回答

TA貢獻(xiàn)2011條經(jīng)驗(yàn) 獲得超2個(gè)贊
這能做到嗎?
在gocbcoreerror.go 中,我們有以下定義:
type memdError struct {
code StatusCode
}
// ...
func (e memdError) KeyNotFound() bool {
return e.code == StatusKeyNotFound
}
func (e memdError) KeyExists() bool {
return e.code == StatusKeyExists
}
func (e memdError) Temporary() bool {
return e.code == StatusOutOfMemory || e.code == StatusTmpFail
}
func (e memdError) AuthError() bool {
return e.code == StatusAuthError
}
func (e memdError) ValueTooBig() bool {
return e.code == StatusTooBig
}
func (e memdError) NotStored() bool {
return e.code == StatusNotStored
}
func (e memdError) BadDelta() bool {
return e.code == StatusBadDelta
}
請(qǐng)注意,這memdError是未導(dǎo)出的,因此您不能在類(lèi)型斷言中使用它。
所以,采取不同的方法:定義你自己的接口:
type MyErrorInterface interface {
KeyNotFound() bool
KeyExists() bool
}
并斷言您從gocb界面返回的任何錯(cuò)誤:
_, err := bucket.Get(key, entity)
if err != nil {
if se, ok = err.(MyErrorInterface); ok {
if se.KeyNotFound() {
// handle KeyNotFound
}
if se.KeyExists() {
// handle KeyExists
}
} else {
// no information about states
}
}
- 1 回答
- 0 關(guān)注
- 177 瀏覽
添加回答
舉報(bào)