Go中切片内的对象

I am trying to embed multiple objects inside a slice, so I can later export them as JSON. The JSON should look something like this:

[
   {
       name: "Nginx"
       version: "1.9"
   },
   {
       name: ircd-hybrid"
       version: "8.2"
    }
]

So far I have this struct in Go:

type response struct {
    application []struct {
        name        string
        version     string
    }
}

Now (I'm not even sure if the struct is correct), I'm trying to access it this way (again, not sure if this is correct):

   var d response
   d[0].name = "Nginx"
   d[0].version = "1.9"

And so on and so forth. However, it is not working, so I assume I went wrong somewhere. I just don't know where.

The form of your 'model' (the struct in Go in this case) isn't quite right. You really just want this;

type application struct {
        Name        string `json:"name"`   // gotta uppercase these so they're exported 
        Version     string `json:"version"` // otherwise you can't marshal them
    }
apps := []application{
     application{Name:"Windows", Version:"10"}
}
app := application{Name:"Linux", Vesion:"14.2"}
apps = append(apps, app)

The json opens with [ meaning it is just an array, there is no enclosing object. If there were you would want another type with an application array ( []application ) property. To add items to that array you can use append or initialize it with some instance using the 'composite literal' syntax.

EDIT: I added some annotations so the json produced will have lower cased names like in your example. If a property isn't exported in Go other libraries like encoding/json won't be able to use it.