2

I have an array of stucts stored a variable my array.

Struct is

  type myStruct struct {
    id          int64   `db:"id" json:"id"`
    Name        string  `form:"name" db:"name" json:"name" binding:"required"`
    Status     string     `form:"status"  db:"status" json:"status" binding:"required"`

My array looks like this and is stored in a variable 'myArray'. This array is formed by iterating over a set of rows coming from database.

[{1 abc default} {2 xyz default}]

I am using gin as http server. How do I set this array into JSON reponse using c.JSON. Something like

[
   {
      id: 1,
      name : 'abc' 
      status: 'default'
   },
   {
      id: 2,
      name : 'xyz' 
      status: 'default'
   }
]

1 Answer 1

4

ok c.JSON(http.StatusOK, myArray) worked. But I cannot see the Id field in the response. Any reason why? Is it because of 'int64' dataType?

Sign up to request clarification or add additional context in comments.

3 Comments

Because it is an unexported field (field starting with a lower case) and thus it can be marshalled.
yes just figured this out.Changing id to Id worked. Thanks!
how to set Id to id in the resulting json?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.