我有一个返回检查模型实例的函数,我想用sort
CreatedDate
进行编译,但编译后得到
不能使用Inspections [i] .CreatedDate(类型字符串)作为返回参数中的bool类型
inspection.go
是
type Inspection struct {
Id int64 `db:"id,omitempty" json:"id,omitempty"`
CreatedDate string `db:"created,omitempty" json:"created_date,omitempty"`
Records []*InspectionRecord `db:"-" json:"records,omitempty"`
InspectionFields
}
而list.go
是
import (
"sort"
)
func (s *Manager) list(fields *inspection.ItemIdField) (*inspection.InspectionHistoryResponse, error) {
return s.listItemInspectionHistory(fields.ItemId)
}
func (s *Manager) listItemInspectionHistory(itemId string) (*inspection.InspectionHistoryResponse, error) {
g := config.Client.Inspections()
var inspections []*models.Inspection
inspections, err := g.FindInspections(itemId)
if err != nil {
s.Log.Debugf("Can't find inspections of item with id %s", itemId)
return nil, err
}
s.Log.Debugf("Found %d inspections for item with id %s", len(inspections), itemId)
for _, inspection := range inspections {
inspection.Records, err = g.FindRecords(inspection.Id)
if err != nil {
s.Log.Debugf("Can't find records for inspection with id %d", inspection.Id)
return nil, err
}
s.Log.Debugf("Found %d records for inspection with id %d", len(inspection.Records), inspection.Id)
}
model := new(models.InspectionHistory)
model.Inspections = inspections
// sort by CreatedDate
sort.Slice(inspections, func(i, j int) bool { return inspections[i].CreatedDate })
return transform.InspectionHistoryModelToProtobufResponse(model)
}
该错误很明显,但是我对如何解决它感到困惑,有人可以向我解释如何解决此问题吗?谢谢。