{"id":1250,"date":"2022-12-05T11:25:15","date_gmt":"2022-12-05T03:25:15","guid":{"rendered":"https:\/\/fdream.net\/blog\/?p=1250"},"modified":"2022-12-05T15:06:57","modified_gmt":"2022-12-05T07:06:57","slug":"go%e6%8c%87%e5%8d%97%e7%bb%83%e4%b9%a010-%e7%ad%89%e4%bb%b7%e4%ba%8c%e5%8f%89%e6%9f%a5%e6%89%be%e6%a0%91","status":"publish","type":"post","link":"https:\/\/fdream.net\/blog\/article\/1250","title":{"rendered":"Go\u6307\u5357\u7ec3\u4e6010-\u7b49\u4ef7\u4e8c\u53c9\u67e5\u627e\u6811"},"content":{"rendered":"\n<p>\u8fd9\u4e2a\u662f<a rel=\"noreferrer noopener\" href=\"https:\/\/go.dev\/tour\/list\" target=\"_blank\">A Tour of Go<\/a>\u8fd9\u4e2a\u6559\u7a0b\u91cc\u9762\u7684\u7b2c\u5341\u4e2a\u7ec3\u4e60\uff0c<a href=\"https:\/\/tour.go-zh.org\/concurrency\/8\" target=\"_blank\" rel=\"noreferrer noopener\">\u539f\u9898<\/a>\u5982\u4e0b\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u4e0d\u540c\u4e8c\u53c9\u6811\u7684\u53f6\u8282\u70b9\u4e0a\u53ef\u4ee5\u4fdd\u5b58\u76f8\u540c\u7684\u503c\u5e8f\u5217\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4e24\u4e2a\u4e8c\u53c9\u6811\u90fd\u4fdd\u5b58\u4e86\u5e8f\u5217 `1\uff0c1\uff0c2\uff0c3\uff0c5\uff0c8\uff0c13`\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/fdream.net\/blog\/wp-content\/uploads\/2022\/12\/image-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"491\" height=\"181\" src=\"https:\/\/fdream.net\/blog\/wp-content\/uploads\/2022\/12\/image-2.png\" alt=\"\" class=\"wp-image-1254\" srcset=\"https:\/\/fdream.net\/blog\/wp-content\/uploads\/2022\/12\/image-2.png 491w, https:\/\/fdream.net\/blog\/wp-content\/uploads\/2022\/12\/image-2-300x111.png 300w\" sizes=\"auto, (max-width: 491px) 100vw, 491px\" \/><\/a><\/figure>\n\n\n\n<p>\u5728\u5927\u591a\u6570\u8bed\u8a00\u4e2d\uff0c\u68c0\u67e5\u4e24\u4e2a\u4e8c\u53c9\u6811\u662f\u5426\u4fdd\u5b58\u4e86\u76f8\u540c\u5e8f\u5217\u7684\u51fd\u6570\u90fd\u76f8\u5f53\u590d\u6742\u3002 \u6211\u4eec\u5c06\u4f7f\u7528 Go \u7684\u5e76\u53d1\u548c\u4fe1\u9053\u6765\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u89e3\u6cd5\u3002<\/p>\n\n\n\n<p>\u672c\u4f8b\u4f7f\u7528\u4e86&nbsp;<code>tree<\/code>&nbsp;\u5305\uff0c\u5b83\u5b9a\u4e49\u4e86\u7c7b\u578b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Tree struct {\n    Left  *Tree\n    Value int\n    Right *Tree\n}<\/code><\/pre>\n\n\n\n<p><strong>1.<\/strong>&nbsp;\u5b9e\u73b0&nbsp;<code>Walk<\/code>&nbsp;\u51fd\u6570\u3002<\/p>\n\n\n\n<p><strong>2.<\/strong>&nbsp;\u6d4b\u8bd5&nbsp;<code>Walk<\/code>&nbsp;\u51fd\u6570\u3002<\/p>\n\n\n\n<p>\u51fd\u6570&nbsp;<code>tree.New(k)<\/code>&nbsp;\u7528\u4e8e\u6784\u9020\u4e00\u4e2a\u968f\u673a\u7ed3\u6784\u7684\u5df2\u6392\u5e8f\u4e8c\u53c9\u67e5\u627e\u6811\uff0c\u5b83\u4fdd\u5b58\u4e86\u503c&nbsp;<code>k<\/code>,&nbsp;<code>2k<\/code>,&nbsp;<code>3k<\/code>, &#8230;,&nbsp;<code>10k<\/code>\u3002<\/p>\n\n\n\n<p>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u4fe1\u9053&nbsp;<code>ch<\/code>&nbsp;\u5e76\u4e14\u5bf9\u5176\u8fdb\u884c\u6b65\u8fdb\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>go Walk(tree.New(1), ch)<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u4ece\u4fe1\u9053\u4e2d\u8bfb\u53d6\u5e76\u6253\u5370 10 \u4e2a\u503c\u3002\u5e94\u5f53\u662f\u6570\u5b57&nbsp;<code>1, 2, 3, ..., 10<\/code>\u3002<\/p>\n\n\n\n<p><strong>3.<\/strong>&nbsp;\u7528&nbsp;<code>Walk<\/code>&nbsp;\u5b9e\u73b0&nbsp;<code>Same<\/code>&nbsp;\u51fd\u6570\u6765\u68c0\u6d4b&nbsp;<code>t1<\/code>&nbsp;\u548c&nbsp;<code>t2<\/code>&nbsp;\u662f\u5426\u5b58\u50a8\u4e86\u76f8\u540c\u7684\u503c\u3002<\/p>\n\n\n\n<p><strong>4.<\/strong>&nbsp;\u6d4b\u8bd5&nbsp;<code>Same<\/code>&nbsp;\u51fd\u6570\u3002<\/p>\n\n\n\n<p><code>Same(tree.New(1), tree.New(1))<\/code>&nbsp;\u5e94\u5f53\u8fd4\u56de&nbsp;<code>true<\/code>\uff0c\u800c&nbsp;<code>Same(tree.New(1), tree.New(2))<\/code>&nbsp;\u5e94\u5f53\u8fd4\u56de&nbsp;<code>false<\/code>\u3002<\/p>\n\n\n\n<p><code>Tree<\/code>&nbsp;\u7684\u6587\u6863\u53ef\u5728<a href=\"https:\/\/godoc.org\/golang.org\/x\/tour\/tree#Tree\" target=\"_blank\" rel=\"noreferrer noopener\">\u8fd9\u91cc<\/a>\u627e\u5230\u3002<\/p>\n<\/blockquote>\n\n\n\n<p>\u539f\u9898\u4ee3\u7801\u6a21\u677f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"golang.org\/x\/tour\/tree\"\n\n\/\/ Walk \u6b65\u8fdb tree t \u5c06\u6240\u6709\u7684\u503c\u4ece tree \u53d1\u9001\u5230 channel ch\u3002\nfunc Walk(t *tree.Tree, ch chan int)\n\n\/\/ Same \u68c0\u6d4b\u6811 t1 \u548c t2 \u662f\u5426\u542b\u6709\u76f8\u540c\u7684\u503c\u3002\nfunc Same(t1, t2 *tree.Tree) bool\n\nfunc main() {\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e2a<code>Same<\/code>\u5b9e\u73b0\u7684\u601d\u60f3\u662f\u521b\u5efa\u4e24\u4e2a\u4fe1\u9053\uff0c\u901a\u8fc7<code>Walk<\/code>\u51fd\u6570\u5206\u522b\u904d\u5386\u4e24\u68f5\u6811\uff0c\u5e76\u628a\u7ed3\u679c\u5206\u522b\u4f20\u5165\u4e24\u4e2a\u4fe1\u9053\uff0c\u7136\u540e\u6bd4\u8f83\u4e24\u4e2a\u4fe1\u9053\u7684\u7ed3\u679c\u662f\u5426\u76f8\u540c\u3002\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n\t\"fmt\"\n\t\"golang.org\/x\/tour\/tree\"\n)\n\n\/\/ Walk \u6b65\u8fdb tree t \u5c06\u6240\u6709\u7684\u503c\u4ece tree \u53d1\u9001\u5230 channel ch\u3002\nfunc Walk(t *tree.Tree, ch chan int){\n\tif t == nil {\n\t\treturn\n\t}\n\t\n\tWalk(t.Left, ch)\n\tch &lt;- t.Value\n\tWalk(t.Right, ch)\n}\n\n\/\/ Same \u68c0\u6d4b\u6811 t1 \u548c t2 \u662f\u5426\u542b\u6709\u76f8\u540c\u7684\u503c\u3002\nfunc Same(t1, t2 *tree.Tree) bool{\n\tch1, ch2 := make(chan int), make(chan int)\n\tgo Walk(t1, ch1)\n\tgo Walk(t2, ch2)\n\tfor i := 0; i &lt; 10; i++{\n\t\tx, y := &lt;-ch1, &lt;-ch2\n\t\tif x != y {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\nfunc main() {\n\tch := make(chan int)\n\n\t\/\/\u4ece\u4fe1\u9053\u4e2d\u6253\u537010\u4e2a\u503c\n\tgo Walk(tree.New(1), ch)\n\tfor i := 0; i &lt; 10; i++ {\n\t\tfmt.Println(&lt;-ch)\n\t}\n\t\n\tfmt.Println(Same(tree.New(1), tree.New(1)))\n\tfmt.Println(Same(tree.New(1), tree.New(2)))\n}<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2\n3\n4\n5\n6\n7\n8\n9\n10\ntrue\nfalse<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u4e2a\u662fA Tour of Go\u8fd9\u4e2a\u6559\u7a0b\u91cc\u9762\u7684\u7b2c\u5341\u4e2a\u7ec3\u4e60\uff0c\u539f\u9898\u5982\u4e0b\uff1a \u539f\u9898\u4ee3\u7801\u6a21\u677f\u5982\u4e0b\uff1a \u8fd9\u4e2aSame\u5b9e\u73b0\u7684\u601d\u60f3\u662f\u521b\u5efa\u4e24\u4e2a\u4fe1\u9053\uff0c\u901a\u8fc7Walk\u51fd\u6570\u5206\u522b\u904d\u5386\u4e24\u68f5\u6811\uff0c\u5e76\u628a\u7ed3\u679c\u5206\u522b\u4f20\u5165\u4e24\u4e2a\u4fe1\u9053\uff0c\u7136\u540e\u6bd4\u8f83\u4e24\u4e2a\u4fe1\u9053\u7684\u7ed3\u679c\u662f\u5426\u76f8\u540c\u3002\u5b9e\u73b0\u5982\u4e0b\uff1a \u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[374],"class_list":["post-1250","post","type-post","status-publish","format-standard","hentry","category-coding","tag-go"],"views":362,"_links":{"self":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/1250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/comments?post=1250"}],"version-history":[{"count":0,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/1250\/revisions"}],"wp:attachment":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/media?parent=1250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/categories?post=1250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/tags?post=1250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}