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

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

如何將數(shù)組的泛型類型作為 Golang 中函數(shù)的參數(shù)

如何將數(shù)組的泛型類型作為 Golang 中函數(shù)的參數(shù)

Go
慕桂英546537 2022-09-19 17:27:04
我是Golang的新手,我的問題是如何使函數(shù)接受泛型類型的參數(shù)。以下是我最近遇到的具體問題:我定義了一個(gè)包含函數(shù)的結(jié)構(gòu)type TestArgs struct {    name string    ...    customAssertForTypeA func(array []*pb.TypeA)    customAssertForTypeB func(array []*pb.TypeB)}// Define the test case with TestArgs.tests := []TestArgs{        {            name: "Test A",            customAssertForTypeA: func(array []*pb.TypeA) {                // Some code            }        },        {            name: "Test B",            customAssertForTypeB: func(array []*pb.TypeB) {                // Some code            }        },}我的問題是,如何使客戶Assert函數(shù)接受泛型類型的參數(shù)?我看到一些類似的問題與解決方案,所以我嘗試interface{}customAssert func(array interface{})它不起作用。
查看完整描述

1 回答

?
呼啦一陣風(fēng)

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

類型斷言https://tour.golang.org/methods/15)是當(dāng)你有一個(gè)并想要訪問具體值時(shí)所需要的:interface{}


type TestArgs struct {

    name         string

    customAssert func(interface{})

}


// Define the test case with TestArgs.

tests := []TestArgs{

    {

        name: "Test A",

        customAssert: func(param interface{}) {

            // Some code

            array, ok := param.([]*pb.TypeA)

            if !ok {

                t.Fatal("assertion error TypeA")

            }

            // use array

        },

    },

    {

        name: "Test B",

        customAssert: func(param interface{}) {

            // Some code

            array, ok := param.([]*pb.TypeB)

            if !ok {

                t.Fatal("assertion error TypeB")

            }

            // use array

        },

    },

}


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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