func HostStats() (*host.InfoStat, error) {
infoStat, err := host.Info()
fmt.Printf("All Host info: ", infoStat)
return infoStat, err
}
All Host info: %!(EXTRA string= {"hostname":"UDAY-PC","uptime":536323,"bootTime":1559911444,"procs":248,"os":"windows","platform":"Microsoft Windows 10 Pro","platformFamily":"Standalone Workstation","platformVersion":"10.0.17134 Build 17134","kernelVersion":"","virtualizationSystem":"","virtualizationRole":"","hostid":"0b324295-3631-47db-b6e8-83cdba2a1af9"})
我想从上方解析并显示以下值:
func HostStats() (*host.InfoStat, error) {
infoStat, err := host.Info()
type Information struct {
Name string
Platform string
HostId string
}
var info []Information
info, err := json.Unmarshal(infoStat, &info)
if err != nil {
fmt.Println("error:", err)
}
fmt.Printf("ok: %+v", info)
//almost every return value is a struct
fmt.Printf("All Host info: ", infoStat)
return infoStat, err
}
答案 0 :(得分:0)
如上所述,Adrian表示您的变量有问题。
在Go中,您可以像以前一样初始化变量:
var info string
// And assign a value to it by:
info = "foo"
Json已编组到您的info变量中。 json.Unmarshal的返回值只是一个错误。因此正确的语法为:
var info []Information
err := json.Unmarshal(infoStat, &info)
因此,请记住初始化var和为这些var赋值的不同方法。 您可以查看GoDocs for Variables了解更多信息:)