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()
}
The question is, what do I need to do with variable d
so data will be outputted in browser?