我剛開始結(jié)合 Gin 框架學(xué)習(xí) GO(lang),我決定編寫一些簡單的 api 來獲取有關(guān)酒精飲料的數(shù)據(jù)。我目前的問題是 api (get method on http://localhost:8080/alcohol-drinks) 返回空數(shù)據(jù)對象我的代碼:package mainimport ( "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以便在瀏覽器中輸出數(shù)據(jù)?
1 回答

慕容森
TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超18個(gè)贊
小寫字段被認(rèn)為是私有的,不會(huì)被標(biāo)準(zhǔn)的 json 序列化器序列化。
更改您的alcoholDrink類型的字段,使它們以大寫字母開頭:
type alcoholDrink struct {
Name string
Description string
NutritionsAmount string
Nutritions map[string]string
}
如果您想在生成的 json 中查看小寫名稱,可以為每個(gè)字段添加注釋:
type alcoholDrink struct {
Name string `json:"name"`
Description string `json:"description"`
NutritionsAmount string `json:"nutritionsAmount"`
Nutritions map[string]string `json:"nutritions"`
}
- 1 回答
- 0 關(guān)注
- 283 瀏覽
添加回答
舉報(bào)
0/150
提交
取消