{"id":207788,"date":"2025-07-08T12:28:09","date_gmt":"2025-07-08T04:28:09","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207788\/"},"modified":"2025-07-08T12:28:09","modified_gmt":"2025-07-08T04:28:09","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88%e7%bb%93%e6%9e%84%e5%86%85%e9%83%a8%e7%9a%84%e6%98%a0%e5%b0%84%e4%b8%8d%e6%8e%a5%e5%8f%97%e8%b5%8b%e5%80%bc","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207788\/","title":{"rendered":"\u4e3a\u4ec0\u4e48\u7ed3\u6784\u5185\u90e8\u7684\u6620\u5c04\u4e0d\u63a5\u53d7\u8d4b\u503c"},"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>\u4e3a\u4ec0\u4e48\u7ed3\u6784\u5185\u90e8\u7684\u6620\u5c04\u4e0d\u63a5\u53d7\u8d4b\u503c<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-25 17:06:37<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4e3a\u4ec0\u4e48\u7ed3\u6784\u5185\u90e8\u7684\u6620\u5c04\u4e0d\u63a5\u53d7\u8d4b\u503c\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<pre>type datedata struct {\n    count     int\n    districts map[string]int\n}<\/pre>\n<p>\u8fd9\u662f\u6211\u9047\u5230\u95ee\u9898\u7684\u7ed3\u6784\u3002<\/p>\n<pre>var dates = make(map[string]datedata)<\/pre>\n<p>\u8fd9\u5c31\u662f\u6211\u521d\u59cb\u5316\u548c\u4f7f\u7528\u6240\u8ff0\u7ed3\u6784\u7684\u65b9\u5f0f<\/p>\n<p>\u4e3a\u4ec0\u4e48\u6211\u4f1a\u6536\u5230 <code> \u6050\u614c\u9519\u8bef\uff1a\u8d4b\u503c\u7ed9 nil map<\/code> \u4e2d\u7684\u6761\u76ee<\/p>\n<p>\u5f53\u6211\u5c1d\u8bd5\u4e3a\u5730\u533a\u5730\u56fe\u5206\u914d\u4e00\u4e2a\u503c\u65f6\uff0c\u5982\u4e0b\u6240\u793a\uff1adates[&#8220;1\/1\/06&#8221;].districts[&#8220;1&#8221;] = 1 \u6211\u65e0\u6cd5\u4e3a districts \u5b57\u6bb5\u5206\u914d\u503c<\/p>\n<p>\u8fd9\u662f\u4e0a\u4e0b\u6587\u7684\u5b8c\u6574\u4ee3\u7801\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"encoding\/csv\"\n    \"fmt\"\n    \"io\"\n    \"log\"\n    \"os\"\n    \"strings\"\n)\n\ntype Report struct {\n    Date      string `json:\"date\"`\n    Address   string `json:\"address\"`\n    District  string `json:\"district\"`\n    Beat      string `json:\"beat\"`\n    Grid      string `json:\"grid\"`\n    Crimedesc string `json:\"crimedesc\"`\n    UCRCode   string `json:\"ucr_ncic_code\"`\n    Lat       string `json:\"latitude\"`\n    Lon       string `json:\"longitude\"`\n}\n\ntype DateData struct {\n    Count     int\n    Districts map[string]int\n}\n\nfunc main() {\n\n    reports := CreateReport()\n\n    var dates = make(map[string]DateData)\n\n    for _, report := range reports {\n\n        dateIndex := strings.LastIndex(report.Date, \"\/\")\n        date := report.Date[0 : dateIndex+3]\n\n        val, ok := dates[date]\n        if !ok {\n            dates[date] = DateData{\n                Count: 1,\n            }\n\n        } else {\n            val.Count += 1\n            dates[date] = val\n        }\n\n    }\n\n    dates[\"1\/1\/06\"].Districts[\"1\"] = 1\n\n    fmt.Println(\"dates slice\", dates)\n    \/\/ fmt.Println(\"dates slice\", dates)\n\n}\n\nfunc CreateReport() []Report {\n    \/\/ csvFile, err := os.Open(\".\/files\/sacramento-jan-2006.csv\")\n    csvFile, err := os.Open(\".\/files\/sacramento-test.csv\")\n    if err != nil {\n        log.Fatal(\"error opeining file\", err)\n    }\n\n    defer csvFile.Close()\n\n    reader := csv.NewReader(csvFile)\n    var reports []Report\n\n    for {\n        line, err := reader.Read()\n        if err == io.EOF {\n            break\n        } else if err != nil {\n            log.Fatal(\"error reading file\", err)\n        }\n\n        reports = append(reports, Report{\n            Date:      line[0],\n            Address:   line[1],\n            District:  line[2],\n            Beat:      line[3],\n            Grid:      line[4],\n            Crimedesc: line[5],\n            UCRCode:   line[6],\n            Lat:       line[7],\n            Lon:       line[8],\n        })\n\n    }\n\n    return reports\n}<\/pre>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u5b83\u62b1\u6028 <code>districts<\/code> \u5730\u56fe\uff0c\u56e0\u4e3a\u8be5\u5730\u56fe\u4e3a\u96f6\u3002\u521d\u59cb\u5316\u4e3a\uff1a<\/p>\n<pre>if !ok {\n            dates[date] = DateData{\n                Count: 1,\n                Districts: make(map[string]int),\n            }<\/pre>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u4e3a\u4ec0\u4e48\u7ed3\u6784\u5185\u90e8\u7684\u6620\u5c04\u4e0d\u63a5\u53d7\u8d4b\u503c\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\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-207788","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207788","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=207788"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207788\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207788"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207788"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207788"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}