{"id":93377,"date":"2020-04-04T08:21:34","date_gmt":"2020-04-04T00:21:34","guid":{"rendered":"http:\/\/4563.org\/?p=93377"},"modified":"2020-04-04T08:21:34","modified_gmt":"2020-04-04T00:21:34","slug":"golang-map-%e5%85%a8%e5%b1%80%e5%8f%98%e9%87%8f%e4%bd%bf%e7%94%a8%e9%97%ae%e9%a2%98%ef%bc%8c%e8%af%b7%e5%90%84%e4%bd%8d%e5%a4%a7%e4%bd%ac%e7%95%99%e6%ad%a5%e4%b8%80%e8%a7%82%e3%80%82","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=93377","title":{"rendered":"Golang map \u5168\u5c40\u53d8\u91cf\u4f7f\u7528\u95ee\u9898\uff0c\u8bf7\u5404\u4f4d\u5927\u4f6c\u7559\u6b65\u4e00\u89c2\u3002"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Golang map \u5168\u5c40\u53d8\u91cf\u4f7f\u7528\u95ee\u9898\uff0c\u8bf7\u5404\u4f4d\u5927\u4f6c\u7559\u6b65\u4e00\u89c2\u3002               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : PEIENYKYK <\/span>  <span><i><\/i> 35<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5404\u4f4d\u5927\u4f6c\u5927\u5bb6\u597d\uff0c\u8fd8\u662f\u5c0f\u5f1f\u6211\uff0c\u8fd9\u6b21\u5f00\u53d1\u9047\u5230\u4e86\u4e00\u4e2a\u633a\u5751\u7684\u95ee\u9898<br \/> \u9996\u5148\u8bf4\u4e00\u4e0b\u9700\u6c42\uff0c\u6211\u8981\u5b8c\u6210\u4e00\u4e2a\u4efb\u52a1\u7684\u5f00\u59cb\u505c\u6b62\uff0c\u5bf9\uff0c\u5c31\u662f\u8fd9\u4e48\u7b80\u5355\u3002<br \/> \u5c0f\u5f1f\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5168\u5c40\u7684 map \u53d8\u91cf\u53bb\u5b58\u50a8 job \u7684\u4fe1\u606f<\/p>\n<pre><code>\/\/SyncJob \u4efb\u52a1\u8be6\u60c5 type SyncJob struct {  ID     string  Ctx    context.Context  Cancel context.CancelFunc }  \/\/Job \u5355\u4e2a\u540c\u6b65\u4efb\u52a1 var Job = make(map[string]interface{})  \/\/JobWork job \u5217\u8868 var JobWork = make(map[string][]map[string]interface{}) <\/code><\/pre>\n<p>\u7136\u540e\u6211\u5b9a\u4e49\u4e86\u4e00\u4e2a startwork \u548c stopwork\uff0c<\/p>\n<pre><code>\/\/SyncStart \u4efb\u52a1\u5f00\u59cb func SyncStart(id string) (msg string, err error) {  var job SyncJob  ctx, cancel := context.WithCancel(context.Background())     go do work(id)  job.ID = id  job.Ctx = ctx  job.Cancel = cancel  Job[id] = job  JobWork[\"job\"] = append(JobWork[\"job\"], Job)  return \"Start work success\", nil } \/\/SyncStop \u4efb\u52a1\u7ed3\u675f func SyncStop(id string) (msg string, err error) {  for _, i := range JobWork[\"job\"] {   jobss := i[id]   op, _ := jobss.(SyncJob)   defer op.Cancel()  }  return \"Stop work success\", nil } <\/code><\/pre>\n<p>\u6211\u5c1d\u8bd5\u4e86\u4e00\u4e0b\u8fd9\u79cd\u5199\u6cd5<\/p>\n<pre><code>infiapi.SyncStart(\"123456\") time.Sleep(time.Second * 10) infiapi.SyncStop(\"123456\") <\/code><\/pre>\n<p>\u8fd9\u6837\u662f\u53ef\u4ee5\u505c\u6b62\u4efb\u52a1\u7684\u3002 \u4f46\u662f\uff01\u5c0f\u5f1f\u5199\u4e86\u4e00\u4e2a web server, \u60f3\u5728 web server \u4e2d\u505c\u6b62\u5b83<\/p>\n<pre><code>func StartSyncwork(w http.ResponseWriter, r *http.Request) {  r.ParseForm()       \/\/\u89e3\u6790\u53c2\u6570\uff0c\u9ed8\u8ba4\u662f\u4e0d\u4f1a\u89e3\u6790\u7684  fmt.Println(r.Form) \/\/\u8fd9\u4e9b\u4fe1\u606f\u662f\u8f93\u51fa\u5230\u670d\u52a1\u5668\u7aef\u7684\u6253\u5370\u4fe1\u606f  id := r.Form[\"uid\"]  infiapi.SyncStart(string(id[0])) }  func StopSyncWork(w http.ResponseWriter, r *http.Request) {  r.ParseForm()       \/\/\u89e3\u6790\u53c2\u6570\uff0c\u9ed8\u8ba4\u662f\u4e0d\u4f1a\u89e3\u6790\u7684  fmt.Println(r.Form) \/\/\u8fd9\u4e9b\u4fe1\u606f\u662f\u8f93\u51fa\u5230\u670d\u52a1\u5668\u7aef\u7684\u6253\u5370\u4fe1\u606f  id := r.Form[\"uid\"]  infiapi.SyncStop(string(id[0])) } ........ http.HandleFunc(\"\/start\", StartSyncwork) \/\/\u8bbe\u7f6e\u8bbf\u95ee\u7684\u8def\u7531 http.HandleFunc(\"\/stop\", StopSyncWork) <\/code><\/pre>\n<p>\u8fd9\u6837\u8c03\u7528\u5f00\u542f\u53ef\u4ee5\uff0c\u4f46\u662f stop \u65e0\u6cd5\u505c\u6b62\uff0c\u662f map \u4f5c\u7528\u57df\u7684\u95ee\u9898\u4e48\uff1f\u8fd9\u6837\u8be5\u5982\u4f55\u89e3\u51b3\u5462\uff1f\uff1f\u62dc\u8c22\uff01<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>12<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1121601\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : OakScript <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             net\/http \u672c\u8d28\u662f\u591a goroutine \u6a21\u578b\uff0cgolang \u7684 map \u5e76\u53d1\u8bfb\u5199\u4f1a panic&#8230;                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1121602\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : PEIENYKYK <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @OakScript \u6211\u8bd5\u4e0b\u52a0\u9501\uff0c\u611f\u6069\u8001\u54e5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1121603\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : songjiaxin2008 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u9996\u5148\u8bb0\u5f97\u52a0\u4e2a\u9501&#8230; \u4e0d\u7136\u4f1a\u6709\u7ade\u4e89\u8bfb\u5199\u95ee\u9898<br \/>\u53ef\u4ee5\u7528`sync.Map`<\/p>\n<p>\u548c\u4f5c\u7528\u57df\u65e0\u5173\u3002<br \/>`SyncStop`\u4e3a\u4ec0\u4e48\u8981\u7528`defer`\u5462 \u76f4\u63a5`op.Cancel()` \u5c31\u53ef\u4ee5\u4e86<br \/>\u53e6\u5916\u4f60\u8fd9\u4e2a\u591a\u5c42\u5957\u5a03\u770b\u7684\u6709\u70b9\u6655\u554a\u3002\u3002\u3002\u641e\u4e2a repo \u53ef\u4ee5\u5e2e\u4f60\u770b\u4e0b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1121604\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : bobuick <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5f88\u4e45\u6ca1\u5199 golang \u4e86\uff0cdefer \u8fd9\u6837\u5728 for \u91cc\u9762\u7528\uff0c\u7591\u4f3c\u6709\u5751\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1121605\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : songjiaxin2008 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             for \u91cc\u9762\u8c03 defer \u6709\u6cc4\u6f0f\u7684\u53ef\u80fd                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1121606\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : OakScript <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u641e\u4e2a\u4e34\u65f6 repo \u6216\u8005\u641e\u4e2a golang playground \u51fa\u6765\u5427\uff0c\u8fd9\u6837\u5927\u5bb6\u597d\u5e2e\u4f60\u8c03\u8bd5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1121607\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : PEIENYKYK <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @OakScript \u8c22\u8c22\u8001\u54e5\uff0c\u95ee\u9898\u5df2\u7ecf\u89e3\u51b3\uff0c\u52a0\u4e86\u9501\uff0c\u4f18\u5316\u4e86\u4e00\u4e0b\u4ee3\u7801\uff0c\u611f\u6069\u611f\u6069\uff01\u8c22\u8c22\u60a8\uff01                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1121608\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : PEIENYKYK <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @songjiaxin2008 \u95ee\u9898\u5df2\u7ecf\u89e3\u51b3\uff0c\u52a0\u4e86\u9501\uff0c\u4f18\u5316\u4e86\u4e00\u4e0b\u4ee3\u7801\uff0c\u611f\u6069\u611f\u6069\uff01\u8c22\u8c22\u60a8\uff01                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1121609\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : beidounanxizi <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u628a map \u4f5c\u4e3a\u4e00\u4e2a channel \u7a7f\u6765\u7a7f\u53bb \u53ef\u89e3\u5fe7                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1121610\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : useben <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             1 \u3001for \u91cc\u9762\u4e0d\u8981 defer\uff0c \u4f1a\u6709\u6cc4\u9732\u95ee\u9898<br \/>2 \u3001\u4efb\u52a1\u8c03\u5ea6\u5668\u4e00\u822c\u4e0d\u8fd9\u6837\u505a\u7684\u3002\u8d77\u4e00\u4e2a\u8c03\u5ea6 goroutine \u4f5c\u4e3a\u8c03\u5ea6\u5668\uff0c\u4efb\u52a1\u961f\u5217 chan\uff0c\u6bcf\u4e2a\u4efb\u52a1 go \u4e00\u4e2a\u534f\u7a0b\u6765\u6267\u884c\uff0c\u5e76\u4e14\u521b\u5efa\u4e00\u4e2a stopChan \u6765\u7b49\u5f85 stop \u4e8b\u4ef6\u505c\u6b62\u4efb\u52a1\u3002\u7136\u540e\u901a\u8fc7 chan \u6216\u8005\u961f\u5217\u7684\u65b9\u5f0f\u6765\u6295\u9012\u4efb\u52a1\u548c\u76d1\u542c chan \u6765\u6d88\u8d39\u4efb\u52a1\u3002\u63a5\u53e3 start \u521b\u5efa\u4efb\u52a1\u548c\u6254\u8fdb chan \u3002stop \u63a5\u53e3\u53d1\u9001 stop \u4e8b\u4ef6\u5230\u5bf9\u5e94 stopChan\uff0c\u8fbe\u5230\u505c\u6b62\u4efb\u52a1\u7684\u76ee\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1121611\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wnanbei <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @useben \u73b0\u5728 1.14 \u91cc\u8fd8\u4f1a\u6709 for \u91cc\u9762 defer \u4f1a\u6cc4\u9732\u7684\u95ee\u9898\u5417\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1121612\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fighterlyt <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @wnanbei \u4e00\u770b\u5c31\u662f\u4e0d\u77e5\u9053\u4e3a\u4ec0\u4e48\u4f1a\u6cc4\u9732                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Golang map \u5168\u5c40\u53d8\u91cf\u4f7f\u7528&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/93377"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=93377"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/93377\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=93377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=93377"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=93377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}