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

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

作為結(jié)構(gòu)成員的函數(shù)

作為結(jié)構(gòu)成員的函數(shù)

Go
楊魅力 2023-06-05 17:06:37
我正在嘗試使功能成為我的結(jié)構(gòu)中的成員type myStruct struct {    myFun func(interface{}) interface{}}func testFunc1(b bool) bool {    //some functionality here    //returns a boolean at the end}func testFunc2(s string) int {    //some functionality like measuring the string length    // returns an integer indicating the length}func main() {    fr := myStruct{testFunc1}    gr := myStruct{testFunc2}}我收到錯(cuò)誤:Cannot use testFunc (type func(b bool) bool) as type func(interface{}) interface{}Inspection info: Reports composite literals with incompatible types and values.我無法弄清楚為什么會(huì)收到此錯(cuò)誤。
查看完整描述

1 回答

?
慕田峪4524236

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

您的代碼的問題是結(jié)構(gòu)中的聲明和testFunc. interface{}接受并返回的函數(shù)與interface{}接受并返回的函數(shù)的類型不同bool,因此初始化失敗。您粘貼的編譯器錯(cuò)誤消息就在此處。


這將起作用:


package main


type myStruct struct {

    myFun func(bool) bool

}


func testFunc(b bool) bool {

    //some functionality here

    //returns a boolean at the end

    return true

}


func main() {

    fr := myStruct{testFunc}

}


查看完整回答
反對(duì) 回復(fù) 2023-06-05
  • 1 回答
  • 0 關(guān)注
  • 126 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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