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

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

嘗試使用空接口參數(shù)返回?cái)?shù)據(jù)時(shí)出錯(cuò)

嘗試使用空接口參數(shù)返回?cái)?shù)據(jù)時(shí)出錯(cuò)

Go
吃雞游戲 2022-07-25 11:13:01
我正在嘗試interface{}通過(guò)添加緩存來(lái)編寫一個(gè)使用參數(shù)返回?cái)?shù)據(jù)的函數(shù)的包裝。我的問(wèn)題是,一旦我有一個(gè)有效的interface{}我不知道如何分配它以在參數(shù)中返回。包裝的調(diào)用(github.Client) .Do在github API 客戶端中,當(dāng)我嘗試使用go-cache添加緩存時(shí),問(wèn)題就來(lái)了這有點(diǎn)我的功能func (c *cachedClient) requestAPI(url string, v interface{}) error {    x, found := c.cache.Get(url)    if found {                        // Found in cache code        log.Printf("cached: %v", x)        v = x // HERE: this do not work. x contains a valid copy of what I want but how do I store it in v?        return nil    }    req, _ := c.githubClient.NewRequest("GET", url, nil)    // not found I cache, request it    res, err := c.githubClient.Do(*c.context, req, v)    if err != nil {        return err    }    if res.StatusCode != 200 {        return fmt.Errorf("Error Getting %v: %v", url, res.Status)    }    c.cache.Add(url, v, cache.DefaultExpiration)   // store in cache    return nil    // once here v works as expected and contain a valid item}當(dāng)我嘗試像這樣使用它時(shí)必須返回緩存值時(shí)它會(huì)失敗:// Some init code c is a cachedClient i := new(github.Issue)c.requestAPI(anAPIValidURLforAnIssue, i)log.Printf("%+v", i)    // var i correctly contains an issue from the github apio := new(github.Issue)c.requestAPI(anAPIValidURLforAnIssue, o)log.Printf("%+v", o)  // var o should have been get from the cache but here is empty所以基本上我的問(wèn)題是,當(dāng)我正確恢復(fù)緩存項(xiàng)目時(shí)它很好,但我不能將它存儲(chǔ)在用于存儲(chǔ)它的參數(shù)中。我不能使用子類,因?yàn)槲艺诎b的調(diào)用interface{}已經(jīng)使用了。而且我不能移動(dòng)它來(lái)返回值,因?yàn)槟悴荒芊祷匾粋€(gè)通用接口。如何使interface{} x存儲(chǔ)在 v 中以使其在外部可用?
查看完整描述

2 回答

?
臨摹微笑

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

要?dú)w檔您想要的內(nèi)容,您需要使用一些反射魔法。請(qǐng)嘗試v = x用下一個(gè)代碼段替換:


reflect.ValueOf(v).Elem().Set(reflect.ValueOf(x).Elem())

來(lái)自 OP 的注釋:我必須添加最后一個(gè).Elem()才能完成這項(xiàng)工作。


注意:在調(diào)用該requestAPI方法時(shí),您應(yīng)該使用指向該值的指針:假設(shè)緩存的值是 type int。然后你應(yīng)該requestAPI像這樣打電話:


var dst int // destination of the cached value or a newly retrieved result

cc.requestAPI(url, &dst)


查看完整回答
反對(duì) 回復(fù) 2022-07-25
?
蝴蝶刀刀

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

在某些假設(shè)下,例如您將 json 數(shù)據(jù)存儲(chǔ)在下面的緩存中,我將嘗試這樣做。錯(cuò)誤未處理。


package main


import (

    "encoding/json"

    "fmt"

)


type Data struct {

    Name string

}


func main() {

    var d Data

    requestAPI(&d)

    fmt.Println(d)

}


func requestAPI(v interface{}) {

    var cache_res interface{} = []byte("{\"Name\":\"CCC\"}")

    //assume you got cache_res from cache

    x, _ := cache_res.([]byte)

    _ = json.Unmarshal(x, &v)

}

其實(shí)上面githubClient.Do也是在做的。它檢查 v 是否滿足io.Writer接口,如果是,則將數(shù)據(jù)寫入 v。如果不是,則將 json 解組到 v 中,如上所示。所以同樣可以從緩存中完成。


在這里查看: https ://github.com/google/go-github/blob/v32.1.0/github/github.go#L586


如果緩存對(duì)象是特定的,則可以使用以下內(nèi)容。您不處理空接口{},因?yàn)槟鷳?yīng)該能夠?qū)⑻囟愋蛡鬟f給c.githubClient.Doas v。由于它使用 json 包,它將檢測(cè)類型信息并相應(yīng)地將值填充到其中。假設(shè)您存儲(chǔ)type Data struct


在下面的代碼中,消除了其他細(xì)節(jié),例如條件檢查是否緩存和錯(cuò)誤處理


package main


import (

    "fmt"

)


type Data struct {

    Name string

}


func main() {

    var d Data

    requestAPI(&d)

    fmt.Println(d)

}


func requestAPI(v *Data) {

    var cache_res interface{} = Data{"CCC"}

    //assume you got cache_res from cache

    x, _ := cache_res.(Data)

    *v = x


    //in case you did not find it in cache then githubClient.Do should unmarshal

    //contents of response body into v *Data if Data fields match that of json

    //res, err := c.githubClient.Do(*c.context, req, v)

}


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

添加回答

舉報(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)