Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c.JSON gin.H{()} outputs empty objects

Tags:

go

go-gin

I just started to learn GO(lang) in combination with Gin framework and I've decided that I will write some simple api for fetching data about alcohol drinks.

My current problem is that api (get method on http://localhost:8080/alcohol-drinks) returns empty data object

My code:

package main

import (
    "github.com/gin-gonic/gin"
)

type alcoholDrink struct {
    name             string
    description      string
    nutritionsAmount string
    nutritions       map[string]string
}

func main() {
    r := gin.Default()
    r.GET("/alcohol-drinks", func(c *gin.Context) {

        d := []alcoholDrink{
            {
                name:             "Gin",
                description:      "DescriptionGin is a distilled alcoholic drink that derives its predominant flavour from juniper berries. Gin is one of the broadest categories of spirits, all of various origins, styles, and flavour profiles, that revolve around juniper as a common ingredient",
                nutritionsAmount: "per 100 grams",
                nutritions: map[string]string{
                    "Calories":     "263",
                    "TotalFat":     "0 g",
                    "Cholesterol":  "0 mg",
                    "Sodium":       "2 mg",
                    "Carbohydrate": "0 g",
                    "Protein":      "0 g",
                },
            },
            {
                name:             "Vodka",
                description:      "odka is a clear distilled alcoholic beverage with different varieties originating in Poland and Russia. It is composed primarily of water and ethanol, but sometimes with traces of impurities and flavorings.",
                nutritionsAmount: "per 100 grams",
                nutritions: map[string]string{
                    "Calories":     "231",
                    "TotalFat":     "0 g",
                    "Cholesterol":  "0 mg",
                    "Sodium":       "1 mg",
                    "Carbohydrate": "0 g",
                    "Protein":      "0 g",
                },
            },
        }

        c.JSON(200, gin.H{
            "status": "OK",
            "code":   200,
            "data":   d,
        })

    })
    r.Run()
}

enter image description here

The question is, what do I need to do with variable d so data will be outputted in browser?

like image 202
Valor_ Avatar asked Dec 22 '22 19:12

Valor_


1 Answers

Lowercase fields are considered private, and will not be serialized by the standard json serializer.

Change the fields of your alcoholDrink type, so that they start with an uppercase letter :

type alcoholDrink struct {
    Name             string
    Description      string
    NutritionsAmount string
    Nutritions       map[string]string
}

If you want to see lowercased names in the resulting json, you can add an annotation to each field :

type alcoholDrink struct {
    Name             string    `json:"name"`
    Description      string    `json:"description"`
    NutritionsAmount string    `json:"nutritionsAmount"`
    Nutritions       map[string]string   `json:"nutritions"`
}
like image 184
LeGEC Avatar answered Dec 29 '22 23:12

LeGEC