亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何訪問http客戶端響應正文全局

如何訪問http客戶端響應正文全局

Go
精慕HU 2023-07-10 17:25:17
在下面的代碼中,我嘗試將變量“Regprofile”作為全局變量訪問,但得到空輸出。有什么幫助嗎?type GMLCInstance struct {    NfInstanceID   string   `json:"nfInstanceID"`    HeartBeatTimer int      `json:"heartBeatTimer"`    NfType         []string `json:"nfType"`    NfStatus       []string `json:"nfStatus"`    Ipv4Addresses  []string `json:"ipv4Addresses"`}var Regprofile GMLCInstance// Request credentials and token from NRF and register profile to NFR databasefunc init() {    urlcred := "https://127.0.0.1:9090/credentials"    // Perform the request    resp, err := netClient.Get(urlcred)    if err != nil {        log.Fatalf("Failed get: %s", err)    }    defer resp.Body.Close()    // Fill the record with the data from the JSON    var cr Credential    // Use json.Decode for reading streams of JSON data    if err := json.NewDecoder(resp.Body).Decode(&cr); err != nil {        log.Println(err)    }    //fmt.Println(cr)    clientId := cr.CLIENTID    clientsec := cr.CLIENTSECRET    // Get token    reqtoken := url.Values{        "grant_type":    []string{"client_credentials"},        "client_id":     []string{clientId},        "client_secret": []string{clientsec},        "scope":         []string{"GMLC"},    }    urlq := "https://127.0.0.1:9090/oauth2/token?"    res, err := netClient.PostForm(urlq+reqtoken.Encode(), nil)    if err != nil {        log.Fatalf("Failed get: %s", err)    }    var auth AccessToken    // Use json.Decode for reading streams of JSON data    if err := json.NewDecoder(res.Body).Decode(&auth); err != nil {        log.Println(err)    }    //fmt.Println(auth.AccessToken)    token := auth.AccessToken    para := url.Values{        "access_token": []string{token},    }我得到的空數據為 { 0 [] [] []}
查看完整描述

1 回答

?
溫溫醬

TA貢獻1752條經驗 獲得超4個贊

您可以在函數范圍內本地func init()重新聲明變量。var Regprofile GMLCInstance該聲明將全局變量與局部變量隱藏起來。只需刪除里面的這個本地聲明即可init()。



查看完整回答
反對 回復 2023-07-10
  • 1 回答
  • 0 關注
  • 178 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號