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

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

具有可變輸入/輸出類(lèi)型的通用函數(shù)

具有可變輸入/輸出類(lèi)型的通用函數(shù)

Go
江戶(hù)川亂折騰 2023-06-19 14:01:46
只是玩 aws sdk for go。當(dāng)列出不同類(lèi)型的資源時(shí),我傾向于使用很多非常相似的函數(shù),例如下面示例中的兩個(gè)。有沒(méi)有辦法將它們重寫(xiě)為一個(gè)通用函數(shù),該函數(shù)將根據(jù)作為參數(shù)傳遞的內(nèi)容返回特定類(lèi)型?就像是:func generic(session, funcToCall, t, input) (interface{}, error) {}目前我必須這樣做(功能是相同的,只是類(lèi)型發(fā)生了變化):func getVolumes(s *session.Session) ([]*ec2.Volume, error) {    client := ec2.New(s)    t := []*ec2.Volume{}    input := ec2.DescribeVolumesInput{}    for {        result, err := client.DescribeVolumes(&input)        if err != nil {            return nil, err        }        t = append(t, result.Volumes...)        if result.NextToken != nil {            input.NextToken = result.NextToken        } else {            break        }    }    return t, nil}func getVpcs(s *session.Session) ([]*ec2.Vpc, error) {    client := ec2.New(s)    t := []*ec2.Vpc{}    input := ec2.DescribeVpcsInput{}    for {        result, err := client.DescribeVpcs(&input)        if err != nil {            return nil, err        }        t = append(t, result.Vpcs...)        if result.NextToken != nil {            input.NextToken = result.NextToken        } else {            break        }    }    return t, nil} 
查看完整描述

2 回答

?
慕哥9229398

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

因?yàn)槟惶幚砗瘮?shù),所以可以使用 reflect 包在運(yùn)行時(shí)生成函數(shù)。

使用對(duì)象類(lèi)型 (Volume, Vpc) 可以導(dǎo)出所有后續(xù)信息,以提供一個(gè)完全通用的實(shí)現(xiàn),該實(shí)現(xiàn)非常枯燥,甚至更復(fù)雜、更慢。


package main


import (

? ? "errors"

? ? "fmt"

? ? "reflect"

)


func main() {

? ? fmt.Printf("%T\n", getter(Volume{}))

? ? fmt.Printf("%T\n", getter(Vpc{}))

}


type DescribeVolumesInput struct{}

type DescribeVpcs struct{}


type Volume struct{}

type Vpc struct{}


type Session struct{}


type Client struct{}


func New(s *Session) Client { return Client{} }


var typeRegistry = make(map[string]reflect.Type)


func init() {

? ? some := []interface{}{DescribeVolumesInput{}, DescribeVpcs{}}

? ? for _, v := range some {

? ? ? ? typeRegistry[fmt.Sprintf("%T", v)] = reflect.TypeOf(v)

? ? }

}


var errV = errors.New("")

var errType = reflect.ValueOf(&errV).Elem().Type()

var zeroErr = reflect.Zero(reflect.TypeOf((*error)(nil)).Elem())

var nilErr = []reflect.Value{zeroErr}


func getter(of interface{}) interface{} {


? ? outType := reflect.SliceOf(reflect.PtrTo(reflect.TypeOf(of)))

? ? fnType := reflect.FuncOf([]reflect.Type{reflect.TypeOf(new(Session))}, []reflect.Type{outType, errType}, false)

? ? fnBody := func(input []reflect.Value) []reflect.Value {


? ? ? ? client := reflect.ValueOf(New).Call(input)[0]


? ? ? ? t := reflect.MakeSlice(outType, 0, 0)

? ? ? ? name := fmt.Sprintf("Describe%TsInput", of)

? ? ? ? descInput := reflect.New(typeRegistry[name]).Elem()


? ? ? ? mName := fmt.Sprintf("Describe%Ts", of)

? ? ? ? meth := client.MethodByName(mName)

? ? ? ? if !meth.IsValid() {

? ? ? ? ? ? return []reflect.Value{

? ? ? ? ? ? ? ? t,

? ? ? ? ? ? ? ? reflect.ValueOf(fmt.Errorf("no such method %q", mName)),

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? for {

? ? ? ? ? ? out := meth.Call([]reflect.Value{descInput.Addr()})

? ? ? ? ? ? if len(out) > 0 {

? ? ? ? ? ? ? ? errOut := out[len(out)-1]

? ? ? ? ? ? ? ? if errOut.Type().Implements(errType) && errOut.IsNil() == false {

? ? ? ? ? ? ? ? ? ? return []reflect.Value{t, errOut}

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? result := out[1]

? ? ? ? ? ? fName := fmt.Sprintf("%Ts", of)

? ? ? ? ? ? if x := result.FieldByName(fName); x.IsValid() {

? ? ? ? ? ? ? ? t = reflect.AppendSlice(t, x)

? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? return []reflect.Value{

? ? ? ? ? ? ? ? ? ? t,

? ? ? ? ? ? ? ? ? ? reflect.ValueOf(fmt.Errorf("field not found %q", fName)),

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }


? ? ? ? ? ? if x := result.FieldByName("NextToken"); x.IsValid() {

? ? ? ? ? ? ? ? descInput.FieldByName("NextToken").Set(x)

? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? break

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return []reflect.Value{t, zeroErr}

? ? }

? ? fn := reflect.MakeFunc(fnType, fnBody)

? ? return fn.Interface()

}


查看完整回答
反對(duì) 回復(fù) 2023-06-19
?
月關(guān)寶盒

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

代理第 3 方 API,用 go 實(shí)現(xiàn)非常簡(jiǎn)單,下面是它是如何用 endly e2e test runner?AWS 代理實(shí)現(xiàn)的

我會(huì)說(shuō) AWS API 是代理的完美候選者,只要反射性能價(jià)格不是問(wèn)題。

像kubernetes這樣的其他一些第三方 API?更具挑戰(zhàn)性,但仍然很容易用 go 代理,這是反射和代碼生成的結(jié)合。


查看完整回答
反對(duì) 回復(fù) 2023-06-19
  • 2 回答
  • 0 關(guān)注
  • 155 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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