{"id":207243,"date":"2025-07-08T10:13:36","date_gmt":"2025-07-08T02:13:36","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207243\/"},"modified":"2025-07-08T10:13:36","modified_gmt":"2025-07-08T02:13:36","slug":"%e6%8c%89%e5%88%9b%e5%bb%ba%e6%97%a5%e6%9c%9f%e6%8e%92%e5%ba%8f%e5%88%97%e8%a1%a8-golang","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207243\/","title":{"rendered":"\u6309\u521b\u5efa\u65e5\u671f\u6392\u5e8f\u5217\u8868 golang"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u6309\u521b\u5efa\u65e5\u671f\u6392\u5e8f\u5217\u8868 golang<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-21 08:18:39<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u6309\u521b\u5efa\u65e5\u671f\u6392\u5e8f\u5217\u8868 golang\u300b<\/span>\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6709\u4e00\u4e2a\u8fd4\u56de inspections \u6a21\u578b\u5b9e\u4f8b\u7684\u51fd\u6570\uff0c\u6211\u60f3\u901a\u8fc7 <code>createddate<\/code> \u5bf9\u5176\u8fdb\u884c <code>sort<\/code> \u4f46\u5728\u7f16\u8bd1\u540e\u6211\u5f97\u5230\u4e86 <\/p>\n<p> \u4e0d\u80fd\u5728\u8fd4\u56de\u53c2\u6570\u4e2d\u4f7f\u7528 inspections[i].createddate \uff08\u5b57\u7b26\u4e32\u7c7b\u578b\uff09\u4f5c\u4e3a bool \u7c7b\u578b <\/p>\n<p><code>inspection.go<\/code> \u662f<\/p>\n<pre>type inspection struct {\n    id          int64               `db:\"id,omitempty\" json:\"id,omitempty\"`\n    createddate string              `db:\"created,omitempty\" json:\"created_date,omitempty\"`\n    records     []*inspectionrecord `db:\"-\" json:\"records,omitempty\"`\n    inspectionfields\n}<\/pre>\n<p><code>list.go<\/code> \u662f<\/p>\n<pre>import (\n    \"sort\"\n)\n\nfunc (s *Manager) list(fields *inspection.ItemIdField) (*inspection.InspectionHistoryResponse, error) {\n    return s.listItemInspectionHistory(fields.ItemId)\n}\n\nfunc (s *Manager) listItemInspectionHistory(itemId string) (*inspection.InspectionHistoryResponse, error) {\n    g := config.Client.Inspections()\n\n    var inspections []*models.Inspection\n\n    inspections, err := g.FindInspections(itemId)\n\n    if err != nil {\n        s.Log.Debugf(\"Can't find inspections of item with id %s\", itemId)\n        return nil, err\n    }\n    s.Log.Debugf(\"Found %d inspections for item with id %s\", len(inspections), itemId)\n\n    for _, inspection := range inspections {\n        inspection.Records, err = g.FindRecords(inspection.Id)\n        if err != nil {\n            s.Log.Debugf(\"Can't find records for inspection with id %d\", inspection.Id)\n            return nil, err\n        }\n        s.Log.Debugf(\"Found %d records for inspection with id %d\", len(inspection.Records), inspection.Id)\n    }\n\n    model := new(models.InspectionHistory)\n    model.Inspections = inspections\n    \/\/ sort by CreatedDate\n    sort.Slice(inspections, func(i, j int) bool { return inspections[i].CreatedDate })\n\n    return transform.InspectionHistoryModelToProtobufResponse(model)\n}<\/pre>\n<p>\u9519\u8bef\u5f88\u660e\u663e\uff0c\u4f46\u6211\u5bf9\u5982\u4f55\u89e3\u51b3\u5b83\u6709\u70b9\u56f0\u60d1\uff0c\u6709\u4eba\u53ef\u4ee5\u5411\u6211\u89e3\u91ca\u5982\u4f55\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5417\uff1f\u8c22\u8c22\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u5fc5\u987b\u89e3\u6790\u65e5\u671f\u5b57\u7b26\u4e32\u5e76\u5c06\u5b83\u4eec\u4f5c\u4e3a \u4e2a\u5b9e\u4f8b\u8fdb\u884c\u6bd4\u8f83<\/p>\n<p>\u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u6709\u6548\u7684\u65e5\u671f\uff0c\u5e76\u4e14\u4ed6\u4eec\u7684\u53f7\u7801\u662f \uff0c\u60a8\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c<\/p>\n<pre>sort.Slice(inspections, func(i, j int) bool {\n        t1, _ := time.Parse(time.RFC3339, inspections[i].CreatedDate)\n        t2, _ := time.Parse(time.RFC3339, inspections[j].CreatedDate)\n        return t1.After(t2)\n    })<\/pre>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u6309\u521b\u5efa\u65e5\u671f\u6392\u5e8f\u5217\u8868 golang\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207243","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207243"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207243\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}