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

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

如何檢查對象是否具有特定方法?

如何檢查對象是否具有特定方法?

Go
白板的微信 2021-09-27 21:24:43
在 Go 中,如何檢查對象是否響應(yīng)方法?例如,在 Objective-C 中,這可以通過執(zhí)行以下操作來實(shí)現(xiàn):if ([obj respondsToSelector:@selector(methodName:)]) { // if method exists  [obj methodName:42]; // call the method}
查看完整描述

3 回答

?
慕的地6264312

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

如果 obj 是一個(gè),interface{}你可以使用 Go 類型斷言:


if correctobj, ok := obj.(interface{methodName()}); ok { 

  correctobj.methodName() 


查看完整回答
反對 回復(fù) 2021-09-27
?
忽然笑

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

除了接口大括號{write_function_declaration_here}內(nèi)@evanmcdonnal的解決方案之外,您將編寫函數(shù)聲明


if correctobj, ok := obj.(interface{methodName(func_arguments_here)(return_elements_here)}); ok { 

 x,... := correctobj.methodName() 

IE


package main


import "fmt"


type test struct {

    fname string

}


func (t *test) setName(name string) bool {

    t.fname = name

    return true

}


func run(arg interface{}) {

    if obj, ok := arg.(interface{ setName(string) bool });

        ok {

        res := obj.setName("Shikhar")

        fmt.Println(res)

        fmt.Println(obj)

    }

}


func main() {

    x := &test{

        fname: "Sticker",

    }

    fmt.Println(x)

    run(x)


}


查看完整回答
反對 回復(fù) 2021-09-27
  • 3 回答
  • 0 關(guān)注
  • 188 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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