如何访问http客户端响应正文全局[关闭]

In the code below, am trying to access the variable "Regprofile" as global variable but and getting empty output. Any help?

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 database
func 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},
    }

    //============================================================
    // Register GMLC Instance to NRF, PUT

    var gmlc = []byte(`{
                "nfInstanceID": "f81d4fae-7dec-11d0-a765-00a0c91egmlc",
                "heartBeatTimer": 0,
                "nfType": ["GMLC"],
                "nfStatus": ["string"],
                "ipv4Addresses": ["172.16.0.X:5000"]
               }`)

    //================= Registser profile to NRF ========================//
    postnrf := "https://127.0.0.1:9090/nnrf-nfm/v1/nf-instances/f81d4fae-7dec-11d0-a765-00a0c91egmlc?"
    rq, err := http.NewRequest("PUT", postnrf+para.Encode(), bytes.NewBuffer(gmlc))
    rq.Header.Set("Content-Type", "application/json")
    rq.Header.Set("Accept", "application/json")

    response, err := netClient.Do(rq)
    if err != nil {
        panic(err)
    }
    defer res.Body.Close()

    decoder := json.NewDecoder(response.Body)

    var Regprofile GMLCInstance

    err = decoder.Decode(&Regprofile)
    if err != nil {
        fmt.Println("Response body not well decoded")
    }

    fmt.Println(Regprofile)

}

The output of fmt.Println(Regprofile) gives {f81d4fae-7dec-11d0-a765-00a0c91egmlc 10 [GMLC] [REGISTERED] [172.16.0.X:5000]}

However, when i print the variable in for example main as

func main(){

fmt.Println(Regprofile)

}

I get empty data as { 0 [] [] []}

1个回答



func init()</ code>中,您在本地重新声明变量 var Regprofile GMLCInstance </ code>以起作用 范围。 该声明用局部变量遮盖了全局变量。 只需在 init()</ code>中删除此本地声明即可。</ p>
</ div>

展开原文

原文

In func init() you redeclare variables var Regprofile GMLCInstance locally to function scope. This declaration shadows global variable with local one. Just delete this local declaration inside init().

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问