如何从Golang中的json中提取单个值?

my code

func HostStats() (*host.InfoStat, error) {
   infoStat, err := host.Info()
   fmt.Printf("All Host info: ", infoStat)
   return infoStat, err
}

output

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"})

I want to parse and show the below value from above:

  1. hostname
  2. Platform
  3. HostId

I tried and below has the additional code:

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
}

As Adrian mentioned above, you are having problems with your Variables.

In Go you can initialize a variable like you did:

var info string
// And assign a value to it by:
info = "foo"

The Json is unmarshaled into your info variable. The return value of the json.Unmarshal is only an error. So the correct syntax would be:

var info []Information
err := json.Unmarshal(infoStat, &info)

So remember the different ways to initialize vars and assign values to those vars. You can look at GoDocs for Variables for more Info :)