该函数返回 nil。

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

The function returns nil

问题

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

我的函数:

func GetAllIproducts(q *models.Products, g *models.Products_2, pagination *models.Params) (*[]models.Products, *[]models.Products_2) {
	var prod []models.Products
	var prod_2 []models.Products_2
	offset := (pagination.Page - 1) * pagination.Limit
	if pagination.Main {
		if pagination.Cat_id == nil {
			config.DB.Model(&models.Products{}).Where(q).Limit(pagination.Limit).Offset(offset).Find(&prod)
		} else {
			config.DB.Model(&models.Products{}).Where(q).Where("cat_id IN (?)", pagination.Cat_id).Limit(pagination.Limit).Offset(offset).Find(&prod)
		}
		return &prod, nil
	} else {
		if pagination.Cat_id == nil {
			config.DB.Model(&models.Products_2{}).Where(g).Limit(pagination.Limit).Offset(offset).Find(&prod_2)
		} else {
			config.DB.Model(&models.Products_2{}).Where(g).Where("cat_id IN (?)", pagination.Cat_id).Limit(pagination.Limit).Offset(offset).Find(&prod_2)
		}
		return nil, &prod_2
	}
}
func GetAllProducts_by_multi_params(c *gin.Context) {
	pagination := GenerateMultiParams(c)
	var prod models.Products
	var prod_2 models.Products_2
	ProdLists, _ := GetAllIproducts(&prod, &prod_2, &pagination)
	c.JSON(http.StatusOK, gin.H{
		"data": ProdLists,
	})
}
英文:

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:

func GetAllIproducts(q *models.Products, g *models.Products_2, pagination *models.Params) (*[]models.Products, *[]models.Products_2) {
	var prod []models.Products
	var prod_2 []models.Products_2
	offset := (pagination.Page - 1) * pagination.Limit
	if pagination.Main {
		if pagination.Cat_id == nil {
			config.DB.Model(&models.Products{}).Where(q).Limit(pagination.Limit).Offset(offset).Find(&prod)
		} else {
			config.DB.Model(&models.Products{}).Where(q).Where("cat_id IN (?)", pagination.Cat_id).Limit(pagination.Limit).Offset(offset).Find(&prod)
		}
		return &prod, nil
	} else {
		if pagination.Cat_id == nil {
			config.DB.Model(&models.Products_2{}).Where(g).Limit(pagination.Limit).Offset(offset).Find(&prod_2)
		} else {
			config.DB.Model(&models.Products_2{}).Where(g).Where("cat_id IN (?)", pagination.Cat_id).Limit(pagination.Limit).Offset(offset).Find(&prod_2)
		}
		return nil, &prod_2
	}
}
func GetAllProducts_by_multi_params(c *gin.Context) {
	pagination := GenerateMultiParams(c)
	var prod models.Products
	var prod_2 models.Products_2
	ProdLists, _ := GetAllIproducts(&prod, &prod_2, &pagination)
	c.JSON(http.StatusOK, gin.H{
		"data": ProdLists,
	})
}

答案1

得分: 0

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

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

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

ProdLists, Prod2Lists := GetAllIproducts(&prod, &prod_2, &pagination)
如果 ProdLists 不为 nil {
    c.JSON(http.StatusOK, gin.H{
        "data": ProdLists,
    })
} else {
    c.JSON(http.StatusOK, gin.H{
        "data": Prod2Lists,
})}
英文:

The second return value is hold by the underscore _ in

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

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

ProdLists, Prod2Lists := GetAllIproducts(&prod, &prod_2, &pagination)
If ProdLists != nil {
    c.JSON(http.StatusOK, gin.H{
        "data": ProdLists,
    })
} else {
    c.JSON(http.StatusOK, gin.H{
        "data": Prod2Lists,
})}

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:

确定