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

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

golang中參數(shù)中的動(dòng)態(tài)結(jié)構(gòu)

golang中參數(shù)中的動(dòng)態(tài)結(jié)構(gòu)

Go
蕭十郎 2022-05-10 14:06:48
我必須處理許多不同域的文件,但它們都有行固定位置我為具有起始位置和結(jié)束位置標(biāo)簽的域創(chuàng)建結(jié)構(gòu)type IP0059T1 struct {    TableID                            string `startpos:"1" endpos:"8"`    EffectiveDateTime                  string `startpos:"11" endpos:"11"`}type IP0059T2 struct {    TableID                            string `startpos:"1" endpos:"8"`    SequenceNumber                  string `startpos:"11" endpos:"14"`}我創(chuàng)建了一個(gè)方法,它適用于一個(gè)表func (s *Service) GetIP0059T01(bundleURI string) ([]IP0059T1, error) {    reader := getReader(bundleURI)    var items []IP0059T1    err = patterns.Each(reader, func(e *contract.Entry) error {        line := string(e.Data)        var item = new(IP0059T1)        structName := reflect.TypeOf(item).Name()        structValues := reflect.ValueOf(item).Elem()        for i := 0; i < structValues.NumField(); i++ { // iterates through every struct type field            field := structValues.Field(i) // returns the content of the struct type field            value, _ := getValue(line, structValues.Type().Field(i), structName)            _ = s.SetValue(field, structValues, i, structName, value)        }        items = append(items, *item)        return nil    })    if err != nil {        return nil, err    }    return items, nil}setValue 使用反射func setValue(field reflect.Value, structValues reflect.Value, i int, structName string, value string) error {    if field.Kind() != reflect.String {        return &FieldNotStringError{Field: structValues.Type().Field(i).Name, Struct: structName}    }    field.SetString(value)    return nil}還 getValue 使用反射func getValue(line string, field reflect.StructField, structName string) (string, error) {    startPosition, _ := strconv.Atoi(field.Tag.Get("startpos"))    endPosition, _ := strconv.Atoi(field.Tag.Get("endpos"))    return line[(startPosition - 1):(endPosition)], nil}那么,將方法 GetIP0059T01 轉(zhuǎn)換為具有獲取 uri 和類(lèi)型的泛型方法并返回一個(gè)可以轉(zhuǎn)換為我傳遞的類(lèi)型的數(shù)組的接口數(shù)組有什么解決方法嗎?基本上,我想要一個(gè)通用的東西
查看完整描述

2 回答

?
慕容3067478

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

看起來(lái)您真正需要該類(lèi)型的唯一兩個(gè)地方是分配它時(shí)以及將其附加到數(shù)組時(shí)。因此,您可以更改函數(shù)以獲取兩個(gè)回調(diào):


func GetStuff(bundleURI string, newItem func() interface{},collect func(interface{})) error {


   // instead of var item = new(IP0059T1)

   var item = newItem()


   ...


   // instead of items = append(items, *item)

   collect(item)



}

并使用以下方法調(diào)用該函數(shù):


items:=make([]Item,0)

GetStuff(uri, func() interface{} {return new(Item)}, 

func(item interface{}) {items=append(items,*item.(*Item))})


查看完整回答
反對(duì) 回復(fù) 2022-05-10
?
catspeake

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

你可以像 Go 標(biāo)準(zhǔn)庫(kù)那樣做,例如 in encoding/json,就是把要填寫(xiě)的值作為參數(shù)。您的“通用”將類(lèi)似于:


GetValues(bundleURI string, dest interface{}) (error)

wheredest應(yīng)該是指向應(yīng)該反序列化為任何類(lèi)型的切片的指針,例如:


var v []IP0059T1

err = GetValues(myURI, &v)

然后,GetValues您可以使用reflect.Type.Elem()獲取切片元素的類(lèi)型,reflect.New()創(chuàng)建該類(lèi)型的新實(shí)例,reflect.Append()并將它們直接附加到dest. 這保留了類(lèi)型安全,同時(shí)允許某種程度的泛型編程。


查看完整回答
反對(duì) 回復(fù) 2022-05-10
  • 2 回答
  • 0 關(guān)注
  • 166 瀏覽
慕課專(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)