1
rrfeng 20 天前 via Android 2
拿 json tag 硬凑 gorm 啊…
gorm 有自己的 tag |
3
saleacy OP @rrfeng 很强!
r.GET("/fp", utils.AuthMiddleware, func(c *gin.Context) { username := c.MustGet("username").(string) var products []struct { Age string `gorm:"column:a"` TaobaoID string `gorm:"column:b"` MinPrice float64 `gorm:"column:c"` TPrice int `gorm:"column:d"` } if err := db.Raw(`select products.age as a,products.min_price as b,products.taobao_id as c from products join users where users.username = ?`, username).Scan(&products).Error; err != nil { c.JSON( http.StatusInternalServerError, gin.H{"error": "Could not retrieve user products"}) return } c.JSON( http.StatusOK, products) }) [root@ctos0 routes]# curl -X GET http://localhost:8080/fp -H "Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MzI2ODg1OTQsImlzcyI6ImFhYSJ9.CRiAl3SG509O2XkB-a_aXtnNcgFORZsbP_7M-580HBg" [GIN] 2024/11/26 - 11:26:53 | 200 | 821.97µs | ::1 | GET "/fp" [{"p.age":"333","p.taobao_id":"033","p.min_price":129,"pr.t_price":79},{"p.age":"222","p.taobao_id":"022","p.min_price":129,"pr.t_price":79}] |