在下面的代碼中,我嘗試將變量“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()
。
- 1 回答
- 0 關注
- 178 瀏覽
添加回答
舉報
0/150
提交
取消