1 回答

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超9個(gè)贊
是的,flush 就可以有簽名;
flush() interface{}, error
你如何實(shí)施?像這樣具有合理方法體的東西應(yīng)該為你做;
type MyDbDriver struct {
//fields
}
func (d *MyDbDriver) query(request string) error {
return nil
}
func (d *MyDbDriver) flush() interface{}, error {
return nil, nil
}
在 Go 中,所有接口實(shí)現(xiàn)都是隱式的,這意味著,如果您的類型具有與接口簽名匹配的方法,那么您已經(jīng)實(shí)現(xiàn)了它。不需要像public class MyType: IMyInterface, IThisIsntCSharp. 請(qǐng)注意,在上面的示例中*MyDbDriver已經(jīng)實(shí)現(xiàn)了您的接口,但MyDbDriver還沒(méi)有。
編輯:下面是一些偽調(diào)用代碼;
e := driver.query("select * from thatTable where ThatProperty = 'ThatValue'")
if e != nil {
return nil, e
}
i, err := driver.flush()
if err != nil {
return nil, err
}
MyConcreteInstance := i.(MyConcreteType)
// note that this will panic if the type of i is not MyConcreteType
// that can be avoided with the familiar object, err calling syntax
MyConcreteIntance, ok := i.(MyConcreteType)
if !ok {
// the type of i was not MyConcreteType :\
}
- 1 回答
- 0 關(guān)注
- 151 瀏覽
添加回答
舉報(bào)