{"id":107,"date":"2020-10-06T23:10:53","date_gmt":"2020-10-07T07:10:53","guid":{"rendered":"http:\/\/iaastalk.com\/?p=107"},"modified":"2020-10-06T23:10:53","modified_gmt":"2020-10-07T07:10:53","slug":"can-done-be-used-multiple-times-in-golang-context","status":"publish","type":"post","link":"http:\/\/iaastalk.com\/?p=107","title":{"rendered":"Can done() be used multiple times in golang context?"},"content":{"rendered":"\n<p>Yes done() can be used as many times as you want.<\/p>\n\n\n\n<p>here is an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n        \"context\"\n        \"time\"\n)\nfunc f(ctx context.Context) {\n\tfor i:=0;i&lt;10;i++{\n\t\tselect {\n\t\tcase &lt;- ctx.Done():\n\t\t\tprintln(\"canceled\")\n\t\tdefault:\n\t\t\tprintln(\".\")\n\n\t\t}\n\n\t\ttime.Sleep(1*time.Second)\n\t}\n\tprintln(\"f exit\")\n}\n\nfunc main() {\n\n        ctx, cancel := context.WithCancel(context.Background())\n        go f(ctx)\n        time.Sleep(3*time.Second)\n        println(\"call ctx cancel()\")\n        cancel()\n        time.Sleep(10*time.Second)\n        println(\"exit\")\n}<\/code><\/pre>\n\n\n\n<p>results:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>go run cancel.go\n.\n.\n.\ncall ctx cancel()\ncanceled\ncanceled\ncanceled\ncanceled\ncanceled\ncanceled\ncanceled\nf exit\nexit<\/code><\/pre>\n\n\n\n<p>See &#8220;canceled&#8221; got returned constantly after context was canceled.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yes done() can be used as many times as you want. here is an example: results: See &#8220;canceled&#8221; got returned constantly after context was canceled.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/iaastalk.com\/index.php?rest_route=\/wp\/v2\/posts\/107"}],"collection":[{"href":"http:\/\/iaastalk.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/iaastalk.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/iaastalk.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/iaastalk.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=107"}],"version-history":[{"count":1,"href":"http:\/\/iaastalk.com\/index.php?rest_route=\/wp\/v2\/posts\/107\/revisions"}],"predecessor-version":[{"id":108,"href":"http:\/\/iaastalk.com\/index.php?rest_route=\/wp\/v2\/posts\/107\/revisions\/108"}],"wp:attachment":[{"href":"http:\/\/iaastalk.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/iaastalk.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=107"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/iaastalk.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}