{"id":7921,"date":"2023-01-28T13:43:56","date_gmt":"2023-01-28T05:43:56","guid":{"rendered":"https:\/\/egonlin.com\/?p=7921"},"modified":"2023-01-28T13:43:56","modified_gmt":"2023-01-28T05:43:56","slug":"15-%e5%9f%ba%e6%9c%ac%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e4%b9%8b%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=7921","title":{"rendered":"15 \u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e4b\u5b57\u7b26\u4e32"},"content":{"rendered":"<h1>\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e4b\u5b57\u7b26\u4e32<\/h1>\n<h2>\u4e00 \u5b57\u7b26\u4e32\u5e95\u5c42\u662f\u4ec0\u4e48<\/h2>\n<p>\u200b   \u5728 C\/C++\u8bed\u8a00\u4e2d\uff0c\u5e76\u4e0d\u5b58\u5728\u539f\u751f\u7684\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u5b83\u4eec\u901a\u5e38\u4f7f\u7528\u5b57\u7b26\u6570\u7ec4\u6765\u8868\u793a\u51fa\u5b57\u7b26\u4e32\u7684\u6982\u5ff5\uff0c\u5e76\u4ee5\u5b57\u7b26\u6307\u9488\u6765\u4f20\u9012\u3002\u800c\u5728Go\u8bed\u8a00\u4e2d\uff0c\u5b57\u7b26\u4e32\u4e5f\u662f\u4e00\u79cd\u57fa\u672c\u7c7b\u578b\uff0c\u5c31\u50cf\u5176\u4ed6\u6570\u636e\u7c7b\u578b\uff08int\u3001bool\u3001float32\u3001float64\u7b49\uff09\u4e00\u6837\u3002<\/p>\n<p>\u200b   Go\u8bed\u8a00\u4e2d\uff0c\u5b57\u7b26\u4e32\u5e95\u5c42\u662f\u4e00\u4e2a\u5207\u7247\uff0c\u8be5\u5207\u7247\u6307\u5411\u4e00\u4e2a\u5e95\u5c42\u6570\u7ec4\uff0c\u6570\u7ec4\u5185\u5143\u7d20\u662f\u4e00\u4e2a\u4e2a\u5b57\u8282(byte)<\/p>\n<p>\u200b   \u518d\u6b21\u5f3a\u8c03\uff1a\u5b57\u7b26\u4e32\u5e95\u5c42\u662f\u4e00\u4e2a\u5207\u7247\u3001\u5207\u7247\u3001\u5207\u7247\uff01\uff01\uff01<\/p>\n<p>\u200b   go\u5723\u7ecf\u4e86\u91cc\u660e\u786e\u8bf4\u9053\uff1a\u5b57\u7b26\u4e32\u4e0e[]byte\u7684\u7ed3\u6784\u76f8\u540c\uff0c\u8fd9\u4e2a\u7275\u626f\u5230\u6570\u636e\u7c7b\u578b\u8f6c\u6362\uff0c\u52a1\u5fc5\u8981\u641e\u6e05\u695a\uff01\u4e0d\u8981\u88ab\u4e00\u4e9b\u6587\u7ae0\u8bef\u5bfc\u3002<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='.\/img\/strings.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\".\/img\/strings.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"goland\u4f7f\u75283\" \/><\/div><\/p>\n<h2>\u4e8c \u58f0\u660e\u4e0e\u521d\u59cb\u5316<\/h2>\n<p>\u58f0\u660e\u8bed\u6cd5<\/p>\n<pre><code class=\"language-go\">var \u53d8\u91cf\u540d string = \u521d\u59cb\u5316\u8868\u8fbe\u5f0f\/\u5b57\u9762\u91cf<\/code><\/pre>\n<p>\u5b57\u7b26\u4e32\u5e95\u5c42\u5c31\u662f\u4e00\u4e2abyte\u7ec4\u6210\u7684\u5207\u7247\uff0c\u5207\u7247\u6307\u5411\u4e00\u4e2a\u5e95\u5c42\u6570\u7ec4\uff0c\u6570\u7ec4\u5185\u5b58\u653e\u7684\u662f\u4e00\u4e2a\u4e2abyte\uff0cbyte\u987e\u540d\u601d\u4e49\u5c31\u662f\u5b57\u8282\uff0c\u662fstring\u7c7b\u578b\u5e95\u5c42\u7684\u5143\u7d20\u7c7b\u578b\uff0c\u6e90\u7801\u91cctype byte = uint8\uff0c\u5373byte\u4e0euint8\u662f\u7b49\u4ef7\u7c7b\u578b\u3002<\/p>\n<p>\u5b57\u7b26\u4e32\u5e95\u5c42\u6570\u7ec4\u7684byte\u4ece\u4f55\u800c\u6765\u5462\uff1f\u6709\u5982\u4e0b\u4e24\u79cd<\/p>\n<ul>\n<li>1\u3001\u5f53\u5b57\u7b26\u4e32\u9762\u503c\u4e3a\u6587\u672c\u5b57\u7b26\u65f6\uff0cgo\u4f1a\u5c06\u7528utf8\u683c\u5f0f\u5c06\u5176\u7f16\u7801\u6210\u4e00\u4e2a\u4e2a\u5b57\u8282\uff0c\u7136\u540e\u5b58\u5165\u5e95\u5c42\u6570\u7ec4\uff1b<\/li>\n<\/ul>\n<pre><code class=\"language-go\">var name string = &quot;egon\u6797&quot;\nfmt.Println(len(name))  \/\/ 7\u4e2a\u5b57\u8282\nfmt.Println(name)       \/\/ egon\u6797<\/code><\/pre>\n<ul>\n<li>2\u3001\u5f53\u5b57\u7b26\u4e32\u9762\u503c\u4e3a\u73b0\u6210\u7684\u5b57\u8282\u6570\u5b57\u65f6\uff0cgo\u5c31\u7701\u4e8b\u4e86\uff0c\u76f4\u63a5\u5b58\u5165\u5e95\u5c42\u6570\u7ec4\u5c31\u597d<\/li>\n<\/ul>\n<pre><code class=\"language-go\">s2:=&quot;\\xe5\\xb3\\xb0&quot;    \/\/ \u5b57\u7b26\u4e32\u5185\u5305\u542b\u7684\u53ef\u4ee5\u76f4\u63a5\u5c31\u662f\u73b0\u6210\u7684\u4e00\u4e2a\u4e2abyte\uff0c\u5e76\u4e14\u6bcf\u4e00\u4e2abyte\u90fd\u53ef\u4ee5\u662f\u4efb\u610fbyte\nfmt.Println(len(s2))  \/\/ 3\u4e2a\u5b57\u8282\nfmt.Println(s2)       \/\/ \u5cf0<\/code><\/pre>\n<p>\u200b   \u4e5f\u5c31\u662f\u8bf4\uff1a\u5b57\u7b26\u4e32\u5e95\u5c42\u6570\u7ec4\u5185\u5b58\u653e\u7684\u5b57\u8282byte\u53ef\u4ee5\u662f\u4efb\u610f\u5b57\u8282\uff0c\u5373\u53ea\u8981\u662fbyte\u5c31\u884c\u3002<\/p>\n<p>\u200b   \u4f46\u662f\u5728\u8bfb\u53d6\u5b57\u7b26\u4e32\u65f6\uff0c\u4f1a\u4ece\u5e95\u5c42\u6570\u7ec4\u4e2d\u53d6\u51fabyte\u7136\u540e\u6309\u7167utf-8\u4e2a\u683c\u5f0f\u8f6c\u6362\u6210\u5b57\u7b26\uff0c\u6240\u4ee5\u5982\u679c\u5f53\u521d\u5b58\u7684\u65f6\u5019\u65e0\u6cd5\u65e0\u5929\uff0c\u6253\u5370\u5b57\u7b26\u4e32\u7684\u65f6\u5019\u4fbf\u6709\u53ef\u80fd\u4f1a\u51fa\u73b0\u4e71\u7801\u3002<\/p>\n<p>\u200b   \u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u6765\u8be6\u7ec6\u770b\u4e00\u4e0bstring\u5b57\u9762\u91cf\uff0c\u65f6\u523b\u7262\u8bb0\uff0c\u65e0\u8bba\u662f\u5565\uff0c\u90fd\u4f1a\u89e3\u6790\u6210byte\u5b58\u5165\u5e95\u5c42\u6570\u7ec4\uff0c\u6253\u5370\u65f6\u4f1a\u53d6\u51fa\u6765\u6309\u7167utf8\u683c\u5f0f\u8fdb\u884c\u89e3\u7801\u3002<\/p>\n<h3>2.1 \u5b57\u9762\u91cf\u53ef\u4ee5\u662f\u4efb\u610f\u5b57\u7b26<\/h3>\n<p>\u5b57\u7b26\u4e32\u7684\u503c\u9700\u8981\u7528\u53cc\u5f15\u53f7\u5305\u542b\uff08\u6ce8\u610f\u4e00\u5b9a\u8981\u662f\u53cc\u5f15\u53f7\uff09\uff0c\u53cc\u5f15\u53f7\u5185\u901a\u5e38\u662f\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff0c\u5373\u4eba\u7c7b\u4ece\u5b57\u9762\u4e0a\u5c31\u53ef\u4ee5\u8bfb\u61c2\u7684\u6587\u672c\u5b57\u7b26\uff0c\u6bd4\u5982<\/p>\n<pre><code class=\"language-go\">var name string = &quot;egon\u6797&quot;<\/code><\/pre>\n<p>\u5b57\u7b26\u4e32\u7684\u5b57\u9762\u503c\u4e3a\u6587\u672c\u5b57\u7b26\u65f6\uff0cgo\u9ed8\u8ba4\u91c7\u7528utf8\u7f16\u7801\u5c06\u5176\u7f16\u7801\u6210\u4e00\u4e2a\u4e2abyte\uff0c\u7136\u540e\u5b58\u5165\u5e95\u5c42\u6570\u7ec4\u30021\u4e2a\u82f1\u6587\u5b57\u7b26\u5360\u4e00\u4e2abyte\uff0c1\u4e2a\u4e2d\u6587\u5b57\u7b26\u53603\u4e2abyte\uff0c\u4e0a\u8ff0\u58f0\u660e\u8bed\u53e5\u5e95\u5c42\u76f8\u5f53\u4e8e\uff1a<\/p>\n<pre><code class=\"language-go\">var name = []byte{101,103,111,110,230,158,151}<\/code><\/pre>\n<p>\u6253\u5370\u65f6\uff0c\u540c\u6837\u4f1a\u91c7\u7528utf8\u7f16\u7801\u5c06\u5176\u89e3\u7801<\/p>\n<pre><code class=\"language-go\">fmt.Println(name)  \/\/ \u53d6\u51fa[]byte\u91cc\u7684\u5185\u5bb9\u6309\u7167utf8\u683c\u5f0f\u8fdb\u884c\u89e3\u7801\uff0c\u8f93\u51fa&quot;egon\u6797&quot;<\/code><\/pre>\n<p>\u5b57\u9762\u91cf\u4e2d\u4e5f\u53ef\u4ee5\u5f15\u5165\u8f6c\u4e49\u7b26\u53f7<\/p>\n<p>\u5728\u4e00\u4e2a\u53cc\u5f15\u53f7\u5305\u542b\u7684\u5b57\u7b26\u4e32\u9762\u503c\u4e2d\uff0c\u53ef\u4ee5\u7528\u4ee5\u53cd\u659c\u6760<code>\\<\/code>\u5f00\u5934\u7684\u8f6c\u4e49\u5e8f\u5217\u63d2\u5165\u4efb\u610f\u7684\u6570\u636e\u3002\u4e0b\u9762\u7684\u6362\u884c\u3001\u56de\u8f66\u548c\u5236\u8868\u7b26\u7b49\u662f\u5e38\u89c1\u7684ASCII\u63a7\u5236\u4ee3\u7801\u7684\u8f6c\u4e49\u65b9\u5f0f\uff1a<\/p>\n<pre><code class=\"language-go\">\\n      \u6362\u884c\n\\r      \u56de\u8f66\n\\t      \u5236\u8868\u7b26\n\\&quot;      \u53cc\u5f15\u53f7 \n\\\\      \u53cd\u659c\u6760\n\/\/ \u4f8b\u5982\nfmt.Println(&quot;file_path := \\&quot;d:\\\\a\\\\b\\\\c.txt\\&quot;&quot;) \/\/ \u8f93\u51fa\uff1afile_path := &quot;d:\\a\\b\\c.txt&quot;\n<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a<\/p>\n<ul>\n<li>1\u3001\u5b57\u7b26\u4e32\u662f\u5305\u542b\u5728\u53cc\u5f15\u53f7\u5185\u7684\uff0c\u5982\u679c\u53cc\u5f15\u53f7\u7684\u5b57\u7b26\u4e32\u5185\u8fd8\u60f3\u518d\u5305\u542b\u53cc\u5f15\u53f7\uff0c\u90a3\u5c31\u9700\u8981\u8f6c\u4e49\u4e86\uff0c\u6b63\u5982\u4e0a\u4f8b\u6240\u793a\u3002\u4f46\u5982\u679c\u53cc\u5f15\u53f7\u5185\u5b58\u5728\u5355\u5f15\u53f7\uff0c\u65e0\u9700\u8f6c\u4e49\uff0c\u4f1a\u76f4\u63a5\u6253\u5370<\/li>\n<\/ul>\n<pre><code class=\"language-go\">fmt.Println(&quot;my name is &#039;egon&#039;&quot;) \/\/ my name is &#039;egon&#039;<\/code><\/pre>\n<ul>\n<li>\u5728go\u4e2d\uff0c\u5355\u5f15\u53f7\u5185\u5305\u542b\u7684\u662f\u5b57\u7b26rune\u7c7b\u578b\uff08\u540e\u7eed\u4f1a\u63d0\u5230\uff09\uff0c\u5982\u679c\u5728\u5b57\u7b26\u7684\u5355\u5f15\u53f7\u5185\u8fd8\u60f3\u518d\u5305\u542b\u5355\u5f15\u53f7\u90a3\u4e5f\u9700\u8981\u8f6c\u4e49<\/li>\n<\/ul>\n<pre><code class=\"language-go\">fmt.Println(&#039;\\&#039;&#039;) \/\/ \u5355\u5f15\u53f7\u5185\u8868\u793a\u7684\u662f\u5b57\u7b26\u7c7b\u578b\uff0c\u53ea\u80fd\u5305\u542b\u4e00\u4e2a\u5b57\u7b26\uff0c\u5982\u679c\u8be5\u5b57\u7b26\u662f\u5355\u5f15\u53f7\uff0c\u90a3\u4e48\u5fc5\u987b\u8f6c\u4e49<\/code><\/pre>\n<p><strong>\u53cd\u5f15\u53f7&#8220;<\/strong><\/p>\n<p>\u4f7f\u7528\u53cd\u5f15\u53f7\u4ee3\u66ff\u53cc\u5f15\u53f7\u4ee3\u8868\u662f\u539f\u751f\u5b57\u7b26\uff0c\u539f\u751f\u5b57\u7b26\u5185\u6ca1\u6709\u8f6c\u4e49\u64cd\u4f5c\uff0c\u5168\u90e8\u7684\u5185\u5bb9\u90fd\u662f\u5b57\u9762\u7684\u610f\u601d\uff0c\u4f1a\u539f\u6837\u8f93\u51fa\u5b57\u7b26\u4e0e\u6362\u884c\u7b49<\/p>\n<pre><code class=\"language-go\">s:=`\n\u5f53\u4f60\u8ba4\u6e05\u81ea\u5df1\u7684\u65f6\u5019\n\u4fbf\\n\\t\\n\\n\u4e0d\u4f1a\u518d\u5c0f\u77a7\u4ed6\u4eba\n\\u4e0a\n`\nfmt.Println(s) \/\/ \u8f93\u51fa\u5982\u4e0b\u5185\u5bb9\n\n\u5f53\u4f60\u8ba4\u6e05\u81ea\u5df1\u7684\u65f6\u5019\n\u4fbf\\n\\t\\n\\n\u4e0d\u4f1a\u518d\u5c0f\u77a7\u4ed6\u4eba\n\\u4e0a\n<\/code><\/pre>\n<p>\u539f\u751f\u5b57\u7b26\u4e32\u9762\u503c\u7528\u4e8e\u7f16\u5199\u6b63\u5219\u8868\u8fbe\u5f0f\u4f1a\u5f88\u65b9\u4fbf\uff0c\u56e0\u4e3a\u6b63\u5219\u8868\u8fbe\u5f0f\u5f80\u5f80\u4f1a\u5305\u542b\u5f88\u591a\u53cd\u659c\u6760\u3002\u539f\u751f\u5b57\u7b26\u4e32\u9762\u503c\u540c\u65f6\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8eHTML\u6a21\u677f\u3001JSON\u9762\u503c\u3001\u547d\u4ee4\u884c\u63d0\u793a\u4fe1\u606f\u4ee5\u53ca\u90a3\u4e9b\u9700\u8981\u6269\u5c55\u5230\u591a\u884c\u7684\u573a\u666f\u3002<\/p>\n<h3>2.3 \u5b57\u9762\u91cf\u53ef\u4ee5\u662f\u5bf9\u5e94\u7684\u5b57\u8282\u6570\u5b57<\/h3>\n<p>\u53cc\u5f15\u53f7\u5185\u4e5f\u53ef\u4ee5\u662f\u76f4\u63a5\u5199\u597d\u7684\u5b57\u8282\u6570\u5b57\uff0cgo\u4f1a\u5c06\u5176\u76f4\u63a5\u5b58\u5165\u5e95\u5c42\u6570\u7ec4\u3002<\/p>\n<p>\u5207\u8bb0\uff1a\u5b58\u7684\u65f6\u5019\u4e71\u5b58\uff0c\u5f53\u7136\u4e0d\u4f1a\u62a5\u9519\uff0c\u4f46\u662f\u5728\u6253\u5370\u5b57\u7b26\u4e32\u65f6\u4f1a\u4ece\u5e95\u5c42\u6570\u7ec4\u53d6\u51fabyte\u7136\u540e\u6309\u7167utf-8\u683c\u5f0f\u89e3\u7801\u6210\u5b57\u7b26\u5c31\u4f1a\u4e71\u7801\u4e86\u3002<\/p>\n<pre><code class=\"language-go\">\/\/ 0\u3001\u5148\u50a8\u5907\u4e00\u4e2a\u77e5\u8bc6\u70b9\n\u4e00\u4e2a\u5b57\u8282=8\u4e2abit\u4f4d\n    \u5bf9\u5e94\u5341\u8fdb\u5236\u6700\u5927\u4e3a255\n    \u5bf9\u5e94\u7684\u5341\u516d\u8fdb\u5236\u6700\u5927\u4e3a0xff\n    \u5bf9\u5e94\u7684\u516b\u8fdb\u5236\u6700\u5927\u4e3a\\377(go\u4e2d\\\u5f00\u5934\u4ee3\u8868\u516b\u8fdb\u5236\uff0c\u4e0d\u8981\u5199\u6210\\o377)\n\n\/\/ 1\u3001\u53cc\u5f15\u53f7\u5185\u662f\u516b\u8fdb\u5236\u6570\u6784\u6210\u7684\u5b57\u7b26\u4e32\ns1:=&quot;\\110\\151&quot; \/\/ \u5e95\u5c42\u6570\u7ec4\u5b58\u653e\u7684\u7b2c\u4e00\u4e2a\u5b57\u8282\u662f\\110\uff0c\u7b2c\u4e8c\u4e2a\u5b57\u8282\u662f\\151\nfmt.Println(s1) \/\/ Hi\nfmt.Println(len(s1)) \/\/ 2\u4e2a\u5b57\u8282\n\n\/\/ 2\u3001\u53cc\u5f15\u53f7\u5185\u662f\u5341\u516d\u8fdb\u5236\u6570\u6784\u6210\u7684\u5b57\u8282\ns2:=&quot;\\xe5\\xb3\\xb0\\xe5\\x93\\xa5&quot; \/\/ \u5e95\u5c42\u6570\u7ec4\u5b58\u653e\u7684\u7b2c\u4e00\u4e2a\u5b57\u8282\u662f\\xe5\uff0c\u7b2c\u4e8c\u4e2a\u5b57\u8282\u662f\\xb3,\u4f9d\u6b21\u7c7b\u63a8...\nfmt.Println(s2) \/\/ \u5cf0\u54e5\nfmt.Println(len(s2)) \/\/ 6\u4e2a\u5b57\u8282\n\n\/\/ 3\u3001\u53cc\u5f15\u53f7\u5185\uff1a\\u\u6307\u7684\u662f\u9075\u5faaUCS-2\u6807\u51c6\u7684unicode\uff0c\\U\u6307\u7684\u662f\u9075\u5faaUCS-4\u6807\u7b7e\u7684unicode\ns3:=&quot;\\u771f\\U00005e05&quot; \/\/ \\u771f\u89e3\u7801\u6210utf-8\u7f16\u7801\u540e\u5bf9\u5e94\u4e09\u4e2a\u5b57\u8282\uff0c\u4f9d\u6b21\u5b58\u5165\u5e95\u5c42\u6570\u636e\uff0c\\U...\u4e00\u6837\nfmt.Println(s3) \/\/ \u771f\u5e05\nfmt.Println(len(s3)) \/\/ 6\u4e2a\u5b57\u8282\n\n\/\/ 4\u3001\u53cc\u5f15\u53f7\u5185\u662f\u5b57\u7b26\u4e32\u9762\u503c\ns4:=&quot;en\u8bf4\u7684\u5bf9&quot; \/\/ go\u4f1a\u5c06\u5b57\u7b26\u4e32\u9762\u503c\u7f16\u7801\u6210utf-8\u683c\u5f0f\uff0c\u7136\u540e\u5c06\u5f97\u5230\u7684byte\u4f9d\u6b21\u5b58\u5165\u5e95\u5c42\u6570\u7ec4\nfmt.Println(s4) \/\/ en\u8bf4\u7684\u5bf9\nfmt.Println(len(s4)) \/\/ 11\u4e2a\u5b57\u8282\n\n\/\/ \u53cc\u5f15\u53f7\u5185\u5b8c\u5168\u53ef\u4ee5\u540c\u65f6\u5bb9\u7eb3\u4e0a1\u30012\u30013\u30014\u6240\u8ff0\u7684\u5185\u5bb9\uff0c\u751a\u81f3\u987a\u5e8f\u90fd\u65e0\u6240\u8c13\uff0c\u56e0\u4e3a\u5b57\u7b26\u4e32\u5e95\u5c42\u5207\u7247\u6307\u5411\u7684\u5c31\u662f\u4e00\u4e2a\u5b57\u8282\u5e8f\u5217\u7684\u6570\u7ec4\uff0c\u81f3\u4e8e\u6570\u7ec4\u5185\u5b58\u7684\u662f\u4ec0\u4e48\u5b57\u8282byte\u662f\u65e0\u6240\u8c13\u7684\uff0c\u4f46\u95ee\u9898\u51fa\u5c31\u51fa\u5728\uff0c\u5f53\u6211\u4eec\u6253\u5370\u5b57\u7b26\u4e32\u7684\u65f6\u5019\uff0cgo\u4f1a\u5c06\u5b57\u7b26\u4e32\u5e95\u5c42\u6570\u7ec4\u4e2d\u7684byte\u53d6\u51fa\u6765\uff0c\u7136\u540e\u6309\u7167utf-8\u4e2a\u7684\u683c\u5f0f\u89e3\u7801\u6210\u5b57\u7b26\uff0c\u5982\u679cbyte\u975e\u6cd5\uff0c\u90a3\u4e48\u89e3\u7801\u51fa\u7684\u5b57\u7b26\u662f\u4e00\u4e2a\u9ed1\u8272\u516d\u89d2\u6216\u94bb\u77f3\u5f62\u72b6\uff0c\u91cc\u9762\u5305\u542b\u4e00\u4e2a\u767d\u8272\u7684\u95ee\u53f7\uff08?\uff09\ns5:=&quot;\\372\\xee\\xe5\\xb3\\xb0\\xe5\\x93\\xa5&quot; \/\/ \u524d2\u4e2a\u5b57\u8282\u90fd\u662f\u975e\u6cd5\u7684\uff0c\u540e6\u4e2a\u5408\u6cd5\nfmt.Println(s5) \/\/ \ufffd\ufffd\u5cf0\u54e5\nfmt.Println(len(s5)) \/\/ 8\u4e2a\u5b57\u8282<\/code><\/pre>\n<p>ps\uff1a\u6709\u4e00\u4e9b\u5b57\u7b26\u4e32\u9762\u503c\u7684\u5b57\u7b26\u6bd4\u8f83\u7279\u6b8a\uff0c\u6211\u4eec\u7528\u7684\u8f93\u5165\u6cd5\u5de5\u5177\u5f88\u96be\u8f93\u5165\uff0c\u6709\u4e00\u4e9b\u751a\u81f3\u662f\u4e0d\u53ef\u89c1\u7684\u5b57\u7b26\uff0c\u6b64\u65f6\u53ef\u4ee5\u5728Go\u8bed\u8a00\u7684\u5b57\u7b26\u4e32\u9762\u503c\u4e2d\uff0c\u901a\u8fc7\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\u52a0\u4e0aUnicode\u7801\u70b9\u7684\u65b9\u5f0f\u6765\u8f93\u5165\u7279\u6b8a\u7684\u5b57\u7b26\uff08\u5982\u4e0a\u4f8b1\u30012\u30013\u30014\u6240\u793a\uff09\u3002unicode\u9075\u5faa\u4e24\u79cd\u6807\u51c6UCS-2\u548cUCS-4\uff0cUCS-2\u7528\u4e24\u4e2a\u5b57\u8282\u7f16\u7801\uff08\\u\u5bf9\u5e94UCS-2\uff09\uff0cUCS-4\u75284\u4e2a\u5b57\u8282\u7f16\u7801\uff08\\U\u5bf9\u5e94UCS-4\uff09\u3002\\uhhhh\u5bf9\u5e9416bit\u7684\u7801\u70b9\u503c\uff0c\\Uhhhhhhhh\u5bf9\u5e9432bit\u7684\u7801\u70b9\u503c\uff0c\u5176\u4e2dh\u662f\u4e00\u4e2a\u5341\u516d\u8fdb\u5236\u6570\u5b57\uff1b\u4e00\u822c\u5e38\u7528\u7684\u662f16bit\u7684\u5f62\u5f0f\uff0c\u5f88\u5c11\u9700\u8981\u4f7f\u752832bit\u7684\u5f62\u5f0f\uff0c\u8fd9\u6b63\u5982\u6211\u4eec\u5e38\u8bf4\u7684\u90a3\u6837\uff0cunicode\u901a\u5e38\u7528\u4e24\u4e2a\u5b57\u8282\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\u3002<\/p>\n<h2>\u4e09 \u5b57\u7b26\u7c7b\u578brune<\/h2>\n<p>\u200b   \u5728python\u4e2d\u53ea\u6709\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u5e76\u6ca1\u6709\u5b57\u7b26\u7c7b\u578b\uff0c\u800cgo\u8bed\u8a00\u5219\u4e0d\u540c\uff0c\u65e2\u6709string\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u8fd8\u6709\u4e00\u4e2arune\u5b57\u7b26\u7c7b\u578b\uff0c\u53ea\u80fd\u5b58\u653e\u5355\u4e2a\u5b57\u7b26\uff0c\u8981\u4e86\u89e3rune\u7c7b\u578b\uff0c\u6211\u4eec\u5148\u6765\u50a8\u5907\u4e00\u4e2a\u77e5\u8bc6<\/p>\n<h3>3.1 \u50a8\u5907\u77e5\u8bc6\uff1a\u4e86\u89e3unicode\u4e0eutf-8<\/h3>\n<pre><code class=\"language-go\">\/\/ ==========================&gt; unicode &lt;==========================\n\u5728\u5f88\u4e45\u4ee5\u524d\uff0c\u4e16\u754c\u8fd8\u662f\u6bd4\u8f83\u7b80\u5355\u7684\uff0c\u8d77\u7801\u8ba1\u7b97\u673a\u4e16\u754c\u5c31\u53ea\u6709\u4e00\u4e2aASCII\u5b57\u7b26\u96c6\uff1a\u7f8e\u56fd\u4fe1\u606f\u4ea4\u6362\u6807\u51c6\u4ee3\u7801\u3002ASCII\uff0c\u66f4\u51c6\u786e\u5730\u8bf4\u662f\u7f8e\u56fd\u7684ASCII\uff0c\u4f7f\u75287bit\u6765\u8868\u793a128\u4e2a\u5b57\u7b26\uff1a\u5305\u542b\u82f1\u6587\u5b57\u6bcd\u7684\u5927\u5c0f\u5199\u3001\u6570\u5b57\u3001\u5404\u79cd\u6807\u70b9\u7b26\u53f7\u548c\u8bbe\u7f6e\u63a7\u5236\u7b26\u3002\u5bf9\u4e8e\u65e9\u671f\u7684\u8ba1\u7b97\u673a\u7a0b\u5e8f\u6765\u8bf4\uff0c\u8fd9\u4e9b\u5c31\u8db3\u591f\u4e86\uff0c\u4f46\u662f\u8fd9\u4e5f\u5bfc\u81f4\u4e86\u4e16\u754c\u4e0a\u5f88\u591a\u5176\u4ed6\u5730\u533a\u7684\u7528\u6237\u65e0\u6cd5\u76f4\u63a5\u4f7f\u7528\u81ea\u5df1\u7684\u7b26\u53f7\u7cfb\u7edf\u3002\u968f\u7740\u4e92\u8054\u7f51\u7684\u53d1\u5c55\uff0c\u6df7\u5408\u591a\u79cd\u8bed\u8a00\u7684\u6570\u636e\u53d8\u5f97\u5f88\u5e38\u89c1\uff08\u8bd1\u6ce8\uff1a\u6bd4\u5982\u672c\u8eab\u7684\u82f1\u6587\u539f\u6587\u6216\u4e2d\u6587\u7ffb\u8bd1\u90fd\u5305\u542b\u4e86ASCII\u3001\u4e2d\u6587\u3001\u65e5\u6587\u7b49\u591a\u79cd\u8bed\u8a00\u5b57\u7b26\uff09\u3002\u5982\u4f55\u6709\u6548\u5904\u7406\u8fd9\u4e9b\u5305\u542b\u4e86\u5404\u79cd\u8bed\u8a00\u7684\u4e30\u5bcc\u591a\u6837\u7684\u6587\u672c\u6570\u636e\u5462\uff1f\n\n\u7b54\u6848\u5c31\u662f\u4f7f\u7528Unicode(http:\/\/unicode.org) \uff0c\u5b83\u6536\u96c6\u4e86\u8fd9\u4e2a\u4e16\u754c\u4e0a\u6240\u6709\u7684\u7b26\u53f7\u7cfb\u7edf\uff0c\u5305\u62ec\u91cd\u97f3\u7b26\u53f7\u548c\u5176\u5b83\u53d8\u97f3\u7b26\u53f7\uff0c\u5236\u8868\u7b26\u548c\u56de\u8f66\u7b26\uff0c\u8fd8\u6709\u5f88\u591a\u795e\u79d8\u7684\u7b26\u53f7\uff0c\u5728\u7b2c\u516b\u7248\u672c\u7684Unicode\u6807\u51c6\u6536\u96c6\u4e86\u8d85\u8fc7120,000\u4e2a\u5b57\u7b26\uff0c\u6db5\u76d6\u8d85\u8fc7100\u591a\u79cd\u8bed\u8a00\u3002\u8fd9\u4e9b\u5728\u8ba1\u7b97\u673a\u7a0b\u5e8f\u548c\u6570\u636e\u4e2d\u662f\u5982\u4f55\u4f53\u73b0\u7684\u5462\uff1f\n\n\/\/ ==========================&gt; unicode\u5b57\u7b26\u4e0eunicode\u7801\u70b9 &lt;==========================\n\u6bcf\u4e2a\u7b26\u53f7\u90fd\u88ab\u6536\u5f55\u8fdb\u4e86unicode\u4e2d\uff0c\u79f0\u4e4b\u4e3aunicode\u5b57\u7b26\uff0c\u6bcf\u4e00\u4e2aunicode\u5b57\u7b26\u90fd\u88ab\u5206\u914d\u4e00\u4e2a\u552f\u4e00\u7684Unicode\u683c\u5f0f\u7684\u6570\u5b57\uff0c\u8be5\u6570\u5b57\u79f0\u4e4b\u4e3aunincode\u7684\u7801\u70b9\uff0c\u901a\u7528\u7684\u8868\u793a\u4e00\u4e2aUnicode\u7801\u70b9\u7684\u6570\u636e\u7c7b\u578b\u662fint32\uff0c\u4e5f\u5c31\u662fGo\u8bed\u8a00\u4e2d\u7684rune\u7c7b\u578b\uff08rune\u662fint32\u7b49\u4ef7\u7c7b\u578b\uff09\uff1b\u5b83\u7684\u540c\u4e49\u8bcdrune\u7b26\u6587\u6b63\u662f\u8fd9\u4e2a\u610f\u601d\u3002\n\nunicode\u9075\u5faa\u4e24\u79cd\u6807\u51c6UCS-2\u548cUCS-4\uff0cUCS-2\u7528\u4e24\u4e2a\u5b57\u8282\u7f16\u7801\uff08go\u4e2d\u5bf9\u5e94\u7684\u8f6c\u4e49\u5b57\u7b26\u662f\\u\uff09\uff0cUCS-4\u75284\u4e2a\u5b57\u8282\u7f16\u7801\uff08go\u4e2d\u5bf9\u5e94\u7684\u8f6c\u4e49\u5b57\u7b26\u662f\\U\uff09\uff0c\\uhhhh\u5bf9\u5e9416bit\u7684\u7801\u70b9\u503c\uff0c\\Uhhhhhhhh\u5bf9\u5e9432bit\u7684\u7801\u70b9\u503c\uff0c\u5176\u4e2dh\u662f\u4e00\u4e2a\u5341\u516d\u8fdb\u5236\u6570\u5b57\uff1b\u4e00\u822c\u5f88\u5c11\u9700\u8981\u4f7f\u752832bit\u7684\u5f62\u5f0f\uff0c\u8fd9\u6b63\u5982\u6211\u4eec\u5e38\u8bf4\u7684\u90a3\u6837\uff0cunicode\u901a\u5e38\u7528\u4e24\u4e2a\u5b57\u8282\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\u3002\n\n\u6211\u4eec*\u53ef\u4ee5*\u5c06\u4e00\u4e2a\u7b26\u6587\u5e8f\u5217\u8868\u793a\u4e3a\u4e00\u4e2aint32\u5e8f\u5217\u3002\u8fd9\u79cd\u7f16\u7801\u65b9\u5f0f\u53ebUTF-32\u6216UCS-4\uff0c\u6bcf\u4e2aUnicode\u7801\u70b9\u90fd\u4f7f\u7528\u540c\u6837\u7684\u5927\u5c0f32bit\u6765\u8868\u793a\u3002\u8fd9\u79cd\u65b9\u5f0f\u6bd4\u8f83\u7b80\u5355\u7edf\u4e00\uff0c\u4f46\u5982\u679c\u6587\u672c\u4e2d\u5305\u542b\u7684\u5927\u591a\u6570\u90fd\u662f\u82f1\u6587\u5b57\u7b26\u7684\u8bdd\uff0c\u5b83\u4f1a\u6d6a\u8d39\u5f88\u591a\u5b58\u50a8\u7a7a\u95f4\uff0c\u56e0\u4e3a\u4e00\u4e2a\u82f1\u6587\u5b57\u7b26\u53ea\u9700\u89818bit\u62161\u5b57\u8282\u5c31\u80fd\u8868\u793a\u3002\u800c\u4e14\u6211\u4eec\u5e38\u7528\u7684\u5b57\u7b26\u662f\u8fdc\u5c11\u4e8e65,536\u4e2a\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\u752816bit\u7f16\u7801\u65b9\u5f0f\uff08UCS-2\uff09\u5c31\u80fd\u8868\u8fbe\u5e38\u7528\u5b57\u7b26\u3002\u4e3a\u4e86\u89e3\u51b3unicode\u7684\u95ee\u9898\uff0cutf-8\u5e94\u8fd0\u800c\u751f\u3002\n\n\/\/ ==========================&gt; utf-8 &lt;==========================\n\u524d\u8a00\uff1aUtf-8\u5c31\u662funicode\u7684\u4e00\u4e2a\u6807\u51c6\uff0c\u628aunicode\u7684\u5185\u5bb9\u8fdb\u884c\u4e86\u7f16\u6392\uff0c\u5b83\u672c\u8d28\u5176\u5b9e\u5c31\u662funicode\uff0cutf-8\u5168\u79f0\u4e5f\u662funicode\u7684\u4e00\u4e2a\u8f6c\u6362\u683c\u5f0f\uff0c\u5728\u5f97\u5230\u4e00\u4e2aunicode\u540e\uff0c\u6211\u4eec\u901a\u8fc7\u5c06\u5176\u6309\u7167utf-8\u7684\u683c\u5f0f\u7f16\u6392\u4e00\u4e0b\uff0c\u66f4\u7cbe\u7b80\u66f4\u7f8e\u597d\n\n\u8be6\u7ec6\uff1a\nUTF8\u7f16\u7801\u7531Go\u8bed\u8a00\u4e4b\u7236Ken Thompson\u548cRob Pike\u5171\u540c\u53d1\u660e\u7684\uff0c\u73b0\u5728\u5df2\u7ecf\u662fUnicode\u7684\u6807\u51c6\u3002\nUTF8\u5168\u79f0(Unicode Transformation Format 8), \u662f\u4e00\u79cd\u5c06Unicode\u7801\u70b9\u7f16\u7801\u4e3a\u5b57\u8282\u5e8f\u5217\u7684\u53d8\u957f\u7f16\u7801\u3002\nUTF8\u7f16\u7801\u4f7f\u75281\u52304\u4e2a\u5b57\u8282\u6765\u8868\u793a\u6bcf\u4e2aUnicode\u7801\u70b9\uff0c\u5176\u4e2d\n    - ASCII\u90e8\u5206\u5b57\u7b26\u53ea\u4f7f\u75281\u4e2a\u5b57\u8282\n    - \u5e38\u7528\u5b57\u7b26\u90e8\u5206\u4f7f\u75282\u62163\u4e2a\u5b57\u8282\u8868\u793a\u3002\n\u6bcf\u4e2a\u7b26\u53f7\u7f16\u7801\u540e\u7b2c\u4e00\u4e2a\u5b57\u8282\u7684\u9ad8\u7aefbit\u4f4d\u7528\u4e8e\u8868\u793a\u603b\u5171\u6709\u591a\u5c11\u7f16\u7801\u4e2a\u5b57\u8282\u3002\u5982\u679c\u7b2c\u4e00\u4e2a\u5b57\u8282\u7684\u9ad8\u7aefbit\u4e3a0\uff0c\u5219\u8868\u793a\u5bf9\u5e947bit\u7684ASCII\u5b57\u7b26\uff0cASCII\u5b57\u7b26\u6bcf\u4e2a\u5b57\u7b26\u4f9d\u7136\u662f\u4e00\u4e2a\u5b57\u8282\uff0c\u548c\u4f20\u7edf\u7684ASCII\u7f16\u7801\u517c\u5bb9\u3002\u5982\u679c\u7b2c\u4e00\u4e2a\u5b57\u8282\u7684\u9ad8\u7aefbit\u662f110\uff0c\u5219\u8bf4\u660e\u9700\u89812\u4e2a\u5b57\u8282\uff1b\u540e\u7eed\u7684\u6bcf\u4e2a\u9ad8\u7aefbit\u90fd\u4ee510\u5f00\u5934\u3002\u66f4\u5927\u7684Unicode\u7801\u70b9\u4e5f\u662f\u91c7\u7528\u7c7b\u4f3c\u7684\u7b56\u7565\u5904\u7406\u3002\n\n0xxxxxxx                             runes 0-127    (ASCII)\n110xxxxx 10xxxxxx                    128-2047       (values &lt;128 unused)\n1110xxxx 10xxxxxx 10xxxxxx           2048-65535     (values &lt;2048 unused)\n11110xxx 10xxxxxx 10xxxxxx 10xxxxxx  65536-0x10ffff (other values unused)\n\nutf-8\u8fd9\u79cd\u53d8\u957f\u7684\u7f16\u7801\uff0c\u56e0\u4e3a\u53d8\u957f\u800c\u5e26\u6765\u7684\u7f3a\u70b9\u662f\uff1a\u65e0\u6cd5\u76f4\u63a5\u901a\u8fc7\u7d22\u5f15\u6765\u8bbf\u95ee\u7b2cn\u4e2a\u5b57\u7b26(python3\u4e2d\u5b57\u7b26\u4e32\u88ab\u5b58\u6210unicode\u7684\u597d\u5904\u5c31\u662f\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u7b2cn\u4e2a\u5b57\u7b26\uff0c\u800c\u8fd9\u4e00\u70b9go\u8bed\u8a00\u662f\u505a\u4e0d\u5230\u7684)\n\u4f46\u662fUTF8\u4e5f\u56e0\u4e3a\u53d8\u957f\u83b7\u5f97\u4e86\u5f88\u591a\u989d\u5916\u7684\u4f18\u70b9\uff1a\u9996\u5148UTF8\u7f16\u7801\u6bd4\u8f83\u7d27\u51d1\uff0c\u5b8c\u5168\u517c\u5bb9ASCII\u7801\uff0c\u5e76\u4e14\u53ef\u4ee5\u81ea\u52a8\u540c\u6b65\uff1a\u5b83\u53ef\u4ee5\u901a\u8fc7\u5411\u524d\u56de\u6714\u6700\u591a2\u4e2a\u5b57\u8282\u5c31\u80fd\u786e\u5b9a\u5f53\u524d\u5b57\u7b26\u7f16\u7801\u7684\u5f00\u59cb\u5b57\u8282\u7684\u4f4d\u7f6e\u3002\u5b83\u4e5f\u662f\u4e00\u4e2a\u524d\u7f00\u7f16\u7801\uff0c\u6240\u4ee5\u5f53\u4ece\u5de6\u5411\u53f3\u89e3\u7801\u65f6\u4e0d\u4f1a\u6709\u4efb\u4f55\u6b67\u4e49\u4e5f\u5e76\u4e0d\u9700\u8981\u5411\u524d\u67e5\u770b\uff08\u8bd1\u6ce8\uff1a\u50cfGBK\u4e4b\u7c7b\u7684\u7f16\u7801\uff0c\u5982\u679c\u4e0d\u77e5\u9053\u8d77\u70b9\u4f4d\u7f6e\u5219\u53ef\u80fd\u4f1a\u51fa\u73b0\u6b67\u4e49\uff09\u3002\u6ca1\u6709\u4efb\u4f55\u5b57\u7b26\u7684\u7f16\u7801\u662f\u5176\u5b83\u5b57\u7b26\u7f16\u7801\u7684\u5b50\u4e32\uff0c\u6216\u662f\u5176\u5b83\u7f16\u7801\u5e8f\u5217\u7684\u5b57\u4e32\uff0c\u56e0\u6b64\u641c\u7d22\u4e00\u4e2a\u5b57\u7b26\u65f6\u53ea\u8981\u641c\u7d22\u5b83\u7684\u5b57\u8282\u7f16\u7801\u5e8f\u5217\u5373\u53ef\uff0c\u4e0d\u7528\u62c5\u5fc3\u524d\u540e\u7684\u4e0a\u4e0b\u6587\u4f1a\u5bf9\u641c\u7d22\u7ed3\u679c\u4ea7\u751f\u5e72\u6270\u3002\u540c\u65f6UTF8\u7f16\u7801\u7684\u987a\u5e8f\u548cUnicode\u7801\u70b9\u7684\u987a\u5e8f\u4e00\u81f4\uff0c\u56e0\u6b64\u53ef\u4ee5\u76f4\u63a5\u6392\u5e8fUTF8\u7f16\u7801\u5e8f\u5217\u3002\u540c\u65f6\u56e0\u4e3a\u6ca1\u6709\u5d4c\u5165\u7684NUL(0)\u5b57\u8282\uff0c\u53ef\u4ee5\u5f88\u597d\u5730\u517c\u5bb9\u90a3\u4e9b\u4f7f\u7528NUL\u4f5c\u4e3a\u5b57\u7b26\u4e32\u7ed3\u5c3e\u7684\u7f16\u7a0b\u8bed\u8a00\u3002\n\nGo\u8bed\u8a00\u7684\u6e90\u6587\u4ef6\u91c7\u7528UTF8\u7f16\u7801\uff0c\u5e76\u4e14Go\u8bed\u8a00\u5904\u7406UTF8\u7f16\u7801\u7684\u6587\u672c\u4e5f\u5f88\u51fa\u8272\u3002unicode\u5305\u63d0\u4f9b\u4e86\u8bf8\u591a\u5904\u7406rune\u5b57\u7b26\u76f8\u5173\u529f\u80fd\u7684\u51fd\u6570\uff08\u6bd4\u5982\u533a\u5206\u5b57\u6bcd\u548c\u6570\u7ec4\uff0c\u6216\u8005\u662f\u5b57\u6bcd\u7684\u5927\u5199\u548c\u5c0f\u5199\u8f6c\u6362\u7b49\uff09\uff0cunicode\/utf8\u5305\u5219\u63d0\u4f9b\u4e86\u7528\u4e8erune\u5b57\u7b26\u5e8f\u5217\u7684UTF8\u7f16\u7801\u548c\u89e3\u7801\u7684\u529f\u80fd\u3002\n\n\u66f4\u591a\u5173\u4e8e\u5b57\u7b26\u7f16\u7801\u7684\u77e5\u8bc6\u8bf7\u53c2\u8003\nhttps:\/\/www.cnblogs.com\/linhaifeng\/articles\/5950339.html\nhttps:\/\/baike.baidu.com\/item\/Unicode\/750500?fr=aladdin\nhttps:\/\/www.kancloud.cn\/lbb4511\/gopl\/1107841\nhttps:\/\/www.kancloud.cn\/lbb4511\/gopl\/1107842<\/code><\/pre>\n<h3>3.2 rune\u7c7b\u578b<\/h3>\n<ul>\n<li>\n<p>1\u3001rune\u7c7b\u578b\u4ecb\u7ecd<\/p>\n<p>\u5b57\u7b26\u4e32\u7c7b\u578b\u5fc5\u987b\u7528\u53cc\u5f15\u53f7<\/p>\n<p>rune\u7c7b\u578b\u5fc5\u987b\u7528\u5355\u5f15\u53f7<\/p>\n<p>\u5b57\u7b26\u4e32\u7c7b\u578b\u53ef\u7528\u4e8e\u5b58\u653e\u4e00\u4e32\u5b57\u7b26\uff0c\u8fd9\u4e00\u4e32\u5b57\u7b26\u5e95\u5c42\u662f\u4e00\u4e2a\u5207\u7247\uff0c\u8be5\u5207\u7247\u6307\u5411\u4e00\u4e2a\u6570\u7ec4\uff0c\u6570\u7ec4\u4e2d\u5b58\u4e00\u7cfb\u5217byte<\/p>\n<p>rune\u7c7b\u578b\u53ea\u80fd\u7528\u4e8e\u5b58\u653e\u5355\u4e2a\u5b57\u7b26\uff0c\u5355\u4e2a\u5b57\u7b26\u5e95\u5c42\u662f\u4e00\u4e2aunicode\u7801\u70b9\uff0c\u5b58\u6210int32<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"language-go\">\u6e90\u7801\u4e2d\ntype byte = uint8  \/\/ byte\u662fuint8\u7b49\u4ef7\u7c7b\u578b\ntype rune = int32  \/\/ rune\u662fint32\u7b49\u4ef7\u7c7b\u578b\n\n\/\/ rune\u4e0eint32\u5b8c\u5168\u7b49\u4ef7\uff0c\u53ef\u4ee5\u6df7\u7528\uff0c\u5982\u4e0b\uff0c\u540c\u7406byte\u4e0euint8\u5b8c\u5168\u7b49\u4ef7\uff0cbyte(111)\u5c31\u662fuint8(111)\nvar x rune = 10\nvar y int32 = 20\nfmt.Println(x + y)  \/\/ 30\n\n\u5728unicode\u7f16\u7801\u91cc\uff0c\u6bcf\u4e2a\u5b57\u7b26\u90fd\u5bf9\u5e94\u4e3a\u4e00\u4e2a\u7801\u70b9\n\u4e00\u4e2a\u7801\u70b9\u5c31\u662f\u4e00\u4e2a\u6570\u5b57\uff0c\u8be5\u6570\u5b57\u5bf9\u5e94go\u8bed\u8a00\u4e2d\u7684rune\u7c7b\u578b<\/code><\/pre>\n<ul>\n<li>\n<p>2\u3001rune\u7c7b\u578b\u793a\u4f8b<\/p>\n<p>rune\u8868\u8fbe\u7684\u662f\u5b57\u7b26\u7c7b\u578b\uff0c\u5b58\u653e\u5b57\u7b26\u9700\u8981\u7528\u5355\u5f15\u53f7\u5305\u542b\uff0c\u6ce8\u610f\u5355\u5f15\u53f7\u5185\u6709\u4e14\u4ec5\u6709\u4e00\u4e2a\u5b57\u7b26<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"language-go\">s1:=&#039;\u6c6a&#039;\nfmt.Println(s1)\nfmt.Printf(&quot;%T&quot;,s1) \/\/ int32,\u4e0d\u8981\u6000\u7591\u4f60\u7684\u53cc\u773c\uff0cint32\u5c31\u662frune\uff0crune\u5c31\u662fint32\uff0c\u4e8c\u8005\u7b49\u540c\nfmt.Printf(&quot;%x\\n&quot;,s1)  \/\/ \u6253\u5370\u6240\u5b58int32\u7684\u5341\u516d\u8fdb\u5236\u683c\u5f0f\uff1a6c6a\nfmt.Printf(&quot;%d\\n&quot;,s1)  \/\/ \u6253\u5370\u6240\u5b58int32\u7684\u5341\u8fdb\u5236\u683c\u5f0f\uff1a27754\n\ns2:=&#039;\u6c6a\u6c6a&#039; \/\/ \u7f16\u8bd1\u9519\u8bef\uff1ainvalid character literal (more than one character)\n\nfmt.Println(&#039;A&#039;)       \/\/ 65\nfmt.Println(&#039;a&#039;)       \/\/ 97\nfmt.Println(&#039;A&#039; - &#039;a&#039;) \/\/ -32<\/code><\/pre>\n<ul>\n<li>3\u3001rune\u7c7b\u578b\u4e2d\u5305\u542b\u8f6c\u4e49\u7b26<\/li>\n<\/ul>\n<pre><code class=\"language-go\">\u5355\u5f15\u53f7\u5b57\u7b26\u5fc5\u987b\u8f6c\u4e49\n&#039;\\&#039;&#039;\nUnicode\u8f6c\u4e49\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5728rune\u5b57\u7b26\u4e2d\u3002\u4e0b\u9762\u4e09\u4e2a\u5b57\u7b26\u662f\u7b49\u4ef7\u7684\uff1a\n&#039;\u4e16&#039; \n&#039;\\u4e16&#039; \n&#039;\\U00004e16&#039;\n\n\u5bf9\u4e8e\u5c0f\u4e8e256\u7801\u70b9\u503c\u53ef\u4ee5\u5199\u5728\u4e00\u4e2a\u5341\u516d\u8fdb\u5236\u8f6c\u4e49\u5b57\u8282\u4e2d\uff0c\u4f8b\u5982&#039;\\x41&#039;\u5bf9\u5e94\u5b57\u7b26&#039;A&#039;\uff0c\u4f46\u662f\u5bf9\u4e8e\u66f4\u5927\u7684\u7801\u70b9\u5219\u5fc5\u987b\u4f7f\u7528\\u\u6216\\U\u8f6c\u4e49\u5f62\u5f0f\uff0c\u56e0\u6b64\uff0c&#039;\\xe4\\xb8\\x96&#039;\u5e76\u4e0d\u662f\u4e00\u4e2a\u5408\u6cd5\u7684rune\u5b57\u7b26\uff0c\u867d\u7136\u8fd9\u4e09\u4e2a\u5b57\u8282\u5bf9\u5e94\u4e00\u4e2a\u6709\u6548\u7684UTF8\u7f16\u7801\u7684\u7801\u70b9\u3002<\/code><\/pre>\n<ul>\n<li>4\u3001\u533a\u5206\u5b57\u8282\u6570\u4e0e\u5b57\u7b26\u6570<\/li>\n<\/ul>\n<pre><code class=\"language-go\">\u5982\u4e0b\u5b57\u7b26\u4e32\u7531\u5b57\u7b26\u4e32\u9762\u503c\u6784\u6210\uff0c\u4f1a\u6309\u7167utf-8\u683c\u5f0f\u7f16\u7801\u621013\u4e2a\u5b57\u8282\uff0c\u7136\u540e\u5b58\u5165\u5e95\u5c42\u6570\u7ec4\ns := &quot;Hello, \u4e16\u754c&quot;\nfmt.Println(len(s))                    \/\/ &quot;13&quot;\uff0c\u4ee3\u8868\u5e95\u5c42\u6570\u7ec4\u4e2d\u670913\u4e2a\u5b57\u8282\nfmt.Println(utf8.RuneCountInString(s)) \/\/ &quot;9&quot;\uff0c\u4ee3\u8868\u4e0a\u8ff013\u4e2a\u5b57\u8282\u5bf9\u5e949\u4e2aUnicode\u5b57\u7b26<\/code><\/pre>\n<h2>\u56db \u6570\u636e\u7c7b\u578b\u8f6c\u6362<\/h2>\n<p>1\u3001string(\u5341\u8fdb\u5236\u6570\u5b57)\u6309\u7167ASCII\u89e3\u6790\u6210\u5b57\u7b26<\/p>\n<pre><code class=\"language-go\">x:=&#039;A&#039;\nfmt.Println(x)  \/\/ 65\nfmt.Printf(&quot;%T\\n&quot;,x)  \/\/ int32\nfmt.Println(string(x))  \/\/ A\n\nfmt.Println(string(65))  \/\/ A\nfmt.Println(string(90))  \/\/ Z<\/code><\/pre>\n<p>2\u3001string(\u5341\u8fdb\u5236\u6570\u5b57)\u6309\u7167unicode\u89e3\u6790\u6210\u5b57\u7b26<\/p>\n<pre><code class=\"language-go\">s1:=&#039;\u6c6a&#039;\nfmt.Printf(&quot;%d\\n&quot;,s1)  \/\/ \u6253\u5370\u6240\u5b58int32\u7684\u5341\u8fdb\u5236\u683c\u5f0f\uff1a27754\n\nfmt.Println(string(27754))  \/\/ \u6c6a<\/code><\/pre>\n<p>3\u3001\u5b57\u7b26\u4e32string\u4e0e\u5b57\u8282\u5207\u7247[]byte\u4e92\u8f6c<\/p>\n<pre><code class=\"language-go\">\/\/ &quot;egon\u6797&quot;\u662fstring\u7c7b\u578b\uff0cstring\u7c7b\u578b\u5e95\u5c42\u662f\u5b57\u8282\u5207\u7247\uff08\u5207\u7247\u6307\u5411\u4e00\u4e2a\u5e95\u5c42\u6570\u7ec4\uff09\n\/\/ \u6240\u4ee5\u8bf4string\u7c7b\u578b\u4e0e[]byte\u6709\u76f8\u540c\u7684\u7ed3\u6784\uff0c\u4e8e\u662f\u6211\u4eec\u53ef\u4ee5\u8fdb\u884c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\nvar name string = &quot;egon\u6797&quot;\n\n\/\/ \u4f8b1\uff1astring\u8f6c[]byte\nvar slice = []byte(name)\nfmt.Println(slice)  \/\/ [101 103 111 110 230 158 151]\n\nvar arr = [7]byte(name)  \/\/ \u62a5\u9519\uff1astring\u7c7b\u578b\u65e0\u6cd5\u8f6c\u6362\u6210[7]byte\u6570\u7ec4\n\n\/\/ \u4f8b2\uff1a[]byte\u8f6c\u6362string\nz := []byte{101, 103, 111, 110, 230, 158, 151}\nfmt.Println(string(z))         \/\/ egon\u6797\nfmt.Println(string(z) == name) \/\/ true<\/code><\/pre>\n<p>4\u3001\u5b57\u7b26\u4e32string\u4e0e\u5b57\u7b26\u5207\u7247[]rune\u4e92\u8f6c<\/p>\n<pre><code class=\"language-go\">\/\/ 1\u3001string\u8f6c\u6210[]rune\nname:=&quot;egon\u6797&quot;\nx:=[]rune(name)\nfmt.Println(x)  \/\/ [101 103 111 110 26519],\u5207\u7247\u5185\u5b58\u653e\u4e00\u4e2arune\u7b26\u6587\uff0c\u6216\u79f0unicode\u7801\u70b9\uff0c\u672c\u8d28\u5c31\u662f\u4e00\u4e2a\u4e2aint32\u7c7b\u578b\u7684\u6570\u5b57\nfmt.Printf(&quot;%T\\n&quot;,x[0])  \/\/ int32\n\n\/\/ 2\u3001[]rune\u8f6c\u6210string\nfmt.Println(string(x))  \/\/ egon\u6797<\/code><\/pre>\n<h2>\u4e94 \u5b57\u7b26\u4e32\u57fa\u672c\u64cd\u4f5c<\/h2>\n<h3>5.1 \u7d22\u5f15\u64cd\u4f5c\u4e4b\u53d6\u5355\u4e2a\u5b57\u8282\/byte<\/h3>\n<p>\u6ce8\u610f\u5b57\u7b26\u4e32\u7684\u5e95\u5c42\u662f\u5b57\u8282\u5207\u7247\uff0c\u6839\u636e\u7d22\u5f15\u53d6\u51fa\u7684\u80af\u5b9a\u662f\u7b2cn\u4e2a\u5b57\u8282\uff0c\u6ce8\u610f\u6ce8\u610f\u6ce8\u610f\u662fbyte\u3001\u800c\u975e\u5b57\u7b26rune<\/p>\n<pre><code class=\"language-go\">s:=&quot;hi\u4f60\u597d\u5440&quot;\nfmt.Println(s[0]) \/\/ 104\n\/\/fmt.Println(s[len(s1)]) \/\/ \u8d85\u51fa\u5b57\u7b26\u4e32\u7d22\u5f15\u8303\u56f4\u5bfc\u81f4panic\u5f02\u5e38\n\/\/fmt.Println(s[-1]) \/\/ \u4e0d\u652f\u6301\u8d1f\u5411\u7d22\u5f15\u5bfc\u81f4panic\u5f02\u5e38\n\n\/\/ ps\uff1a\ngo\u4e2ds[i]\u8bbf\u95ee\u7684\u90fd\u662f\u7b2ci\u4e2a\u5b57\u8282\u800c\u4e0d\u662f\u5b57\u7b26\uff0c\u56e0\u4e3ago\u7684\u5b57\u7b26\u4e32s\u7528\u7684\u662futf-8\u7f16\u7801,\u662f\u53d8\u957f\u7684\uff0c\u800c\u53d8\u957f\u7684\u7f16\u7801\u65e0\u6cd5\u901a\u8fc7\u7d22\u5f15\u64cd\u4f5c\u786e\u5b9a\u5b57\u7b26\u5360\u7528\u5b57\u8282\u7684\u4e2a\u6570\uff0c\u6240\u4ee5\u53ea\u80fd\u53d6\u5b57\u8282\u4e86\uff0c\u8fd9\u662f\u5b83\u7684\u7f3a\u70b9\uff1a\n\n\u800cpython\u4e2ds[i]\u8bbf\u95ee\u7684\u90fd\u662f\u7b2ci\u4e2a\u5b57\u7b26\uff0c\u56e0\u4e3apython3\u4e2d\u5b57\u7b26\u4e32\u7528\u662funicode\u7f16\u7801\uff0c\u662f\u5b9a\u957f\u7684<\/code><\/pre>\n<h3>5.2 \u7d22\u5f15\u64cd\u4f5c\u4e4b\u53d6\u5207\u7247<\/h3>\n<p>\u50a8\u5907\u77e5\u8bc6\uff1a<\/p>\n<ul>\n<li>\n<p>\u95ee\uff1a<\/p>\n<p>\u65e2\u7136\u5b57\u7b26\u4e32\u7c7b\u578b\u5e95\u5c42\u662f[]byte\u5207\u7247\uff0c\u90a3\u4e48fmt.Println()\u6253\u5370\u51fa\u6765\u7684\u4e0d\u5e94\u8be5\u5c31\u662f[]byte\u90a3\u4e48\uff0c\u4e3a\u4f55\u6253\u5370\u51fa\u7684\u662f\u5b57\u7b26\u5462\uff1f\uff1f\uff1f<\/p>\n<\/li>\n<li>\n<p>\u7b54\uff1a<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"language-go\">\u5b57\u7b26\u4e32\u5e95\u5c42\u786e\u5b9e\u662f[]byte\uff0c\u4f46\u662f\u5728fmt.Println()\u6253\u5370\u65f6\uff0c\u4f1a\u8c03\u7528String()\u65b9\u6cd5\uff0c\u5c06\u5b57\u8282\u6309\u7167utf8\u89e3\u7801\u6210\u5b57\u7b26\n\u5373\uff1a\nvar name string = &quot;egon\u6797&quot;  \/\/ \u5e95\u5c42\u5b58\u6210[]byte{101, 103, 111, 110, 230, 158, 151}\nfmt.Println(name)  \/\/ \u5e95\u5c42\u5b9e\u9645\u64cd\u4f5c\uff1afmt.Println(string(\u4e0a\u8ff0\u5207\u7247))<\/code><\/pre>\n<ul>\n<li>\n<p>\u603b\u7ed3<\/p>\n<p>\u6253\u5370\u5b57\u7b26\u4e32\uff0c\u5c31\u662f\u5728\u6253\u5370\u5e95\u5c42\u5b57\u8282\u5207\u7247[]byte\uff0c\u800c\u5728\u6253\u5370\u8fd9\u4e2a\u5b57\u8282\u5207\u7247\u65f6\u4f1a\u8c03\u7528String()\u65b9\u6cd5\u5c06\u5176\u6309\u7167utf8\u89e3\u7801\u6210\u5b57\u7b26<\/p>\n<\/li>\n<\/ul>\n<p>\u4f8b1\uff1a\u56e0\u4e3a\u5b57\u7b26\u4e32\u5e95\u5c42\u5c31\u662f[]byte\u5207\u7247\uff0c\u6240\u4ee5\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u5207\u7247\u64cd\u4f5c\uff0c\u5f97\u5230\u7684\u6570\u636e\u5e95\u5c42\u5176\u5b9e\u4ecd\u7136\u662f[]byte\uff0c\u6211\u4eec\u6253\u5370\u65f6\uff0c\u6253\u5370\u51fa\u7684\u4ecd\u7136\u662f\u5b57\u7b26<\/p>\n<pre><code class=\"language-go\">s1:=\"hi\u4f60\u597d\u5440\"\n\n\/\/ \u987e\u5934\u4e0d\u987e\u5c3e\nfmt.Printf(\"%T\",s1[0:1])  \/\/ string\n\nfmt.Println(s1[0:1]) \/\/ h\nfmt.Println(s1[0:2]) \/\/ hi\nfmt.Println(s1[0:3]) \/\/ hi\ufffd\nfmt.Println(s1[0:4]) \/\/ hi\ufffd\nfmt.Println(s1[0:5]) \/\/ hi\u4f60\nfmt.Println(s1[:5]) \/\/ hi\u4f60\uff0c\u9ed8\u8ba4\u4ece0\u5f00\u59cb\nfmt.Println(s1[2:]) \/\/ \u4f60\u597d\u5440\uff0c\u9ed8\u8ba4\u5230\u672b\u5c3e\nfmt.Println(s1[:]) \/\/  hi\u4f60\u597d\u5440\uff0c\u9ed8\u8ba4\u4ece\u5934\u5230\u672b\u5c3e<\/code><\/pre>\n<p>\u4f8b2\uff1a\u5982\u679c\u53ea\u662f\u53d6\u5230\u5355\u72ec\u67d0\u4e2a\u5143\u7d20\uff0c\u800c\u4e0d\u662f[]byte\uff0c\u90a3\u4e48\u6253\u5370\u7684\u5c31\u662f\u5b57\u8282\u4e86\uff0c\u5982\u4e0b<\/p>\n<pre><code class=\"language-go\">fmt.Println(s1[0]) \/\/ 104\n\n\/\/ \u6211\u4eec\u5f53\u7136\u53ef\u4ee5\u624b\u52a8\u8c03\u7528string()\u6765\u8f6c\u6362\u5b57\u7b26\nfmt.Println(string(s1[0])) \/\/ h\n\n\u56e0\u4e3a104\u8fd9\u4e2a\u5b57\u8282\u5c31\u5bf9\u5e94\u4e00\u4e2a\u82f1\u6587\u5b57\u7b26\uff0c\u53ef\u4ee5\u8f6c\u6362\u6210\u529f<\/code><\/pre>\n<h3>5.3 \u5b57\u7b26\u4e32\u4e0d\u53ef\u4fee\u6539<\/h3>\n<p>\u5b57\u7b26\u4e32\u5e95\u5c42\u662f\u4e00\u4e2a[]byte\u5207\u7247\uff0c\u6307\u5411byte\u5143\u7d20\u7ec4\u6210\u7684\u6570\u7ec4\uff0c\u800c\u5b57\u7b26\u4e32\u5e95\u5c42\u6570\u7ec4\u5185\u7684byte\u662f\u4e0d\u53ef\u4ee5\u88ab\u66ff\u6362\u7684\uff0c\u5373\u4e0d\u80fd\u4fee\u6539\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u7684\u5185\u5bb9\u3002<\/p>\n<pre><code class=\"language-go\">s:=&quot;hi\u4f60\u597d\u5440&quot;\ns[0] = &#039;L&#039; \/\/ \u7f16\u8bd1\u9519\u8bef: cannot assign to s[0]<\/code><\/pre>\n<p>\u4e0d\u53d8\u6027\u610f\u5473\u5982\u679c\u4e24\u4e2a\u5b57\u7b26\u4e32\u5171\u4eab\u76f8\u540c\u7684\u5e95\u5c42\u6570\u636e\u7684\u8bdd\u4e5f\u662f\u5b89\u5168\u7684\uff0c\u8fd9\u4f7f\u5f97<\/p>\n<ul>\n<li>\n<p>1\u3001\u590d\u5236\u4efb\u4f55\u957f\u5ea6\u7684\u5b57\u7b26\u4e32\u4ee3\u4ef7\u662f\u4f4e\u5ec9\u7684\u3002<\/p>\n<\/li>\n<li>\n<p>2\u3001\u540c\u6837\uff0c\u4e00\u4e2a\u5b57\u7b26\u4e32s\u548c\u5bf9\u5e94\u7684\u5b50\u5b57\u7b26\u4e32\u5207\u7247s[7:]\u7684\u64cd\u4f5c\u4e5f\u53ef\u4ee5\u5b89\u5168\u5730\u5171\u4eab\u76f8\u540c\u7684\u5185\u5b58\uff0c\u56e0\u6b64\u5b57\u7b26\u4e32\u5207\u7247\u64cd\u4f5c\u4ee3\u4ef7\u4e5f\u662f\u4f4e\u5ec9\u7684\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u5728\u8fd9\u4e24\u79cd\u60c5\u51b5\u4e0b\u90fd\u6ca1\u6709\u5fc5\u8981\u5206\u914d\u65b0\u7684\u5185\u5b58\u3002 \u4e0b\u56fe\u6f14\u793a\u4e86\u4e00\u4e2a\u5b57\u7b26\u4e32\u548c\u4e24\u4e2a\u5b57\u4e32\u5171\u4eab\u76f8\u540c\u7684\u5e95\u5c42\u6570\u636e\u3002<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='.\/img\/\u5b57\u7b26\u4e32\u5171\u4eab\u5e95\u5c42\u6570\u7ec4.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\".\/img\/\u5b57\u7b26\u4e32\u5171\u4eab\u5e95\u5c42\u6570\u7ec4.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20200529215401956\" \/><\/div><\/p>\n<h3>5.4 \u5229\u7528[]rune\u3001[]byte\u201c\u4fee\u6539\u201d\u5b57\u7b26\u4e32<\/h3>\n<p>string\u7c7b\u578b\u503c\u80af\u5b9a\u662f\u4e0d\u53ef\u4ee5\u88ab\u4fee\u6539\u7684\uff0c\u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u5c06string\u8f6c\u6362\u4e3a[]rune\u6216[]byte\u6765\u5b8c\u6210\u4fee\u6539\uff0c\u4fee\u6539\u5b8c\u6bd5\u540e\uff0c\u518d\u4f7f\u7528string()\u8f6c\u6362\u5f97\u5230\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u3002<\/p>\n<p><strong>1\u3001\u5c06string\u8f6c\u6362\u4e3a[]rune\u5207\u7247\uff0c\u7136\u540e\u8fdb\u884c\u4fee\u6539<\/strong><\/p>\n<pre><code class=\"language-go\">s1 := &quot;\u4f60\u662f\u5c0f\u738b\u5417&quot;\n\nrune1 := []rune(s1)  \/\/ []rune\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff0c\u6570\u7ec4\u5185\u5b58\u653e\u7684\u662f\u4eces1\u4e2d\u62f7\u8d1d\u8fc7\u6765\u7684\u5185\u5bb9\nrune1[len(rune1)-1] = &#039;\u5427&#039;  \/\/  \u6539\u52a8\u7684\u662f[]rune\u81ea\u5df1\u7684\u5e95\u5c42\u6570\u7ec4\uff0c\u8fd9\u662f\u53ef\u4ee5\u7684\uff0c\u6b64\u65f6\u6539\u7684\u662funicode\u7801\u70b9\uff0c\u5bf9\u5e94\u4e00\u4e2a\u5b8c\u6574\u7684\u5b57\u7b26\uff0c\u8981\u6539\u4e00\u4e2a\u4e2d\u6587\u5b57\u7b26\uff0c\u9700\u8981\u7528\u7d22\u5f15\u53d6\u51fa\u4e00\u4e2a\u7801\u70b9\u8fdb\u884c\u4fee\u6539\u5373\u53ef\n\n\/\/ string()\u53ef\u4ee5\u6309\u7167utf-8\u7f16\u7801\u683c\u5f0f\u5c06\u4e00\u4e2a[]rune\u7c7b\u578b\u7684Unicode\u5b57\u7b26\u5207\u7247\u6216\u6570\u7ec4\u8f6c\u4e3astring\u5b57\u7b26\u4e32\nfmt.Println(string(runeS2)) \/\/ \u5403\u9e21\u5427\n\nps\uff1a\n\u53ef\u4ee5\u5c06\u4e00\u4e2aUTF8\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u89e3\u7801\u4e3aUnicode\u5b57\u7b26\u5e8f\u5217\uff0cUTF8\u5b57\u7b26\u4e32\u4f5c\u4e3a\u4ea4\u6362\u683c\u5f0f\u662f\u975e\u5e38\u65b9\u4fbf\u7684\uff0c\u4f46\u662f\u5728\u7a0b\u5e8f\u5185\u90e8\u91c7\u7528rune\u5e8f\u5217\u53ef\u80fd\u66f4\u65b9\u4fbf\uff0c\u56e0\u4e3arune\u5927\u5c0f\u4e00\u81f4\uff0c\u652f\u6301\u6570\u7ec4\u7d22\u5f15\u548c\u65b9\u4fbf\u5207\u5272\u3002<\/code><\/pre>\n<p><strong>2\u3001\u5c06string\u8f6c\u6362\u4e3a[]byte\u5207\u7247\uff0c\u7136\u540e\u8fdb\u884c\u4fee\u6539<\/strong><\/p>\n<pre><code class=\"language-go\">s2 := &quot;\u4f60\u662f\u5c0f\u738b\u5417&quot;\n\nbyte1 := []byte(s2)  \/\/ \u4ea7\u751f\u4e86\u65b0\u7684\u6570\u7ec4\uff0c&#039;\u5417&#039;\u5bf9\u5e94\u7684byte\u7d22\u5f15\u4e3a12\u300113\u300114\nbyte1[12] = 229\nbyte1[13] = 144\nbyte1[14] = 167\nfmt.Println(string(byte1))  \/\/ \u4f60\u662f\u5c0f\u738b\u5427\n\ns3:=&quot;\u4f60\u662f\u5c0f\u738b\u5427&quot;\nfmt.Println([]byte(s3))  \/\/ [...... 229 144 167]<\/code><\/pre>\n<p>\u7ec3\u4e60\u98981\uff1a<\/p>\n<pre><code class=\"language-go\">\/\/ \u628a\u5b57\u7b26\u4e32\u5e95\u5c42\u7684byte\u8f6c\u6210unicode\u7801\u70b9\u5b58\u5165\u5207\u7247\uff1a[]rune\ns := &quot;egon\u6797\u6d77\u5cf0&quot;\n\/\/ fmt.Println(r) \u9ed8\u8ba4\u6253\u5370\u5341\u8fdb\u5236\uff0c%x\u5219\u6253\u5370\u5341\u516d\u673a\u5236\uff0c% x\u53c2\u6570\u7528\u4e8e\u5728\u6bcf\u4e2a\u5341\u516d\u8fdb\u5236\u6570\u5b57\u524d\u63d2\u5165\u4e00\u4e2a\u7a7a\u683c\uff0c\nfmt.Printf(&quot;% x\\n&quot;, s) \/\/ 65 67 6f 6e e6 9e 97 e6 b5 b7 e5 b3 b0\n\nr := []rune(s)\nfmt.Printf(&quot;%x\\n&quot;, r) \/\/ [65 67 6f 6e 6797 6d77 5cf0]\n\nr[0] = &#039;E&#039;\nr[5] = &#039;\\u55e8&#039;\nr[6] = &#039;\u75af&#039;\nfmt.Printf(&quot;%x\\n&quot;, r) \/\/ [45 67 6f 6e 6797 55e8 75af]\n\nfmt.Println(string(r)) \/\/ Egon\u6797\u55e8\u75af\nfmt.Printf(&quot;%q\\n&quot;, r) \/\/ [&#039;E&#039; &#039;g&#039; &#039;o&#039; &#039;n&#039; &#039;\u6797&#039; &#039;\u55e8&#039; &#039;\u75af&#039;]<\/code><\/pre>\n<p>\u7ec3\u4e60\u98982\uff1a<\/p>\n<pre><code class=\"language-go\">msg:=&quot;hi\u5403\u9e21\u5417&quot;\n\n\/\/ \u628a\u5b57\u7b26\u4e32\u5e95\u5c42\u7684byte\u5b58\u5165\u5207\u7247\uff1a[]byte\nx:=[]byte(msg)  \/\/ []byte\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff0c\u6570\u7ec4\u5185\u5b58\u653e\u7684\u662f\u4eces1\u4e2d\u62f7\u8d1d\u8fc7\u6765\u7684\u5185\u5bb9\nfmt.Println(x)  \/\/ [104 105 229 144 131 233 184 161 229 144 151]\n\n\/\/ \u4e0b\u8ff0\u6539\u52a8\u7684\u662f[]byte\u81ea\u5df1\u7684\u5e95\u5c42\u6570\u7ec4\uff0c\u8fd9\u662f\u53ef\u4ee5\u7684,\u4f46\u53ea\u80fd\u5355\u72ec\u4fee\u6539\u4e00\u4e2a\u4e2a\u7684byte\uff0c\u8981\u6539\u4e00\u4e2a\u4e2d\u6587\u5b57\u7b26\uff0c\u9700\u8981\u7528\u7d22\u5f15\u53d6\u51fa3\u4e2abyte\u6765\u9010\u4e00\u4fee\u6539\u3002\nx[0]=&#039;S&#039;    \/\/ \u4e4b\u6240\u4ee5\u7528&#039;S&#039;\u662f\u56e0\u4e3a\u5355\u5f15\u53f7\u4ee3\u8868\u7684\u662fint32\uff0c\u6b64\u5904\u5176\u5b9e\u5c31\u662f\u5c06\u4e00\u4e2a\u6570\u5b57\u8d4b\u503c\u7ed9\u4e86x[0],\u5982\u679c\u7528&quot;S&quot;\u5219\u4ee3\u8868\u7684\u662f\u4e00\u4e2a\u5b57\u8282\u6570\u7ec4\uff0c\u7528\u53cc\u5f15\u53f7\u7684&quot;S&quot;\u5c31\u4e0d\u5bf9\u4e86\u3002\nx[1]=&#039;B&#039;\n\nx[8]=&#039;\\xe5&#039; \/\/ \u66ff\u6362\u4e3a\u4e2d\u6587\u5b57\u7b26&quot;\u5c4e&quot;\u7684\u7b2c1\u4e2autf-8\u7801\u70b9\nx[9]=&#039;\\xb1&#039; \/\/ \u66ff\u6362\u4e3a\u4e2d\u6587\u5b57\u7b26&quot;\u5c4e&quot;\u7684\u7b2c2\u4e2autf-8\u7801\u70b9\nx[10]=&#039;\\x8e&#039; \/\/ \u66ff\u6362\u4e3a\u4e2d\u6587\u5b57\u7b26&quot;\u5c4e&quot;\u7684\u7b2c3\u4e2autf-8\u7801\u70b9\nfmt.Println(x) \/\/ [83 66 229 144 131 233 184 161 229 177 142]\n\nfmt.Println(string(x)) \/\/ SB\u5403\u9e21\u5c4e\n\nx[len(x)-1]=&#039;a&#039;\nfmt.Println(string(x)) \/\/ SB\u5403\u9e21\ufffda<\/code><\/pre>\n<p>\u7ec3\u4e60\u98983<\/p>\n<pre><code class=\"language-go\">s := &quot;\u30d7\u30ed\u30b0\u30e9\u30e0&quot;\nfmt.Printf(&quot;% x\\n&quot;, s) \/\/ &quot;e3 83 97 e3 83 ad e3 82 b0 e3 83 a9 e3 83 a0&quot;\nr := []rune(s)\nfmt.Printf(&quot;%x\\n&quot;, r)  \/\/ &quot;[30d7 30ed 30b0 30e9 30e0]&quot;\n\nfmt.Println(string(r)) \/\/ &quot;\u30d7\u30ed\u30b0\u30e9\u30e0&quot;<\/code><\/pre>\n<p>\u7ec3\u4e60\u98984\uff1a<\/p>\n<pre><code class=\"language-go\">fmt.Println(string(65))     \/\/ \u6ce8\u610f\u5f97\u5230\u7684\u662f\u5b57\u7b26&quot;A&quot;, \u800c\u4e0d\u662f &quot;65&quot;\nfmt.Println(string(0x4eac)) \/\/ &quot;\u4eac&quot;<\/code><\/pre>\n<p>\u7ec3\u4e60\u98985\uff1a\u5982\u679c\u5bf9\u5e94\u7801\u70b9\u7684\u5b57\u7b26\u662f\u65e0\u6548\u7684\uff0c\u5219\u7528&#8217;\\uFFFD&#8217;\u65e0\u6548\u5b57\u7b26\u4f5c\u4e3a\u66ff\u6362\uff1a<\/p>\n<pre><code class=\"language-go\">fmt.Println(string(1234567)) \/\/ &quot;(?)&quot;<\/code><\/pre>\n<p>\u7ec3\u4e60\u98986\uff1a\u5c06\u4e00\u4e2a\u6574\u6570\u8f6c\u578b\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/p>\n<pre><code class=\"language-go\">fmt.Println(string(65))     \/\/ \u6ce8\u610f\u5f97\u5230\u7684\u662f\u5b57\u7b26&quot;A&quot;, \u800c\u4e0d\u662f &quot;65&quot;\nfmt.Println(string(0x4eac)) \/\/ &quot;\u4eac&quot;<\/code><\/pre>\n<h3>5.5 \u4e8c\u5143\u8fd0\u7b97<\/h3>\n<p>\u5b57\u7b26\u4e32\u53ea\u80fd\u4e0e\u5b57\u7b26\u4e32\u4e4b\u95f4\u8fdb\u884c\u8fd0\u7b97<\/p>\n<p>\uff081\uff09\u76f8\u7b49\u6027\u5224\u65ad\uff1a\u5b57\u7b26\u4e32\u53ea\u80fd\u4e0e\u65e0\u7c7b\u578b\/\u6709\u7c7b\u578b\u7684\u5b57\u7b26\u4e32\u6bd4\u8f83\u662f\u5426\u76f8\u7b49<\/p>\n<p>\uff082\uff09\u6bd4\u5927\u5c0f\uff1a\u5b57\u7b26\u4e32\u4e4b\u95f4\u4e5f\u53ef\u4ee5\u6bd4\u5927\u5c0f\uff0c\u5e95\u5c42\u901a\u8fc7\u9010\u4e2a\u5b57\u8282\u6bd4\u8f83\u5b8c\u6210\u7684\uff0c\u5982\u4e0b\u6240\u793a<\/p>\n<pre><code class=\"language-go\">var x string = &quot;egon\u4f1a&quot;\nvar y string = &quot;egon\u98de&quot;\n\n\/\/ \u5e95\u5c42\nfmt.Println([]byte(x)) \/\/ [101 103 111 110 228 188 154]\nfmt.Println([]byte(y)) \/\/ [101 103 111 110 233 163 158]\n\n\/\/ \u6309\u6b21\u5e8f\u6bd4\u8f83\u5b57\u8282\nfmt.Println(x &gt; y)  \/\/ true\nfmt.Println(x &lt; y)  \/\/ false<\/code><\/pre>\n<p>\uff083\uff09\u76f8\u52a0\uff1a\u62fc\u63a5\u5b57\u7b26\u4e32<\/p>\n<pre><code class=\"language-go\">fmt.Println(&quot;hello&quot; + &quot;egon&quot;)  \/\/ helloegon\n\n\/\/ \u6ce8\u610f\uff1a\u4e0d\u80fd\u4e58\nfmt.Println(&quot;hello&quot;*3)         \/\/ \u9519\u8bef<\/code><\/pre>\n<h3>5.6  \u904d\u5386\u5b57\u7b26\u4e32\u7684\u4e24\u79cd\u65b9\u5f0f<\/h3>\n<p>\u4f9d\u8d56\u7d22\u5f15<\/p>\n<pre><code class=\"language-go\">\u4e3a\u4e86\u5904\u7406\u8fd9\u4e9b\u771f\u5b9e\u7684\u5b57\u7b26\uff08\u5373\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u7684unicode\u5b57\u7b26\uff09\uff0c\u6211\u4eec\u9700\u8981\u4e00\u4e2aUTF8\u89e3\u7801\u5668\u3002unicode\/utf8\u5305\u63d0\u4f9b\u4e86\u8be5\u529f\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528\uff1a\n\ns := &quot;Hello, \u4e16\u754c&quot;\nfor i := 0; i &lt; len(s); {\n    r, size := utf8.DecodeRuneInString(s[i:]) \/\/ r\u5bf9\u5e94\u5b57\u7b26\u672c\u8eab\uff0csize\u4ee3\u8868utf-8\u7f16\u7801\u5b57\u7b26r\u7684\u5b57\u8282\u4e2a\u6570\n    fmt.Printf(&quot;%d\\t%c\\n&quot;, i, r)\n    i += size \/\/ \u66f4\u65b0\u7d22\u5f15\u6307\u5411\u4e0b\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\n}\n\/\/ \u8f93\u51fa\n0       H\n1       e\n2       l\n3       l\n4       o\n5       ,\n6        \n7       \u4e16\n10      \u754c<\/code><\/pre>\n<p>\u4f9d\u8d56range<\/p>\n<pre><code class=\"language-go\">\u4f46\u662f\u4e0a\u8ff0\u4f9d\u8d56\u7d22\u5f15\u7684\u8fd9\u79cd\u65b9\u5f0f\u662f\u7b28\u62d9\u7684\uff0c\u6211\u4eec\u9700\u8981\u66f4\u7b80\u6d01\u7684\u8bed\u6cd5\u3002\u5e78\u8fd0\u7684\u662f\uff0cGo\u8bed\u8a00\u7684range\u5faa\u73af\u5728\u5904\u7406\u5b57\u7b26\u4e32\u7684\u65f6\u5019\uff0c\u4f1a\u81ea\u52a8\u9690\u5f0f\u89e3\u7801UTF8\u5b57\u7b26\u4e32\u3002\n\nfor i, r := range &quot;Hello, \u4e16\u754c&quot; {\n    fmt.Printf(&quot;%d\\t%q\\t%d\\n&quot;, i, r, r) \n}\n\/\/ \u8f93\u51fa\n0       &#039;H&#039;     72\n1       &#039;e&#039;     101\n2       &#039;l&#039;     108\n3       &#039;l&#039;     108\n4       &#039;o&#039;     111\n5       &#039;,&#039;     44\n6       &#039; &#039;     32\n7       &#039;\u4e16&#039;    19990 \/\/ \u4e2d\u6587\u5b57\u7b26\u201d\u4e16\u201c\u7684\u4e09\u4e2a\u5b57\u8282\u88ab\u5408\u5e76\u5230\u4e86\u4e00\u8d77\uff0c\u7136\u540e\u88ab\u89e3\u7801\n10      &#039;\u754c&#039;    30028\n\n\u4e0a\u8ff0range\u5faa\u73af\u8fd0\u884c\u5982\u56fe\u6240\u793a\uff1b\u9700\u8981\u6ce8\u610f\u7684\u662f\u5bf9\u4e8e\u975eASCII\uff0c\u7d22\u5f15\u66f4\u65b0\u7684\u6b65\u957f\u5c06\u8d85\u8fc71\u4e2a\u5b57\u8282\u3002<\/code><\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='.\/img\/range\u5faa\u73af\u5b57\u7b26.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\".\/img\/range\u5faa\u73af\u5b57\u7b26.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20200529233600074\" \/><\/div><\/p>\n<p><strong>\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u4e2a\u6570<\/strong><\/p>\n<p>\u57fa\u4e8e\u4e0a\u8ff0range\u5faa\u73af\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u7b80\u5355\u7684\u5faa\u73af\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u6570\u76ee\uff0c\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre><code class=\"language-Go\">s:=&quot;Hello, \u4e16\u754c&quot;\nn := 0\nfor _, _ = range s {\n    n++\n}\nfmt.Println(n) \/\/ 9<\/code><\/pre>\n<p>\u50cf\u5176\u5b83\u5f62\u5f0f\u7684\u5faa\u73af\u90a3\u6837\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5ffd\u7565\u4e0d\u9700\u8981\u7684\u53d8\u91cf\uff1a<\/p>\n<pre><code class=\"language-Go\">s:=&quot;Hello, \u4e16\u754c&quot;\nn := 0\nfor range s {\n    n++\n}\nfmt.Println(n) \/\/ 9<\/code><\/pre>\n<p>\u6216\u8005\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528utf8.RuneCountInString(s)\u51fd\u6570\u3002<\/p>\n<pre><code class=\"language-go\">s:=&quot;Hello, \u4e16\u754c&quot;\nfmt.Println(utf8.RuneCountInString(s)) \/\/ 9<\/code><\/pre>\n<h2>\u516d \u5176\u4ed6\u5b57\u7b26\u4e32\u5e38\u7528\u64cd\u4f5c<\/h2>\n<p><strong>1\u3001fmt.Printf\u683c\u5f0f\u5316\u6253\u5370\u5b57\u7b26\u4e32<\/strong><\/p>\n<pre><code class=\"language-go\">\/\/ \u5b57\u7b26\u4f7f\u7528`%c`\u53c2\u6570\u6253\u5370\uff0c\u6216\u8005\u662f\u7528`%q`\u53c2\u6570\u6253\u5370\u5e26\u5355\u5f15\u53f7\u7684\u5b57\u7b26\uff1a\nascii := &#039;a&#039;\nunicode := &#039;\u56fd&#039;\nnewline := &#039;\\n&#039;\nfmt.Printf(&quot;%d %[1]c %[1]q\\n&quot;, ascii)   \/\/ 97 a &#039;a&#039;\nfmt.Printf(&quot;%d %[1]c %[1]q\\n&quot;, unicode) \/\/ 22269 \u56fd &#039;\u56fd&#039;\nfmt.Printf(&quot;%d %[1]q\\n&quot;, newline)       \/\/ 10 &#039;\\n&#039;\n\n\/\/ \u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65f6\u5e38\u7528\u7684\u52a8\u8bcd\u53ca\u529f\u80fd\u8868\u5982\u4e0b<\/code><\/pre>\n<table>\n<thead>\n<tr>\n<th>\u52a8  \u8bcd<\/th>\n<th>\u529f  \u80fd<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>%v<\/td>\n<td>\u6309\u503c\u7684\u672c\u6765\u503c\u8f93\u51fa<\/td>\n<\/tr>\n<tr>\n<td>%+v<\/td>\n<td>\u5728 %v \u57fa\u7840\u4e0a\uff0c\u5bf9\u7ed3\u6784\u4f53\u5b57\u6bb5\u540d\u548c\u503c\u8fdb\u884c\u5c55\u5f00<\/td>\n<\/tr>\n<tr>\n<td>%#v<\/td>\n<td>\u8f93\u51fa Go \u8bed\u8a00\u8bed\u6cd5\u683c\u5f0f\u7684\u503c<\/td>\n<\/tr>\n<tr>\n<td>%T<\/td>\n<td>\u8f93\u51fa Go \u8bed\u8a00\u8bed\u6cd5\u683c\u5f0f\u7684\u7c7b\u578b\u548c\u503c<\/td>\n<\/tr>\n<tr>\n<td>%%<\/td>\n<td>\u8f93\u51fa % \u672c\u4f53<\/td>\n<\/tr>\n<tr>\n<td>%b<\/td>\n<td>\u6574\u578b\u4ee5\u4e8c\u8fdb\u5236\u65b9\u5f0f\u663e\u793a<\/td>\n<\/tr>\n<tr>\n<td>%o<\/td>\n<td>\u6574\u578b\u4ee5\u516b\u8fdb\u5236\u65b9\u5f0f\u663e\u793a<\/td>\n<\/tr>\n<tr>\n<td>%d<\/td>\n<td>\u6574\u578b\u4ee5\u5341\u8fdb\u5236\u65b9\u5f0f\u663e\u793a<\/td>\n<\/tr>\n<tr>\n<td>%x<\/td>\n<td>\u6574\u578b\u4ee5\u5341\u516d\u8fdb\u5236\u65b9\u5f0f\u663e\u793a<\/td>\n<\/tr>\n<tr>\n<td>%X<\/td>\n<td>\u6574\u578b\u4ee5\u5341\u516d\u8fdb\u5236\u3001\u5b57\u6bcd\u5927\u5199\u65b9\u5f0f\u663e\u793a<\/td>\n<\/tr>\n<tr>\n<td>%U<\/td>\n<td>Unicode \u5b57\u7b26<\/td>\n<\/tr>\n<tr>\n<td>%f<\/td>\n<td>\u6d6e\u70b9\u6570<\/td>\n<\/tr>\n<tr>\n<td>%p<\/td>\n<td>\u6307\u9488\uff0c\u5341\u516d\u8fdb\u5236\u65b9\u5f0f\u663e\u793a<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>2\u3001\u5b57\u7b26\u4e32\u548c\u6570\u5b57\u7684\u8f6c\u6362<\/strong><\/p>\n<p>\u9664\u4e86\u5b57\u7b26\u4e32\u3001\u5b57\u7b26\u3001\u5b57\u8282\u4e4b\u95f4\u7684\u8f6c\u6362\uff0c\u5b57\u7b26\u4e32\u548c\u6570\u503c\u4e4b\u95f4\u7684\u8f6c\u6362\u4e5f\u6bd4\u8f83\u5e38\u89c1\uff0c\u9700\u8981\u7528\u5230strconv\u5305\u63d0\u4f9b\u7684\u529f\u80fd\u3002<\/p>\n<ul>\n<li>2.1 \u5c06\u4e00\u4e2a\u6574\u6570\u8f6c\u4e3a\u5b57\u7b26\u4e32<\/li>\n<\/ul>\n<pre><code class=\"language-Go\">\/\/ \u65b9\u6cd51\uff1a\u7528fmt.Sprintf\u8fd4\u56de\u4e00\u4e2a\u683c\u5f0f\u5316\u7684\u5b57\u7b26\u4e32\nx:=123\nx_new := fmt.Sprintf(&quot;%d&quot;, x)\nfmt.Printf(&quot;%[1]T,%[1]v\\n&quot;,x_new) \/\/ string,123\n\n\/\/ \u65b9\u6cd52\uff1a\u7528strconv.Itoa(\u201c\u6574\u6570\u5230ASCII\u201d)\uff1a\ny:=456\ny_new := fmt.Sprintf(&quot;%d&quot;, y)\nfmt.Printf(&quot;%[1]T,%[1]v\\n&quot;,y_new) \/\/ string,456<\/code><\/pre>\n<ul>\n<li>2.2 \u5c06\u4e00\u4e2a\u6574\u6570\u8f6c\u4e3a\u5176\u4ed6\u8fdb\u5236\uff0c\u5e76\u4e14\u7ed3\u679c\u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b<\/li>\n<\/ul>\n<pre><code class=\"language-Go\">\/\/ \u65b9\u6cd51\uff1astrconv\u5305\u5185\u7684Format\u51fd\u6570\u5982FormatInt\u548cFormatUint\u51fd\u6570\u53ef\u4ee5\u7528\u4e0d\u540c\u7684\u8fdb\u5236\u6765\u683c\u5f0f\u5316\u6570\u5b57\nx:=123\nres:=strconv.FormatInt(int64(x), 2) \/\/ 10\u8fdb\u5236\u6574\u578b\u8f6c\u62102\u8fdb\u5236,\u7ed3\u679c\u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b\uff1a&quot;1111011&quot;\nfmt.Printf(&quot;%[1]T,%[1]v\\n&quot;,res) \/\/ string,1111011\n\n\/\/ \u65b9\u6cd52\uff1afmt.Sprintf\u7ed3\u5408\u52a8\u8bcd%b\u3001%d\u3001%o\u548c%x\u7b49\u53c2\u6570\uff0c\u5728\u9700\u8981\u9644\u52a0\u989d\u5916\u7684\u4fe1\u606f\u65f6\u53ef\u4ee5\u7528\u8be5\u65b9\u6cd5\uff0c\u4f1a\u6bd4\u65b9\u6cd51\u66f4\u597d\u7528\ns := fmt.Sprintf(&quot;res=%b&quot;, x) \/\/ &quot;res=1111011&quot;<\/code><\/pre>\n<ul>\n<li>2.3 \u5b57\u7b26\u4e32\u7c7b\u578b\u89e3\u6790\u4e3a\u6574\u6570<\/li>\n<\/ul>\n<pre><code class=\"language-go\">\/\/ 1\u3001\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u6574\u6570\uff0c\u7ed3\u679c\u4e3aint\u7c7b\u578b\nx, err := strconv.Atoi(&quot;123&quot;)             \/\/ x is an int\n\n\/\/ 2\u3001\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u6574\u6570\uff0c\u7ed3\u679c\u4e3aint64\u7c7b\u578b\ny, err := strconv.ParseInt(&quot;123&quot;, 10, 64) \/\/ base 10, up to 64 bits\n\nParseInt\u51fd\u6570\u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u7528\u4e8e\u6307\u5b9a\u6574\u578b\u6570\u7684\u5927\u5c0f\uff1b\u4f8b\u598216\u8868\u793aint16\uff0c0\u5219\u8868\u793aint\u3002\u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\uff0c\u8fd4\u56de\u7684\u7ed3\u679cy\u603b\u662fint64\u7c7b\u578b\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u5c06\u5b83\u8f6c\u4e3a\u66f4\u5c0f\u7684\u6574\u6570\u7c7b\u578b\u3002\n\n\/\/ 3\u3001\u8fd8\u6709\u7528\u4e8e\u89e3\u6790\u65e0\u7b26\u53f7\u6574\u6570\u7684ParseUint\u51fd\u6570(\u4e86\u89e3)<\/code><\/pre>\n<p><strong>3\u3001\u5176\u4ed6<\/strong><\/p>\n<pre><code class=\"language-go\">\/\/ \u6c42\u957f\u5ea6\nlen(str) \u6c42\u957f\u5ea6\uff08\u5b57\u8282\u4e2a\u6570\uff0c\u6587\u672c\u5b57\u7b26\u91c7\u7528\u7684\u662futf-8\u7f16\u7801\uff09\n\u4f8b\u5982\uff1alen(&quot;a\u4f60&quot;) \u7ed3\u679c\u4e3a4\n\nutf8.RuneCountInString(s)\u6c42\u957f\u5ea6\uff08\u5b57\u7b26\u4e2a\u6570\uff0c\u975e\u6cd5\u5b57\u7b26\u94bb\u77f3\u95ee\u53f7\u4e5f\u7b97\u5b57\u7b26\uff09\n\u4f8b\u5982\uff1autf8.RuneCountInString(&quot;\\xff\u4f60\u597dhi&quot;) \u7ed3\u679c\u4e3a5\n\n\/\/ \u5b57\u7b26\u4e32\u62fc\u63a5\n+\u6216fmt.Sprintf\u62fc\u63a5\u5b57\u7b26\u4e32\n\u4f8b\u5982\uff1ad := fmt.Sprintf(&quot;%v - %v - %v&quot;, &quot;2020&quot;, &quot;05&quot;, 20) \u7ed3\u679c\u4e3a\uff1a2020 - 05 - 20\n\n\/\/ strings\nstrings.HasPrefix(s string,preffix string) bool:\n\u5224\u65ad\u5b57\u7b26\u4e32s\u662f\u5426\u4ee5prefix\u5f00\u5934\n\nstirngs.HasSuffix(s string,suffix string) bool:\n\u5224\u65ad\u5b57\u7b26\u4e32s\u662f\u5426\u4ee5suffix\u7ed3\u5c3e\n\nstrings.Index(s string,str string) int:\n\u5224\u65adstr\u5728s\u4e2d\u9996\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\uff0c\u5982\u679c\u6ca1\u6709\u51fa\u73b0\uff0c\u5219\u8fd4\u56de-1\n\nstrings.LastIndex(s string,str string) int:\n\u5224\u65adstr\u5728s\u4e2d\u6700\u540e\u51fa\u73b0\u7684\u4f4d\u7f6e\uff0c\u5982\u679c\u6ca1\u6709\u51fa\u73b0\uff0c\u5219\u8fd4\u56de-1\n\nstrings.Replace(str string,old string,new string,n int):\n\u5b57\u7b26\u4e32\u66ff\u6362\n\nstrings.Count(str string,count int)string:\n\u5b57\u7b26\u4e32\u8ba1\u6570\n\nstrings.Repeat(str string,count int) string:\n\u91cd\u590dcount\u6b21str\n\nstrings.ToLower(str string)\n\u8f6c\u6362\u4e3a\u5c0f\u5199\n\nstrings.ToUpper(str string)string:\n\u8f6c\u6362\u4e3a\u5927\u5199\n\nstrings.TrimSpace(str string):\n\u53bb\u6389\u5b57\u7b26\u4e32\u9996\u4f4d\u7a7a\u767d\u5b57\u7b26\n\nstrings.Trim(str string,cut string):\n\u53bb\u6389\u5b57\u7b26\u4e32\u9996\u5c3ecut\u5b57\u7b26\n\nstrings.TrimLeft(str string,cut string):\n\u53bb\u6389\u5b57\u7b26\u4e32\u9996\u90e8cut\u5b57\u7b26\n\nstrings.TrimRight(str string,cunt string):\n\u53bb\u6389\u5b57\u7b26\u4e32\u5c3e\u90e8cut\u5b57\u7b26\n\nstrings.Field(str string):\n\u8fd4\u56destr\u7a7a\u683c\u5206\u9694\u7684\u6240\u6709\u5b50\u4e32\u7684slice\n\nstring.Split(str string,split string):\n\u8fd4\u56destr split\u5206\u5272\u7684\u6240\u6709\u5b50\u4e32\u7684slice\n\u4f8b\u5982\uff1astrings.Split(&quot;egon:18:male&quot;,&quot;:&quot;)\u7ed3\u679c\u4e3a\u5207\u7247\uff1a[egon 18 male]\n\nstrings.Join(s1 []string,sep string):\n\u7528sep\u628as1\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8fde\u63a5\u8d77\u6765\n\u4f8b\u5982\uff1astrings.Join([]string{&quot;egon&quot;,&quot;18&quot;,&quot;male&quot;},&quot;:&quot;)\u7ed3\u679c\u4e3aegon:18:male\n\nstrings.contains\n\u5224\u65ad\u662f\u5426\u5305\u542b\n\u4f8b\u5982\uff1astrings.Contains(&quot;abcdef&quot;,&quot;abc&quot;)\u7ed3\u679c\u4e3atrue\n\n\/\/ strconv\nscronv.Itoa(i int):\u628a\u4e00\u4e2a\u6574\u6570\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\n\nscronv.Atio(str string)(int,errror):\n\u628a\u4e00\u4e2a\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6574\u6570\n<\/code><\/pre>\n<p>\u6269\u5c55\u9605\u8bfb<\/p>\n<pre><code class=\"language-go\">\u5f97\u76ca\u4e8eUTF8\u7f16\u7801\u4f18\u826f\u7684\u8bbe\u8ba1\uff0c\u8bf8\u591a\u5b57\u7b26\u4e32\u64cd\u4f5c\u90fd\u4e0d\u9700\u8981\u89e3\u7801\u64cd\u4f5c\u3002\u6211\u4eec\u53ef\u4ee5\u4e0d\u7528\u89e3\u7801\u76f4\u63a5\u6d4b\u8bd5\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u662f\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u524d\u7f00\uff1a\n\n\u200b```Go\nfunc HasPrefix(s, prefix string) bool {\n    return len(s) &gt;= len(prefix) &amp;&amp; s[:len(prefix)] == prefix\n}\n\u200b```\n\n\u6216\u8005\u662f\u540e\u7f00\u6d4b\u8bd5\uff1a\n\n\u200b```Go\nfunc HasSuffix(s, suffix string) bool {\n    return len(s) &gt;= len(suffix) &amp;&amp; s[len(s)-len(suffix):] == suffix\n}\n\u200b```\n\n\u6216\u8005\u662f\u5305\u542b\u5b50\u4e32\u6d4b\u8bd5\uff1a\n\n\u200b```Go\nfunc Contains(s, substr string) bool {\n    for i := 0; i &lt; len(s); i++ {\n        if HasPrefix(s[i:], substr) {\n            return true\n        }\n    }\n    return false\n}\n\u200b```\n\n\u5bf9\u4e8eUTF8\u7f16\u7801\u540e\u6587\u672c\u7684\u5904\u7406\u548c\u539f\u59cb\u7684\u5b57\u8282\u5904\u7406\u903b\u8f91\u662f\u4e00\u6837\u7684\u3002\u4f46\u662f\u5bf9\u5e94\u5f88\u591a\u5176\u5b83\u7f16\u7801\u5219\u5e76\u4e0d\u662f\u8fd9\u6837\u7684\u3002\uff08\u4e0a\u9762\u7684\u51fd\u6570\u90fd\u6765\u81eastrings\u5b57\u7b26\u4e32\u5904\u7406\u5305\uff0c\u771f\u5b9e\u7684\u4ee3\u7801\u5305\u542b\u4e86\u4e00\u4e2a\u7528\u54c8\u5e0c\u6280\u672f\u4f18\u5316\u7684Contains \u5b9e\u73b0\u3002<\/code><\/pre>\n<p>\u6807\u51c6\u5e93\u4e2d\u6709\u56db\u4e2a\u5305\u5bf9\u5b57\u7b26\u4e32\u5904\u7406\u5c24\u4e3a\u91cd\u8981\uff1abytes\u3001strings\u3001strconv\u548cunicode\u5305\u3002strings\u5305\u63d0\u4f9b\u4e86\u8bb8\u591a\u5982\u5b57\u7b26\u4e32\u7684\u67e5\u8be2\u3001\u66ff\u6362\u3001\u6bd4\u8f83\u3001\u622a\u65ad\u3001\u62c6\u5206\u548c\u5408\u5e76\u7b49\u529f\uff0c\u540e\u7eed\u5c06\u4f1a\u4ecb\u7ecd\u3002<\/p>\n<h2>\u4e03 \u5728\u51fd\u6570\u53c2\u6570\u4e2d\u4f20\u9012<\/h2>\n<p>\u672c\u8282\u6211\u4eec\u5176\u5b9e\u5b66\u4e86\u4e24\u79cd\u7c7b\u578b\uff1a\u5b57\u7b26\u4e32\u7c7b\u578bstring\u4e0e\u5b57\u7b26\u7c7b\u578brune<\/p>\n<p>\u4e8c\u8005\u5728\u51fd\u6570\u53c2\u6570\u4e2d\u7684\u4f20\u9012\u90fd\u662f\u503c\u4f20\u9012<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e4b\u5b57\u7b26\u4e32 \u4e00 \u5b57\u7b26\u4e32\u5e95\u5c42\u662f\u4ec0\u4e48 \u200b \u5728 C\/C++\u8bed\u8a00\u4e2d\uff0c\u5e76\u4e0d\u5b58\u5728\u539f\u751f\u7684\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u5b83\u4eec\u901a\u5e38\u4f7f\u7528\u5b57 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,449],"tags":[],"_links":{"self":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/7921"}],"collection":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7921"}],"version-history":[{"count":0,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/7921\/revisions"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}