在给mirrors变量分配内存之前,需要使用make函数来初始化切片。

修改代码如下:

mirrors := make([]model.Mirror, 0)
tx := srv.db.Table("mirror").Where("1=1").Find(&mirrors)
if tx.Error != nil {
    logger.Error(tx.Error.Error())
    return nil, tx.Error
}

这样就可以避免mirrors为空指针的问题。

var mirrors modelMirror	tx = srvdbTablemirrorWhere1=1Find&mirrors	if tx != nil 		loggerErrortxErrorError		return nil txError	gorm报错panicmirrors 空指针如何解决

原文地址: http://www.cveoy.top/t/topic/h08x 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录