{"id":1245,"date":"2022-12-01T09:43:03","date_gmt":"2022-12-01T01:43:03","guid":{"rendered":"https:\/\/fdream.net\/blog\/?p=1245"},"modified":"2022-12-05T11:25:56","modified_gmt":"2022-12-05T03:25:56","slug":"go%e6%8c%87%e5%8d%97%e7%bb%83%e4%b9%a08-rot13reader","status":"publish","type":"post","link":"https:\/\/fdream.net\/blog\/article\/1245","title":{"rendered":"Go\u6307\u5357\u7ec3\u4e608-rot13Reader"},"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\u516b\u4e2a\u7ec3\u4e60\uff0c<a rel=\"noreferrer noopener\" href=\"https:\/\/tour.go-zh.org\/methods\/23\" target=\"_blank\">\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>\u6709\u79cd\u5e38\u89c1\u7684\u6a21\u5f0f\u662f\u4e00\u4e2a&nbsp;<a href=\"https:\/\/go-zh.org\/pkg\/io\/#Reader\" target=\"_blank\" rel=\"noreferrer noopener\"><code>io.Reader<\/code><\/a>&nbsp;\u5305\u88c5\u53e6\u4e00\u4e2a&nbsp;<code>io.Reader<\/code>\uff0c\u7136\u540e\u901a\u8fc7\u67d0\u79cd\u65b9\u5f0f\u4fee\u6539\u5176\u6570\u636e\u6d41\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c<a href=\"https:\/\/go-zh.org\/pkg\/compress\/gzip\/#NewReader\" target=\"_blank\" rel=\"noreferrer noopener\"><code>gzip.NewReader<\/code><\/a>&nbsp;\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a&nbsp;<code>io.Reader<\/code>\uff08\u5df2\u538b\u7f29\u7684\u6570\u636e\u6d41\uff09\u5e76\u8fd4\u56de\u4e00\u4e2a\u540c\u6837\u5b9e\u73b0\u4e86&nbsp;<code>io.Reader<\/code>&nbsp;\u7684&nbsp;<code>*gzip.Reader<\/code>\uff08\u89e3\u538b\u540e\u7684\u6570\u636e\u6d41\uff09\u3002<\/p>\n\n\n\n<p>\u7f16\u5199\u4e00\u4e2a\u5b9e\u73b0\u4e86&nbsp;<code>io.Reader<\/code>&nbsp;\u5e76\u4ece\u53e6\u4e00\u4e2a&nbsp;<code>io.Reader<\/code>&nbsp;\u4e2d\u8bfb\u53d6\u6570\u636e\u7684&nbsp;<code>rot13Reader<\/code>\uff0c\u901a\u8fc7\u5e94\u7528&nbsp;<a href=\"http:\/\/en.wikipedia.org\/wiki\/ROT13\" target=\"_blank\" rel=\"noreferrer noopener\">rot13<\/a>&nbsp;\u4ee3\u6362\u5bc6\u7801\u5bf9\u6570\u636e\u6d41\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n\n\n\n<p><code>rot13Reader<\/code>&nbsp;\u7c7b\u578b\u5df2\u7ecf\u63d0\u4f9b\u3002\u5b9e\u73b0&nbsp;<code>Read<\/code>&nbsp;\u65b9\u6cd5\u4ee5\u6ee1\u8db3&nbsp;<code>io.Reader<\/code>\u3002<\/p>\n<\/blockquote>\n\n\n\n<p>\u539f\u9898\u6a21\u677f\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n\t\"io\"\n\t\"os\"\n\t\"strings\"\n)\n\ntype rot13Reader struct {\n\tr io.Reader\n}\n\nfunc main() {\n\ts := strings.NewReader(\"Lbh penpxrq gur pbqr!\")\n\tr := rot13Reader{s}\n\tio.Copy(os.Stdout, &amp;r)\n}<\/code><\/pre>\n\n\n\n<p>ROT13\u8f6c\u6362\u6bd4\u8f83\u7b80\u5355\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><a href=\"https:\/\/fdream.net\/blog\/wp-content\/uploads\/2022\/12\/image.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/fdream.net\/blog\/wp-content\/uploads\/2022\/12\/image.png\" alt=\"\" class=\"wp-image-1246\" width=\"380\" height=\"220\" srcset=\"https:\/\/fdream.net\/blog\/wp-content\/uploads\/2022\/12\/image.png 320w, https:\/\/fdream.net\/blog\/wp-content\/uploads\/2022\/12\/image-300x173.png 300w\" sizes=\"auto, (max-width: 380px) 100vw, 380px\" \/><\/a><\/figure>\n\n\n\n<p>\u5b9e\u73b0\u8d77\u6765\u5c31\u662f\u8981\u8986\u76d6\u4e00\u4e0b\u539f\u6765\u7684Read\u65b9\u6cd5\uff0c\u5728Read\u65b9\u6cd5\u4e2d\u5bf9\u6bcf\u4e2a\u5b57\u7b26\u8fdb\u884c\u66ff\u6362\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n\t\"io\"\n\t\"os\"\n\t\"strings\"\n)\n\ntype rot13Reader struct {\n\tr io.Reader\n}\n\n\/\/\u5b57\u6bcd\u8f6c\u6362\nfunc rot13(out byte) byte{\n\tswitch{\n\t\tcase out &gt;= 'A' &amp;&amp; out &lt;= 'M' || out &gt;= 'a' &amp;&amp; out &lt;= 'm':\n\t\t\tout += 13\n\t\tcase out &gt;= 'N' &amp;&amp; out &lt;= 'Z' || out &gt;= 'n' &amp;&amp; out &lt;= 'z':\n\t\t\tout -= 13\n\t}\n\treturn out\n}\n\n\/\/\u91cd\u5199Read\u65b9\u6cd5\nfunc (fz rot13Reader) Read(b &#91;]byte) (int,error){\n\tn, e := fz.r.Read(b)\n\tfor i := 0; i &lt; n; i++ {\n\t\tb&#91;i] = rot13(b&#91;i])\n\t}\n\treturn n,e\n}\n\nfunc main() {\n\ts := strings.NewReader(\"Lbh penpxrq gur pbqr!\")\n\tr := rot13Reader{s}\n\tio.Copy(os.Stdout, &amp;r)\n}<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\u7ed3\u679c\uff08\u8f6c\u6362\u540e\u7684\u5185\u5bb9\uff09\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>You cracked the code!<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u4e2a\u662fA Tour of Go\u8fd9\u4e2a\u6559\u7a0b\u91cc\u9762\u7684\u7b2c\u516b\u4e2a\u7ec3\u4e60\uff0c\u539f\u9898\u5982\u4e0b\uff1a \u6709\u79cd\u5e38\u89c1\u7684\u6a21\u5f0f\u662f\u4e00\u4e2a&nbsp;io.Reader&nbsp;\u5305\u88c5\u53e6\u4e00\u4e2a&nbsp;io.Reader\uff0c\u7136\u540e\u901a\u8fc7\u67d0\u79cd\u65b9\u5f0f\u4fee\u6539\u5176\u6570\u636e\u6d41\u3002 \u4f8b\u5982\uff0cgzip.NewReader&nbsp;\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a&nbsp;io.Reader\uff08\u5df2\u538b\u7f29\u7684\u6570\u636e\u6d41\uff09\u5e76\u8fd4\u56de\u4e00\u4e2a\u540c\u6837\u5b9e\u73b0\u4e86&nbsp;io.Reader&nbsp;\u7684&nbsp;*gzip.Reader\uff08\u89e3\u538b\u540e\u7684\u6570\u636e\u6d41\uff09\u3002 \u7f16\u5199\u4e00\u4e2a\u5b9e\u73b0\u4e86&nbsp;io.Reader&nbsp;\u5e76\u4ece\u53e6\u4e00\u4e2a&nbsp;io.Reader&nbsp;\u4e2d\u8bfb\u53d6\u6570\u636e\u7684&nbsp;rot13Reader\uff0c\u901a\u8fc7\u5e94\u7528&nbsp;rot13&nbsp;\u4ee3\u6362\u5bc6\u7801\u5bf9\u6570\u636e\u6d41\u8fdb\u884c\u4fee\u6539\u3002 rot13Reader&nbsp;\u7c7b\u578b\u5df2\u7ecf\u63d0\u4f9b\u3002\u5b9e\u73b0&nbsp;Read&nbsp;\u65b9\u6cd5\u4ee5\u6ee1\u8db3&nbsp;io.Reader\u3002 \u539f\u9898\u6a21\u677f\u4ee3\u7801\u5982\u4e0b\uff1a ROT13\u8f6c\u6362\u6bd4\u8f83\u7b80\u5355\uff0c\u5982\u56fe\uff1a \u5b9e\u73b0\u8d77\u6765\u5c31\u662f\u8981\u8986\u76d6\u4e00\u4e0b\u539f\u6765\u7684Read\u65b9\u6cd5\uff0c\u5728Read\u65b9\u6cd5\u4e2d\u5bf9\u6bcf\u4e2a\u5b57\u7b26\u8fdb\u884c\u66ff\u6362\uff1a \u8f93\u51fa\u7ed3\u679c\uff08\u8f6c\u6362\u540e\u7684\u5185\u5bb9\uff09\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-1245","post","type-post","status-publish","format-standard","hentry","category-coding","tag-go"],"views":310,"_links":{"self":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/1245","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=1245"}],"version-history":[{"count":0,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/1245\/revisions"}],"wp:attachment":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/media?parent=1245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/categories?post=1245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/tags?post=1245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}