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

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

如何從任何結(jié)構(gòu)類(lèi)型派生結(jié)構(gòu)列表 - 從 interface{} 獲取可變長(zhǎng)度切片

如何從任何結(jié)構(gòu)類(lèi)型派生結(jié)構(gòu)列表 - 從 interface{} 獲取可變長(zhǎng)度切片

Go
眼眸繁星 2023-08-07 15:24:02
我嘗試實(shí)現(xiàn)一個(gè)采用(任何)結(jié)構(gòu)的函數(shù),返回這些結(jié)構(gòu)的數(shù)組。ReturnArrayOfStory 用固定類(lèi)型結(jié)構(gòu)體類(lèi)型展示了這一思想。嘗試使用函數(shù) ReturnArrayOfX 對(duì)任何類(lèi)型執(zhí)行相同的操作,但反射在編譯時(shí)失敗。package mainimport (    "fmt"    "reflect")type story_t struct {    LANGUAGE string    SPECIES  string}func ReturnArrayOfStory(x story_t) []story_t {    x1 := x    var a1 []story_t    a1 = append(a1, x1)    a1 = append(a1, x1)    a1 = append(a1, x1)    return a1}func ReturnArrayOfX(x interface{}) []interface{} {    x1 := x    v1 := reflect.ValueOf(&x1).Elem()    a1 := []reflect.TypeOf(&x1)    //  var a1 []x    a1 = append(a1, x1)    a1 = append(a1, x1)    a1 = append(a1, x1)    //return a1    return a1}func main() {    var as1 []story_t    s1 := story_t{"EN", "Prince of Persia"}    as1 = ReturnArrayOfStory(s1)    //as1 := ReturnArrayOfX(s1)    for i := 0; i < len(as1); i++ {        fmt.Printf("%02d %+v\n", i, as1[i])    }    as2 := ReturnArrayOfX(s1)    //as1 := ReturnArrayOfX(s1)    for i := 0; i < len(as2); i++ {        fmt.Printf("%02d %+v\n", i, as2[i])    }}a1 := []reflect.TypeOf(&x1)main.go:25:8: reflect.TypeOf is not a type這是一個(gè)簡(jiǎn)化的場(chǎng)景。實(shí)際上,我喜歡從數(shù)據(jù)庫(kù)等外部數(shù)據(jù)源讀取多種結(jié)構(gòu)類(lèi)型。我怎樣才能通過(guò) ReturnArrayOfX 實(shí)現(xiàn)我的目標(biāo)?列表項(xiàng) 這可能嗎?如果沒(méi)有,為什么?
查看完整描述

2 回答

?
隔江千里

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

您的問(wèn)題有兩種解決方案:


第一:如果你想使用反射返回一個(gè)類(lèi)型的切片:


// You cannot return []interface{}, because this function will return [](type of x), and that is not []interface{}

func ReturnArrayOfX(x interface{}) interface{} {

    x1 := x

    a1 := 

// this creates *[](typeOf x)

reflect.New(reflect.SliceOf(reflect.TypeOf(x)))

// Append the first element to *[](typeof x)

// after this, a1 now points to a slice, not to a slice *

    a1 = reflect.Append(a1.Elem(), reflect.ValueOf(x1))

    a1 = reflect.Append(a1, reflect.ValueOf(x1))

    a1 = reflect.Append(a1, reflect.ValueOf(x1))

    //return [](typeof x)

    return a1.Interface()

}

您可以將其用作:


as2 := ReturnArrayOfX(s1)

arr:=as2.([]story_t)

for i := 0; i < len(arr); i++ {

    fmt.Printf("%02d %+v\n", i, arr[i])

}

第二:你可以返回 []interface{} 而不反射:


func ReturnArrayOfX(x interface{}) []interface{} {

   ret:=make([]interface{},0)

   ret=append(ret,x)

   ret=append(ret,x)

   ret=append(ret,x)

}

然后你需要處理數(shù)組的每個(gè)元素:


as2 := ReturnArrayOfX(s1)

for i := 0; i < len(as2); i++ {

    fmt.Printf("%02d %+v\n", i, as2[i])

    data:=as2[i].(story_t)

}


查看完整回答
反對(duì) 回復(fù) 2023-08-07
?
波斯汪

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

這是一個(gè)通用的切片轉(zhuǎn)換函數(shù):


// convertSlice copies the slice in src to the slice pointed to by pdst. 

// The concrete values in src must be assignable to the dst elements.

func convertSlice(pdst interface{}, src interface{}) {

    dstv := reflect.ValueOf(pdst).Elem()

    srcv := reflect.ValueOf(src)

    dstv.Set(reflect.MakeSlice(dstv.Type(), srcv.Len(), srcv.Len()))

    for i := 0; i < srcv.Len(); i++ {

        dstv.Index(i).Set(reflect.ValueOf(srcv.Index(i).Interface()))

    }

}

像這樣使用它:


// Convert []story_t to []interface{}


s0 := []story_t{{"EN", "Prince of Persia"}, {"EN", "Karateka"}}

var s1 []interface{}

convertSlice(&s1, s0)


// Convert []interface{} containing story_t to []story_t


var s2 []story_t

convertSlice(&s2, s1)

在操場(chǎng)上運(yùn)行它。



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