我正在写一段 go 通过 http api 或者 F5 的信息,并且可以获取到 json 数据,但我只想要制定的字段的值,当我通过 json.Unmarshal 解码 json 数据时却报错了。
错位信息:
.\license.go:49:6: no new variables on left side of :=
代码内容:
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
)
var (
Username = "admin"
Password = "123456"
)
type license struct {
licenseStartDate string
licenseEndDate int
}
func call(url, method string) error {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{
Transport: tr,
Timeout: time.Second * 10,
}
req, err := http.NewRequest(method, url, nil)
if err != nil {
return fmt.Errorf("Got error %s", err.Error())
}
req.SetBasicAuth(Username, Password)
response, err := client.Do(req)
if err != nil {
return fmt.Errorf("Got error %s", err.Error())
}
defer response.Body.Close()
body, _ := ioutil.ReadAll(response.Body)
res := string(body) // 这个时直接获取到的数据
j := []byte(res)
var l license
err := json.Unmarshal(j, &l) //报错位置
if err != nil {
log.Fatal(err)
}
fmt.Printf("licenseStartDate: %s\nlicenseEndDate: %s", l.licenseStartDate, l.licenseEndDate)
return nil
}
func main() {
call("https://<mg ip>/mgmt/tm/sys/license", "GET")
}
直接获取到的数据内容:
{"kind":"tm:sys:license:licensestats","selfLink":"https://localhost/mgmt/tm/sys/license?ver=16.1.2.1","entries":{"https://localhost/mgmt/tm/sys/license/0":{"nestedStats":{"entries":{"dailyRenewNotifPeriod":{"description":"5"},"licenseEndDate":{"description":"2023/05/12"},"licenseStartDate":{"description":"2023/04/10"},"licensedOnDate":{"description":"2023/04/11"},"licensedVersion":{"description":"16.1.2"},"platformId":{"description":"Z100"},"registrationKey":{"description":"XXXXXXXXXXXXXXXXXXXXXXXXXXX"},"serviceCheckDate":{"description":"2023/04/10"},"https://localhost/mgmt/tm/sys/license/0/active-modules":{"nestedStats":{"entries":{"https://localhost/mgmt/tm/sys/license/0/active-modules/%22BIG-IP,%20VE%20Trial%22":{"nestedStats":{"entries":{"featureModules":{"description":"{ \"Rate Shaping\" \"External Interface and Network HSM, VE\" \"SDN Services, VE\" \"SSL, Forward Proxy, VE\" \"BIG-IP VE, Multicast Routing\" \"APM, Limited\" \"SSL, VE\" \"DNS (1K QPS), VE\" \"Routing Bundle, VE\" \"ASM, VE\" \"Crytpo Offload, VE, Tier 1 (25M - 200M)\" \"Max Compression, VE\" \"AFM, VE\" \"Advanced Web Application Firewall, VE\" \"DNSSEC\" \"Anti-Virus Checks\" \"Base Endpoint Security Checks\" \"Firewall Checks\" \"Network Access\" \"Secure Virtual Keyboard\" \"APM, Web Application\" \"Machine Certificate Checks\" \"Protected Workspace\" \"Remote Desktop\" \"App Tunnel\" \"VE, Carrier Grade NAT (AFM ONLY)\" \"PSM, VE\" }"},"key":{"description":"SDGNLAO-EGDMHQK"}}}}}}}}}}}}
你沒看console跳給你的錯誤訊息
也沒看Funciton註記跟回傳值
也沒看Package使用方法
.\license.go:49:6: no new variables on left side of :=
你前面已經透過req, err := http.NewRequest(method, url, nil)
宣告了err,所以在這段function內要用到err
直接用=再次賦值就好
但是要注意後面再次賦值給err會蓋過前面的value
這段跟你json有問題的部分完全沒關係
最好去用個IDE開發,這個還不用編譯在static check的時候就會報錯了
body, _ := ioutil.ReadAll(response.Body)
這段Package API的回傳值本身就是[]byte,所以下面在string跟byte互轉完全沒必要
去看一下 struct tag語法使用跟json package使用方法,json package在encoding/decoding的時候會有影響
json package在unexport struct field是無法使用的
fmt.Printf("licenseStartDate: %s\nlicenseEndDate: %s", l.licenseStartDate, l.licenseEndDate)
這段也有跳警告出來,自己看一下怎麼修吧
網路範例參考
https://riptutorial.com/go/example/14194/marshaling-structs-with-private-fields