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

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

Go 接口在方法之間強制執(zhí)行相同的參數(shù)類型(但可以是任何類型)

Go 接口在方法之間強制執(zhí)行相同的參數(shù)類型(但可以是任何類型)

Go
紅糖糍粑 2022-05-05 17:40:44
我敢肯定,標(biāo)題令人困惑,但很難描述我的意思。我想創(chuàng)建一個有兩種方法的 Go 接口;第一個返回一個值,第二個接受一個值。我想確保方法 1 返回與方法 2 接受的類型相同的類型,而不指定類型是什么(除了它是一個結(jié)構(gòu))。例如:type MyInterface interface {    Method1() MyType    Method2(MyType) error}whereMyType可以是任何類型(結(jié)構(gòu)),只要它在方法 1 和方法 2 中都相同。有沒有辦法在 Go 中做到這一點?編輯:根據(jù)@iLoveReflection 的回答,我嘗試了以下方法:package maintype MyInterface interface {    GetType() interface{}    UseType(input interface{})}type MyImplementation struct{}type MyType struct {}func (i MyImplementation) GetType() MyType {    return MyType{}}func (i MyImplementation) UseType(input MyType) {    return}func test(input MyInterface) {    return}func assertArgAndResult() {    var v MyImplementation    v.UseType(v.GetType())}func main() {    test(MyImplementation{})}所以基本上,我指定了一個接口MyInterface(MyImplementationassertArgAndResult()正在按預(yù)期工作,并確保MyImplementation滿足要求。但是,我在函數(shù)中得到一個編譯錯誤main():cannot use MyImplementation literal (type MyImplementation) as type MyInterface in argument to test:    MyImplementation does not implement MyInterface (wrong type for GetType method)        have GetType() MyType        want GetType() interface {}
查看完整描述

1 回答

?
犯罪嫌疑人X

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

將以下函數(shù)添加到包中,以確保在編譯時輸入和輸出類型匹配:


func assertArgAndResult() {

    var v MyInterface

    v.Method2(v.Method1())

}

只要不調(diào)用該函數(shù),該函數(shù)就不會包含在可執(zhí)行文件中。


沒有編譯時檢查可以確保它MyType是問題中指定的結(jié)構(gòu)類型。


reflect 包可用于完全檢查類型類型。


// checkItf returns true of the interface value pointed to by

// pi has Method1 with some return type T and Method2 with

// argument type T.

func checkItf(pi interface{}) bool {

    t := reflect.TypeOf(pi)

    if t.Kind() != reflect.Ptr {

        return false // or handle as error

    }

    t = t.Elem()

    if t.Kind() != reflect.Interface {

        return false // or handle as error

    }

    m1, ok := t.MethodByName("Method1")


    // Method1 should have no outputs and one input.

    if !ok || m1.Type.NumIn() != 0 || m1.Type.NumOut() != 1 {

        return false

    }


    // Method2 should have one input and one output.

    m2, ok := t.MethodByName("Method2")

    if !ok || m2.Type.NumIn() != 1 || m2.Type.NumOut() != 1 {

        return false

    }


    e := reflect.TypeOf((*error)(nil)).Elem()

    s := m1.Type.Out(0)


    // The type must be a struct and

    // the input type of Method2 must be the same as the output of Method1 and

    // Method2 must return error.

    return s.Kind() == reflect.Struct &&

        m2.Type.In(0) == s &&

        m2.Type.Out(0) == e

}

像這樣稱呼它:


func init() {

   if !checkItf((*MyInterface)(nil)) {

      panic("mismatched argument and return time son MyInterface")

   }

}


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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