I am importing "encoding/json" package to print output in json.I was able to get the json format but i want to modify the response structure. Here is the sample code
var people []Person
people = append(people, Person{Id: "1", Firstname: "John", Lastname: "Doe", Address: Address{City: "City X", State: "State X"}})
people = append(people, Person{Id: "2", Firstname: "Koko", Lastname: "Doe", Address: Address{City: "City Z", State: "State Y"}})
people = append(people, Person{Id: "3", Firstname: "Francis", Lastname: "Sunday"})
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(people)
which outputs the following response
[
{
"id": "1",
"firstname": "John",
"lastname": "Doe",
"address": {
"city": "City X",
"state": "State X"
}
},
{
"id": "2",
"firstname": "Koko",
"lastname": "Doe",
"address": {
"city": "City Z",
"state": "State Y"
}
},
{
"id": "3",
"firstname": "Francis",
"lastname": "Sunday",
"address": {
"city": "",
"state": ""
}
}
]
But i want to response into the following format
{
"status": "200",
"data": [
{
"id": "1",
"firstname": "John",
"lastname": "Doe",
"address": {
"city": "City X",
"state": "State X"
}
},
{
"id": "2",
"firstname": "Koko",
"lastname": "Doe",
"address": {
"city": "City Z",
"state": "State Y"
}
},
{
"id": "3",
"firstname": "Francis",
"lastname": "Sunday",
"address": {
"city": "",
"state": ""
}
}
]
}
How can I modify the response format ? I am beginner in Go. Thanks for your help.