我刚开始结合 Gin 框架学习 GO(lang),我决定编写一些简单的 api 来获取有关酒精饮料的数据。
我目前的问题是 api (get method on http://localhost:8080/alcohol-drinks) 返回空数据对象
我的代码:
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",
},
},
}
问题是,我需要对变量做什么,d以便在浏览器中输出数据?
慕容森
相关分类