{"id":1599,"date":"2012-04-01T19:33:04","date_gmt":"2012-04-01T10:33:04","guid":{"rendered":"http:\/\/peta.okechan.net\/blog\/?p=1599"},"modified":"2012-04-01T19:35:37","modified_gmt":"2012-04-01T10:35:37","slug":"a-tour-of-go%e3%81%ae68","status":"publish","type":"post","link":"https:\/\/peta.okechan.net\/blog\/archives\/1599","title":{"rendered":"A Tour of Go\u306e68"},"content":{"rendered":"<p><a href=\"http:\/\/tour.golang.org\/#68\" target=\"_blank\">A Tour of Go\u306e68 Exercise: Equivalent Binary Trees<\/a>\u3092\u81ea\u5206\u306a\u308a\u306b\u304c\u3093\u3070\u3063\u305f\u306e\u3067\u3001\u3053\u3053\u306b\u30bd\u30fc\u30b9\u3092\u4fdd\u5b58\u3057\u3066\u304a\u304d\u307e\u3059\u3002\uff08\u518d\u5e30\u4f7f\u3063\u3066\u308b\u306e\u304c\u30a2\u30ec\u3067\u3059\u304c\u2026\uff09<br \/>\nGo\u8a00\u8a9e\u3092\u4f7f\u3044\u59cb\u3081\u3066\u307e\u30601\u65e5\u3082\u7d4c\u3063\u3066\u306a\u3044\u306e\u3067Go\u306e\u4f5c\u6cd5\u7684\u306b\u3069\u3046\u306a\u3093\u3067\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">package main\r\n\r\nimport (\r\n\t&quot;tour\/tree&quot;\r\n\t&quot;fmt&quot;\r\n)\r\n\r\n\/\/ Walk walks the tree t sending all values\r\n\/\/ from the tree to the channel ch.\r\nfunc Walk(t *tree.Tree, ch chan int) {\r\n\t_walk := func(t *tree.Tree){}\r\n\t_walk = func(t *tree.Tree) {\r\n\t\tif t.Left != nil {\r\n\t\t\t_walk(t.Left)\r\n\t\t}\r\n\t\tch &lt;- t.Value\r\n\t\tif t.Right != nil {\r\n\t\t\t_walk(t.Right)\r\n\t\t}\r\n\t}\r\n\t_walk(t)\r\n\tclose(ch)\r\n}\r\n\r\n\/\/ Same determines whether the trees\r\n\/\/ t1 and t2 contain the same values.\r\nfunc Same(t1, t2 *tree.Tree) bool {\r\n\tch1 := make(chan int)\r\n\tch2 := make(chan int)\r\n\tgo Walk(t1, ch1)\r\n\tgo Walk(t2, ch2)\r\n\tfor {\r\n\t\tv1, ok1 := &lt;-ch1\r\n\t\tv2, ok2 := &lt;-ch2\r\n\t\tif ok1 != ok2 || v1 != v2 {\r\n\t\t\treturn false\r\n\t\t}\r\n\t\tif !ok1 &amp;&amp; !ok2 {\r\n\t\t\tbreak\r\n\t\t}\r\n\t}\r\n\treturn true\r\n}\r\n\r\nfunc main() {\r\n\tch := make(chan int)\r\n\tgo Walk(tree.New(1), ch)\r\n\tfor i := 0; i &lt; 10; i++ {\r\n\t\tv, ok := &lt;-ch\r\n\t\tif !ok {break}\r\n\t\tfmt.Println(v)\r\n\t}\r\n\tfmt.Println(Same(tree.New(1), tree.New(1)))\r\n\tfmt.Println(Same(tree.New(1), tree.New(2)))\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p><a href=\"http:\/\/tour.golang.org\/#68\" target=\"_blank\">A Tour of Go\u306e68 Exercise: Equivalent Binary Trees<\/a>\u3092\u81ea\u5206\u306a\u308a\u306b\u304c\u3093\u3070\u3063\u305f\u306e\u3067\u3001\u3053\u3053\u306b\u30bd\u30fc\u30b9\u3092\u4fdd\u5b58\u3057\u3066\u304a\u304d\u307e\u3059\u3002\uff08\u518d\u5e30\u4f7f\u3063\u3066\u308b\u306e\u304c\u30a2\u30ec\u3067\u3059\u304c\u2026\uff09<br \/>\nGo\u8a00\u8a9e\u3092\u4f7f\u3044\u59cb\u3081\u3066\u307e\u30601\u65e5\u3082\u7d4c\u3063\u3066\u306a\u3044\u306e\u3067Go\u306e\u4f5c\u6cd5\u7684\u306b\u3069\u3046\u306a\u3093\u3067\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">package main\r\n\r\nimport (\r\n\t&quot;tour\/tree&quot;\r\n\t&quot;fmt&quot;\r\n)\r\n\r\n\/\/ Walk walks the tree t sending all values\r\n\/\/ from the tree to the channel ch.\r\nfunc Walk(t *tree.Tree, ch chan int) {\r\n\t_walk := func(t *tree.Tree){}\r\n\t_walk = func(t *tree.Tree) {\r\n\t\tif t.Left != nil {\r\n\t\t\t_walk(t.Left)\r\n\t\t}\r\n\t\tch &lt;- t.Value\r\n\t\tif t.Right != nil {\r\n\t\t\t_walk(t.Right)\r\n\t\t}\r\n\t}\r\n\t_walk(t)\r\n\tclose(ch)\r\n}\r\n\r\n\/\/ Same determines whether the trees\r\n\/\/ t1 and t2 contain the same values.\r\nfunc Same(t1, t2 *tree.Tree) bool {\r\n\tch1 := make(chan int)\r\n\tch2 := make(chan int)\r\n\tgo Walk(t1, ch1)\r\n\tgo Walk(t2, ch2)\r\n\tfor {\r\n\t\tv1, ok1 := &lt;-ch1\r\n\t\tv2, ok2 := &lt;-ch2\r\n\t\tif ok1 != ok2 || v1 != v2 {\r\n\t\t\treturn false\r\n\t\t}\r\n\t\tif !ok1 &amp;&amp; !ok2 {\r\n\t\t\tbreak\r\n\t\t}\r\n\t}\r\n\treturn true\r\n}\r\n\r\nfunc main() {\r\n\tch := make(chan int)\r\n\tgo Walk(tree.New(1), ch)\r\n\tfor i := 0; i &lt; 10; i++ {\r\n\t\tv, ok := &lt;-ch\r\n\t\tif !ok {break}\r\n\t\tfmt.Println(v)\r\n\t}\r\n\tfmt.Println(Same(tree.New(1), tree.New(1)))\r\n\tfmt.Println(Same(tree.New(1), tree.New(2)))\r\n}<\/pre>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[410],"class_list":["post-1599","post","type-post","status-publish","format-standard","hentry","category-tech","tag-go"],"_links":{"self":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/1599","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/comments?post=1599"}],"version-history":[{"count":0,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/1599\/revisions"}],"wp:attachment":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/media?parent=1599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/categories?post=1599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/tags?post=1599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}