今天学习golang,使用 map 时,报错了: assignment to nil map。

assignment to nil map
报错的代码如下所示:
func (service *StoreService) GetManyByPage(pageCondition PageConditionDTO) (map[string]interface{}, error) {
var returnMap map[string]interface{}
var rowList []map[string]interface{}
whereCondition := map[string]interface{}{"is_del": 0, "is_active": 1}
selectedFields := []string{"id", " ctime ", "mtime", "priority", "is_active", "name", "address"}
rowStartIndex := pageCondition.PageIndex * pageCondition.RowCountPerPage
// Get rows
err := service.DB.Model(&StoreEntity{}).Select(selectedFields).Where(whereCondition).Limit(pageCondition.RowCountPerPage).Offset(rowStartIndex).Order("mtime DESC, id DESC").Find(&rowList).Error
if err != nil {
return returnMap, err
}
// Get row count
rowCount, err := service.getRowCount(whereCondition)
if err != nil {
return returnMap, err
}
returnMap["pageIndex"] = pageCondition.PageIndex
returnMap["pageCount"] = utils.CalPageCount(rowCount, int64(pageCondition.RowCountPerPage))
returnMap["rowCount"] = rowCount
returnMap["rowCountPerPage"] = pageCondition.RowCountPerPage
returnMap["rows"] = rowList
returnMap["rowStartIndex"] = rowStartIndex
return returnMap, nil
}
导致错误的就是这一句:
var returnMap map[string]interface{}
改为如下代码即可
returnMap := map[string]interface{}{}
或者
var returnMap = make(map[string]interface{})