{"id":4109,"date":"2023-10-06T21:18:37","date_gmt":"2023-10-06T12:18:37","guid":{"rendered":"https:\/\/dennie.tokyo\/it\/?p=4109"},"modified":"2025-07-10T19:22:34","modified_gmt":"2025-07-10T10:22:34","slug":"%e3%80%90go%e3%80%91%e3%83%87%e3%83%83%e3%83%88%e3%83%ad%e3%83%83%e3%82%af","status":"publish","type":"post","link":"https:\/\/dennie.tokyo\/it\/?p=4109","title":{"rendered":"\u3010Go\u3011\u30ed\u30c3\u30af\u3068\u30c7\u30c3\u30c8\u30ed\u30c3\u30af"},"content":{"rendered":"\n<p>\u51e6\u7406\u306e\u30ed\u30c3\u30af\u3084\u30c7\u30c3\u30c8\u30ed\u30c3\u30af\u304c\u8d77\u304d\u308b\u30d1\u30bf\u30fc\u30f3<\/p>\n\n\n\n<!--more-->\n\n\n\n<h1 class=\"wp-block-heading\">\u30c7\u30c3\u30c8\u30ed\u30c3\u30af\u30c1\u30e3\u30cd\u30eb\u306e\u8aad\u307f\u8fbc\u307f\u306e\u307f<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9f\u884c\u4f8b<\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>package main\n\nimport (\n\t&quot;fmt&quot;\n\t&quot;time&quot;\n)\n\n\/\/ \u30b4\u30eb\u30fc\u30c1\u30f3\u3067\u30c1\u30e3\u30cd\u30eb\u304c\u66f8\u304d\u8fbc\u307e\u308c\u306a\u3044\nfunc sampleGoRoutineA(channel chan&lt;- string) {\n\tfmt.Printf(&quot;\u3010\u958b\u59cb\u3011 &quot; + time.Now().Format(time.TimeOnly) + &quot; sampleGoRoutineA \\n&quot;)\n\ttime.Sleep(5 * time.Second)\n\tfmt.Printf(&quot;\u3010\u7d42\u4e86\u3011 &quot; + time.Now().Format(time.TimeOnly) + &quot; sampleGoRoutineA \\n&quot;)\n\t\/\/channel &lt;- &quot;\u7d42\u4e86 \\n&quot;\n}\n\nfunc main() {\n\tchannelA := make(chan string)\n\tgo sampleGoRoutineA(channelA)\n\tval := &lt;-channelA\n\tfmt.Print(val)\n}\n<\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">\u7d50\u679c<\/h3>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>\u3010\u958b\u59cb\u3011 21:13:08 sampleGoRoutineA \n\u3010\u7d42\u4e86\u3011 21:13:13 sampleGoRoutineA \nfatal error: all goroutines are asleep - deadlock!\n\ngoroutine 1 [chan receive]:\nmain.main()\n\t\/Users\/hoge\/main.go:53 +0x7f\nexit status 2\n<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u63a8\u6e2c<\/h2>\n\n\n\n<p>\u30b5\u30d6\u30eb\u30fc\u30c1\u30f3\u304c\u306a\u304f\u306a\u308a\u3001\u30c1\u30e3\u30cd\u30eb\u306b\u5024\u304c\u306a\u3044\u72b6\u614b\u3067\u8aad\u307f\u8fbc\u307f\u3092\u884c\u3046\u3068\u30c7\u30c3\u30c8\u30ed\u30c3\u30af\u306b\u306a\u308b\u304b\u3082\u3057\u308c\u3042\u305b\u3093\u3002<br>\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3001\u51e6\u7406\u306b10\u79d2\u304b\u304b\u308b\u95a2\u6570\u30921\u3064\u8ffd\u52a0\u3057\u3001\u30b4\u30eb\u30fc\u30c1\u30f3\u3067\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>package main\n\nimport (\n\t&quot;fmt&quot;\n\t&quot;time&quot;\n)\n\nfunc sampleGoRoutineA(channel chan&lt;- string) {\n\tfmt.Printf(&quot;\u3010\u958b\u59cb\u3011 &quot; + time.Now().Format(time.TimeOnly) + &quot; sampleGoRoutineA \\n&quot;)\n\ttime.Sleep(5 * time.Second)\n\tfmt.Printf(&quot;\u3010\u7d42\u4e86\u3011 &quot; + time.Now().Format(time.TimeOnly) + &quot; sampleGoRoutineA \\n&quot;)\n\t\/\/channel &lt;- &quot;\u7d42\u4e86 \\n&quot;\n}\n\nfunc sampleRoutineB() {\n\tfmt.Printf(&quot;\u3010\u958b\u59cb\u3011 &quot; + time.Now().Format(time.TimeOnly) + &quot; sampleGoRoutineB \\n&quot;)\n\ttime.Sleep(10 * time.Second)\n\tfmt.Printf(&quot;\u3010\u7d42\u4e86\u3011 &quot; + time.Now().Format(time.TimeOnly) + &quot; sampleGoRoutineB \\n&quot;)\n}\n\nfunc main() {\n\tchannelA := make(chan string)\n\tgo sampleGoRoutineA(channelA)\n\tgo sampleRoutineB()\n\n\tval := &lt;-channelA\n\tfmt.Println(val)\n}\n<\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">\u7d50\u679c<\/h3>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>\u3010\u958b\u59cb\u3011 21:32:52 sampleGoRoutineB \n\u3010\u958b\u59cb\u3011 21:32:52 sampleGoRoutineA \n\u3010\u7d42\u4e86\u3011 21:32:57 sampleGoRoutineA \n\u3010\u7d42\u4e86\u3011 21:33:02 sampleGoRoutineB \nfatal error: all goroutines are asleep - deadlock!\n\ngoroutine 1 [chan receive]:\nmain.main()\n\t\/Users\/hoge\/main.go:52 +0x8b\nexit status 2<\/code><\/pre><\/div>\n\n\n\n<p>2\u3064\u306e\u30eb\u30fc\u30c1\u30f3\u306f\u6b63\u5e38\u306b\u5b9f\u884c\u3055\u308c\u307e\u3057\u305f\u3002\u305d\u306e\u5f8c\u30c7\u30c3\u30c8\u30ed\u30c3\u30af\u304c\u767a\u751f\u3057\u3066\u3044\u307e\u3059\u3002\u306a\u306e\u3067\u3001Go\u306e\u30e9\u30f3\u30bf\u30a4\u30e0\u304c\u30eb\u30fc\u30c1\u30f3\u306e\u6570\u3068\u30c1\u30e3\u30cd\u30eb\u306e\u8aad\u307f\u8fbc\u307f\u306e\u95a2\u4fc2\u3092\u5224\u65ad\u3057\u3066\u3044\u308b\u306e\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u30c1\u30e3\u30cd\u30eb\u306e\u66f8\u304d\u8fbc\u307f\u30ed\u30c3\u30af<\/h1>\n\n\n\n<p>\u30c1\u30e3\u30cd\u30eb\u306e\u8aad\u307f\u8fbc\u307f\u3092\u305b\u305a\u3001\u30c1\u30e3\u30cd\u30eb\u306e\u66f8\u304d\u8fbc\u307f\u306e\u307f\u884c\u3046\u5834\u5408\u3001\u30c7\u30c3\u30c8\u30ed\u30c3\u30af\u306b\u306f\u306a\u308a\u307e\u305b\u3093\u3002\u3057\u304b\u3057\u3001\u66f8\u304d\u8fbc\u307f\u30ed\u30c3\u30af\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>package main\n\nimport (\n\t&quot;fmt&quot;\n\t&quot;time&quot;\n)\n\nfunc sampleGoRoutineA(channel chan&lt;- string) {\n\tfmt.Printf(&quot;\u3010\u958b\u59cb\u3011 &quot; + time.Now().Format(time.TimeOnly) + &quot; sampleGoRoutineA \\n&quot;)\n\tchannel &lt;- &quot;\u7d42\u4e86 \\n&quot;  \/\/ \u66f8\u304d\u8fbc\u307f\u3067\u51e6\u7406\u3092\u30ed\u30c3\u30af\u3059\u308b\u3002\n\tfmt.Printf(&quot;\u3010\u7d42\u4e86\u3011 &quot; + time.Now().Format(time.TimeOnly) + &quot; sampleGoRoutineA \\n&quot;)\n}\n\nfunc sampleRoutineB() {\n\tfmt.Printf(&quot;\u3010\u958b\u59cb\u3011 &quot; + time.Now().Format(time.TimeOnly) + &quot; sampleRoutineB \\n&quot;)\n\ttime.Sleep(10 * time.Second)\n\tfmt.Printf(&quot;\u3010\u7d42\u4e86\u3011 &quot; + time.Now().Format(time.TimeOnly) + &quot; sampleRoutineB \\n&quot;)\n}\n\nfunc main() {\n\tchannelA := make(chan string)\n\tgo sampleGoRoutineA(channelA)\n\tsampleRoutineB()\n}\n<\/code><\/pre><\/div>\n\n\n\n<p>\u7d50\u679c<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>\u3010\u958b\u59cb\u3011 21:52:09 sampleRoutineB \n\u3010\u958b\u59cb\u3011 21:52:09 sampleGoRoutineA \n\u3010\u7d42\u4e86\u3011 21:52:19 sampleRoutineB <\/code><\/pre><\/div>\n\n\n\n<p>\u66f8\u304d\u8fbc\u307f\u30ed\u30c3\u30af\u306e\u70ba\u3001&#8221;\u3010\u7d42\u4e86\u3011 21:52:14 sampleGoRoutineA&#8221; \u304c\u8868\u793a\u3055\u308c\u307e\u305b\u3093\u3067\u3057\u305f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u51e6\u7406\u306e\u30ed\u30c3\u30af\u3084\u30c7\u30c3\u30c8\u30ed\u30c3\u30af\u304c\u8d77\u304d\u308b\u30d1\u30bf\u30fc\u30f3<\/p>\n","protected":false},"author":1,"featured_media":968,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[],"class_list":["post-4109","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go"],"_links":{"self":[{"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/posts\/4109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4109"}],"version-history":[{"count":6,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/posts\/4109\/revisions"}],"predecessor-version":[{"id":4117,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/posts\/4109\/revisions\/4117"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/media\/968"}],"wp:attachment":[{"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}