-1

I'm trying to return a struct as a JSON in my golang app, as seen here

Beego json return example

And this is my code:

type RespJson struct {
   value1 string `json:"value1"`
   value2 string `json:"value2"`
}

func (c *ApiController) Prepare() {
   c.BaseController.Prepare()
}

func (c *ApiController) Post() {
   someData:= c.GetString("someData")
   moreData:= c.GetString("moreData")

   //do something with data

   var responseJSON RespJson
        responseJSON = RespJson{
            value1:    "dataExample",
            value2:    "dataExample",
        }
        c.Data["json"] = &responseJSON
        c.ServeJSON()

}

However, when I test it on postman I always get {}

It's probably a dumb thing because I searched for the error and no one is getting it, so thanks for your time.

cjf93
  • 317
  • 2
  • 11

1 Answers1

0

Answering my own question as I found the problem

Ok, as I said, it was a dumb thing... The fields of my struct were private due to the first lowercase letter, so if I change the struct for

type RespJson struct {
  Value1 string `json:"value1"`
  Value2 string `json:"value2"`
}

and the code for

var responseJSON RespJson
    responseJSON = RespJson{
        Value1:    "dataExample",
        Value2:    "dataExample",
    }
    c.Data["json"] = &responseJSON
    c.ServeJSON()

it works as expected.

cjf93
  • 317
  • 2
  • 11