该函数返回 nil。

huangapple go评论116阅读模式
英文:

The function returns nil

问题

我是一个初学者,我编写了一个函数,用于检查请求中的pagination.Main == true,如果是,则函数返回prod,否则返回prod_2。默认参数是main == true,但如果我将其更改为false,我会得到"data": null

我的函数:

  1. func GetAllIproducts(q *models.Products, g *models.Products_2, pagination *models.Params) (*[]models.Products, *[]models.Products_2) {
  2. var prod []models.Products
  3. var prod_2 []models.Products_2
  4. offset := (pagination.Page - 1) * pagination.Limit
  5. if pagination.Main {
  6. if pagination.Cat_id == nil {
  7. config.DB.Model(&models.Products{}).Where(q).Limit(pagination.Limit).Offset(offset).Find(&prod)
  8. } else {
  9. config.DB.Model(&models.Products{}).Where(q).Where("cat_id IN (?)", pagination.Cat_id).Limit(pagination.Limit).Offset(offset).Find(&prod)
  10. }
  11. return &prod, nil
  12. } else {
  13. if pagination.Cat_id == nil {
  14. config.DB.Model(&models.Products_2{}).Where(g).Limit(pagination.Limit).Offset(offset).Find(&prod_2)
  15. } else {
  16. config.DB.Model(&models.Products_2{}).Where(g).Where("cat_id IN (?)", pagination.Cat_id).Limit(pagination.Limit).Offset(offset).Find(&prod_2)
  17. }
  18. return nil, &prod_2
  19. }
  20. }
  1. func GetAllProducts_by_multi_params(c *gin.Context) {
  2. pagination := GenerateMultiParams(c)
  3. var prod models.Products
  4. var prod_2 models.Products_2
  5. ProdLists, _ := GetAllIproducts(&prod, &prod_2, &pagination)
  6. c.JSON(http.StatusOK, gin.H{
  7. "data": ProdLists,
  8. })
  9. }
英文:

I'm a beginner, I wrote a function that checks if in the request pagination.Main == true , then the function returns prod, otherwise prod_2. The default parameter is main == true , but if I change it to false I get "data": null

My funcs:

  1. func GetAllIproducts(q *models.Products, g *models.Products_2, pagination *models.Params) (*[]models.Products, *[]models.Products_2) {
  2. var prod []models.Products
  3. var prod_2 []models.Products_2
  4. offset := (pagination.Page - 1) * pagination.Limit
  5. if pagination.Main {
  6. if pagination.Cat_id == nil {
  7. config.DB.Model(&models.Products{}).Where(q).Limit(pagination.Limit).Offset(offset).Find(&prod)
  8. } else {
  9. config.DB.Model(&models.Products{}).Where(q).Where("cat_id IN (?)", pagination.Cat_id).Limit(pagination.Limit).Offset(offset).Find(&prod)
  10. }
  11. return &prod, nil
  12. } else {
  13. if pagination.Cat_id == nil {
  14. config.DB.Model(&models.Products_2{}).Where(g).Limit(pagination.Limit).Offset(offset).Find(&prod_2)
  15. } else {
  16. config.DB.Model(&models.Products_2{}).Where(g).Where("cat_id IN (?)", pagination.Cat_id).Limit(pagination.Limit).Offset(offset).Find(&prod_2)
  17. }
  18. return nil, &prod_2
  19. }
  20. }
  1. func GetAllProducts_by_multi_params(c *gin.Context) {
  2. pagination := GenerateMultiParams(c)
  3. var prod models.Products
  4. var prod_2 models.Products_2
  5. ProdLists, _ := GetAllIproducts(&prod, &prod_2, &pagination)
  6. c.JSON(http.StatusOK, gin.H{
  7. "data": ProdLists,
  8. })
  9. }

答案1

得分: 0

第二个返回值由下划线 _ 保存在 ProdLists 中

  1. ProdLists, _ := GetAllIproducts(&prod, &prod_2, &pagination)

你可以将其更改为 Prod2Lists 并检查是否为 nil,例如:

  1. ProdLists, Prod2Lists := GetAllIproducts(&prod, &prod_2, &pagination)
  2. 如果 ProdLists 不为 nil {
  3. c.JSON(http.StatusOK, gin.H{
  4. "data": ProdLists,
  5. })
  6. } else {
  7. c.JSON(http.StatusOK, gin.H{
  8. "data": Prod2Lists,
  9. })}
英文:

The second return value is hold by the underscore _ in

  1. ProdLists, _ := GetAllIproducts(&prod, &prod_2, &pagination)

You can change it to Prod2Lists and check for nil, like:

  1. ProdLists, Prod2Lists := GetAllIproducts(&prod, &prod_2, &pagination)
  2. If ProdLists != nil {
  3. c.JSON(http.StatusOK, gin.H{
  4. "data": ProdLists,
  5. })
  6. } else {
  7. c.JSON(http.StatusOK, gin.H{
  8. "data": Prod2Lists,
  9. })}

huangapple
  • 本文由 发表于 2022年7月28日 20:12:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/73152695.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定