{"id":10006487,"date":"2022-09-15T12:43:20","date_gmt":"2022-09-15T04:43:20","guid":{"rendered":"https:\/\/ityet.com\/?p=10006487"},"modified":"2022-09-15T12:43:20","modified_gmt":"2022-09-15T04:43:20","slug":"%e3%80%901-3-golang%e3%80%91go%e8%af%ad%e8%a8%80%e5%bf%ab%e9%80%9f%e5%85%a5%e9%97%a8-%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006487","title":{"rendered":"\u30101-3 Golang\u3011Go\u8bed\u8a00\u5feb\u901f\u5165\u95e8\u2014\u5b57\u7b26\u4e32"},"content":{"rendered":"<p>&emsp;&emsp;Go\u8bed\u8a00\u5b57\u7b26\u4e32\u7684\u7528\u6cd5\u8fd8\u662f\u6bd4\u8f83\u7b80\u5355\u7684\uff0c\u5e38\u7528\u4e5f\u5c31\u662f\u5b57\u7b26\u4e32\u76f8\u52a0\uff0c\u5b57\u7b26\u4e32\u4e0ebyte\u5207\u7247\u3001rune\u5207\u7247\u4e92\u76f8\u8f6c\u6362\uff0c\u5b57\u7b26\u4e32\u8f93\u51fa\u7b49\u7b49\u64cd\u4f5c\u3002\u90a3\u6709\u4ec0\u4e48\u53ef\u5b66\u7684\u5462\uff1f\u5176\u5b9e\u8fd8\u662f\u6709\u4e00\u4e9b\u7ec6\u8282\u9700\u8981\u5173\u6ce8\uff0c\u6bd4\u5982\u5b57\u7b26\u4e32&#8221;\u53ea\u8bfb&#8221;\u7279\u6027\uff0c\u5b57\u7b26\u4e32\u7f16\u7801\u7b49\u7b49\u3002<\/p>\n<h2>\u57fa\u672c\u64cd\u4f5c<\/h2>\n<p>&emsp;&emsp;\u5b57\u7b26\u4e32\u53ea\u8bfb\uff1f\u662f\u7684\uff0c\u5c31\u662f\u4f60\u60f3\u7684\u90a3\u6837\uff0c\u53ea\u8bfb\u5c31\u662f\u4e0d\u80fd\u4fee\u6539\u7684\u610f\u601d\u3002\u90a3\u4e0b\u9762\u7a0b\u5e8f\u600e\u4e48\u89e3\u91ca\u5462\uff1f<\/p>\n<pre><code>package main\n\nimport &quot;fmt&quot;\n\nfunc main() {\n    str := &quot;hello&quot;\n    str += &quot; world&quot;\n    fmt.Println(str)   \/\/hello world\n}<\/code><\/pre>\n<p>&emsp;&emsp;\u770b\u5230\u4e86\u5427\uff0c\u6211\u786e\u5b9e\u6539\u53d8\u4e86\u5b57\u7b26\u4e32str\u7684\u503c\u3002\u662f\u7684\uff0c\u5b57\u7b26\u4e32str\u786e\u5b9e\u6539\u53d8\u4e86\uff0c\u800c\u5b57\u7b26\u4e32\u786e\u5b9e\u4e5f\u662f\u53ea\u8bfb\u7684\uff1b\u8fd9\u91cc\u53ef\u80fd\u5b58\u5728\u4e00\u4e9b\u6b67\u4e49\uff0c\u51c6\u5907\u7684\u8bf4\uff0c\u5e94\u8be5\u662f\u5b57\u7b26\u4e32\u53d8\u91cfstr\u6307\u5411\u4e86\u65b0\u7684\u5b57\u7b26\u4e32\u3002\u5b57\u7b26\u4e32&#8221;hello&#8221;\u5e76\u6ca1\u6709\u6539\u53d8\uff0c\u53ea\u662f\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32&#8221;hello world&#8221;\uff0c\u540c\u65f6\u8ba9\u5b57\u7b26\u4e32\u53d8\u91cfstr\u6307\u5411\u8fd9\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u3002\u8fd8\u6709\u4e00\u4e2a\u65b9\u6cd5\u9a8c\u8bc1\u8fd9\u4e2a\u8bf4\u6cd5\uff1a<\/p>\n<pre><code>go tool compile -S -N -l test.go\n\ngo.string.&quot;hello&quot; SRODATA dupok size=5\n    0x0000 68 65 6c 6c 6f                                   hello\ngo.string.&quot; world&quot; SRODATA dupok size=6\n    0x0000 20 77 6f 72 6c 64                                 world<\/code><\/pre>\n<p>&emsp;&emsp;go.string.&#8221;hello&#8221;\u6240\u5c5e\u5185\u5b58\u533a\u57df\u662fSRODATA\uff0cRO\u5c31\u662fread only\u53ea\u8bfb\u7684\u610f\u601d\u3002\u518d\u4e3e\u4e00\u4e2a\u4e8b\u4f8b\uff1a\u5b57\u7b26\u4e32\u4e0d\u80fd\u6309\u7167\u7d22\u5f15\u64cd\u4f5c\uff0c\u5982\u679c\u5c06\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3abyte\u5207\u7247\uff0c\u6309\u7406\u8bf4byte\u5207\u7247\u4e0e\u5b57\u7b26\u4e32\u5e95\u5c42\u6570\u636e\u5e94\u8be5\u5171\u7528\uff0c\u90a3\u4e48\u4fee\u6539\u8be5byte\u5207\u7247\uff0c\u5b57\u7b26\u4e32\u4e5f\u5e94\u8be5\u540c\u6b65\u6539\u53d8\u3002<\/p>\n<pre><code>package main\n\nimport &quot;fmt&quot;\n\nfunc main() {\n    str := &quot;hello world&quot;\n    \/\/\u5b57\u7b26\u4e32\u8f6c\u5316\u4e3abyte\u5207\u7247\uff0c\u4fee\u6539\u5207\u7247\u5143\u7d20\n    b := []byte(str)\n    b[0] = 69\n\n    fmt.Println(str)          \/\/hello world\n    for _, c := range b {\n        fmt.Printf(&quot;%c&quot;, c)   \/\/Eello world\n    }\n}<\/code><\/pre>\n<p>&emsp;&emsp;byte\u5207\u7247\u786e\u5b9e\u88ab\u4fee\u6539\u4e86\uff0c\u800c\u5b57\u7b26\u4e32\u53d8\u91cfstr\u5374\u6ca1\u6709\u6539\u53d8\uff0c\u4e3a\u4ec0\u4e48\u5462\uff1f\u56e0\u4e3a\u5b57\u7b26\u4e32\u662f\u53ea\u8bfb\u7684\uff0c\u6240\u4ee5\u5728[]byte(str)\u5f3a\u5236\u7c7b\u578b\u8f6c\u5316\u65f6\uff0c\u4f1a\u6267\u884c\u4e86\u6570\u636e\u7684\u62f7\u8d1d\uff0c\u907f\u514d\u4fee\u6539byte\u5207\u7247\u5f71\u54cd\u539f\u5b57\u7b26\u4e32\u3002<\/p>\n<p>&emsp;&emsp;\u6700\u540e\u5728\u4f7f\u7528\u5b57\u7b26\u4e32\u65f6\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\u4e00\u4e2a\u95ee\u9898\uff1alen\u7528\u4e8e\u83b7\u53d6\u5b57\u7b26\u4e32\u957f\u5ea6\uff0c\u7eaf\u82f1\u6587\u5b57\u7b26\u4e32\u4e00\u5207\u6b63\u5e38\uff0c\u4f46\u662f\u5f53\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u4e2d\u6587\u65f6\uff0c\u60c5\u51b5\u5c31\u6709\u4e9b\u4e0d\u540c\u4e86\u3002<\/p>\n<pre><code>package main\n\nimport &quot;fmt&quot;\n\nfunc main() {\n    str := &quot;Go\u8bed\u8a00\u8fd8\u662f\u633a\u4e0d\u9519\u7684&quot;\n    fmt.Println(len(str))   \/\/26\n}<\/code><\/pre>\n<p>&emsp;&emsp;str\u5b57\u7b26\u4e32\u5305\u542b2\u4e2a\u82f1\u6587\u5b57\u6bcd\uff0c8\u4e2a\u4e2d\u6587\u6c49\u5b57\uff0c\u8f93\u51fa\u663e\u793a\u5b57\u7b26\u4e32\u957f\u5ea6\u662f26\u3002\u8fd9\u5c31\u6709Go\u8bed\u8a00\u5b57\u7b26\u4e32\u7f16\u7801\u6709\u5173\u4e86\uff0cGo\u8bed\u8a00\u5b57\u7b26\u4e32\u91c7\u53d6utf-8\u7f16\u7801\uff0c\u4e00\u4e2a\u4e2d\u6587\u6c49\u5b57\u53603\u4e2a\u5b57\u8282\uff0c\u6240\u4ee5\u7b97\u4e0b\u6765\u5b57\u7b26\u4e32\u957f\u5ea6\u5c31\u662f26\u4e86\u3002\u90a3\u786e\u5b9e\u60f3\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u6570\u76ee\u5462\uff1f\u53ef\u901a\u8fc7\u4e0b\u9762\u65b9\u5f0f\uff1a<\/p>\n<pre><code>package main\n\nimport &quot;fmt&quot;\n\nfunc main() {\n    str := &quot;Go\u8bed\u8a00\u8fd8\u662f\u633a\u4e0d\u9519\u7684&quot;\n\n    r := []rune(str)   \/\/rune\u5176\u5b9e\u5c31\u662fint32\uff0c4\u5b57\u8282\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\uff1br\u76f8\u5f53\u4e8e\u5b57\u7b26\u5207\u7247\n    fmt.Println(len(r))\n\n    n := 0\n    for _, _ = range str {    \/\/range\u904d\u5386\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\u5b57\u7b26\u7d22\u5f15\uff0c\u4e0e\u5f53\u524d\u5b57\u7b26\n        n ++\n    }\n    fmt.Println(n)\n}<\/code><\/pre>\n<h2>\u5b9e\u73b0\u539f\u7406<\/h2>\n<p>&emsp;&emsp;\u4e0b\u9762\u6211\u4eec\u5c06\u7ed3\u5408\u5e95\u5c42\u5b9e\u73b0\u539f\u7406\uff0c\u4e00\u4e00\u89e3\u91ca\u4e0a\u9762\u7684\u51e0\u79cd\u60c5\u51b5\uff1a\u5b57\u7b26\u4e32\u76f8\u52a0\uff0c\u5b57\u7b26\u4e32\u4e0ebyte\u5207\u7247\u8f6c\u6362\uff0c\u5b57\u7b26\u4e32\u4e0erune\u5207\u7247\u4e92\u76f8\u8f6c\u6362\u3002<\/p>\n<p>&emsp;&emsp;\u5b57\u7b26\u4e32\u7ed3\u6784\u5b9a\u4e49\u4ee5\u53ca\u57fa\u672c\u64cd\u4f5c\u53ef\u4ee5\u5728\u6587\u4ef6runtime\/string.go\u67e5\u770b\uff0c\u5b57\u7b26\u4e32\u7ed3\u6784\u5b9a\u4e49\u6bd4\u5207\u7247\u7c7b\u4f3c\uff0c\u7a0d\u5fae\u7b80\u5355\u4e9b\uff0c\u56e0\u4e3a\u5b57\u7b26\u4e32\u53ea\u8bfb\uff0c\u6240\u4ee5\u6ca1\u6709\u5fc5\u8981\u9884\u5206\u914d\u7a7a\u95f4\uff0c\u4e5f\u5c31\u4e0d\u9700\u8981cap\u5b57\u6bb5\uff1a<\/p>\n<pre><code>type stringStruct struct {\n    str unsafe.Pointer\n    len int\n}<\/code><\/pre>\n<p>&emsp;&emsp;str\u6307\u5411\u5e95\u5c42\u771f\u6b63\u5b58\u50a8\u5b57\u7b26\u4e32\u7684\u6570\u7ec4\uff0c\u53ea\u662f\u6211\u4eec\u4e0d\u80fd\u83b7\u53d6\u5230\u8be5\u6570\u7ec4\u5f15\u7528\uff0c\u6240\u4ee5\u4e5f\u5c31\u65e0\u6cd5\u76f4\u63a5\u4fee\u6539\u5b57\u7b26\u4e32\u3002\u800c\u5b57\u7b26\u4e32\u5728\u4f5c\u4e3a\u8f93\u5165\u53c2\u6570\u65f6\uff0c\u4f20\u9012\u7684\u4e5f\u662f\u8be5\u7ed3\u6784\uff1blen\u51fd\u6570\u83b7\u53d6\u5b57\u7b26\u4e32\u957f\u5ea6\u65f6\uff0c\u5b57\u7b26\u4e32\u53d8\u91cf\u5730\u5740 +8\u5b57\u8282\u5c31\u662f\u4e86\uff0c\u8fd9\u4e9b\u90fd\u548c\u4e0a\u4e00\u5c0f\u8282\u5207\u7247\u7684\u57fa\u672c\u539f\u7406\u975e\u5e38\u7c7b\u4f3c\u3002<\/p>\n<p>&emsp;&emsp;s\u5b57\u7b26\u4e32\u76f8\u52a0\uff0c\u7f16\u8bd1\u9636\u6bb5\u4f1a\u66ff\u6362\u4e3a\u51fd\u6570\u8c03\u7528concatstrings\uff0c\u5176\u5b9e\u73b0\u4e5f\u633a\u7b80\u5355\u7684\uff0c\u8ba1\u7b97\u6240\u6709\u5b57\u7b26\u4e32\u957f\u5ea6\u4e4b\u548c\uff0c\u7533\u8bf7\u5185\u5b58\uff0c\u62f7\u8d1d\u539f\u59cb\u591a\u4e2a\u5b57\u7b26\u4e32\u5230\u65b0\u7684\u5185\u5b58\uff0c\u6784\u9020\u5b57\u7b26\u4e32\u7ed3\u6784\u4f53stringStruct\u8fd4\u56de\u3002\u51fd\u6570concatstrings\u6838\u5fc3\u903b\u8f91\u5982\u4e0b\uff1a<\/p>\n<pre><code>func concatstrings(a []string) string {\n    l := 0\n    \/\/\u8ba1\u7b97\u6240\u6709\u5b57\u7b26\u4e32\u957f\u5ea6\u4e4b\u548c\n    for _, x := range a {\n        n := len(x)\n        l += n\n    }\n\n    var s string\n    var b []byte\n    \/\/\u7533\u8bf7\u5185\u5b58\n    p := mallocgc(uintptr(l), nil, false)\n    \/\/\u6784\u9020\u5b57\u7b26\u4e32stringStruct\u7ed3\u6784\n    (*stringStruct)(unsafe.Pointer(s)).str = p\n    (*stringStruct)(unsafe.Pointer(s)).len = l\n\n    \/\/\u501f\u5207\u7247\u62f7\u8d1d\n    *(*slice)(unsafe.Pointer(&amp;b)) = slice{p, l, l}\n    for _, x := range a {\n        copy(b, x)\n        b = b[len(x):]\n    }\n\n    return s\n}<\/code><\/pre>\n<p>&emsp;&emsp;\u770b\u5230\u4e86\u5427\uff0c\u5b57\u7b26\u4e32\u76f8\u52a0\uff0c\u662f\u7533\u8bf7\u4e86\u65b0\u7684\u5185\u5b58\uff0c\u5e76\u6267\u884c\u4e86\u6570\u636e\u62f7\u8d1d\uff0c\u539f\u59cb\u5b57\u7b26\u4e32\u6ca1\u6709\u53d1\u751f\u4efb\u4f55\u6539\u53d8\uff0c\u5f80\u5f80\u6539\u53d8\u7684\u53ea\u662f\u5b57\u7b26\u4e32\u53d8\u91cf\u6307\u5411\u7684\u5185\u5b58\u5730\u5740\u3002<\/p>\n<p>&emsp;&emsp;\u5b57\u7b26\u4e32\u8f6c\u5316\u4e3abyte\u5207\u7247\uff0c\u4fee\u6539\u5207\u7247\uff0c\u4e3a\u4ec0\u4e48\u5b57\u7b26\u4e32\u5374\u6ca1\u6709\u6539\u53d8\uff0c\u8981\u56de\u7b54\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ea\u80fd\u770b\u5b57\u7b26\u4e32\u8f6c\u5316\u5207\u7247\u7684\u5b9e\u73b0\u51fd\u6570\u4e86\u3002\u901a\u8fc7[]byte(&#8220;&#8221;)\u5f62\u5f0f\u7c7b\u578b\u5f3a\u8f6c\uff0c\u7f16\u8bd1\u9636\u6bb5\u4f1a\u66ff\u6362\u4e3a\u51fd\u6570\u8c03\u7528stringtoslicebyte\uff0c\u800c\u8be5\u51fd\u6570\u5176\u5b9e\u4e5f\u662f\u7533\u8bf7\u65b0\u7684\u5185\u5b58\uff0c\u62f7\u8d1d\u6570\u636e\uff0c\u6784\u9020\u5207\u7247\u7ed3\u6784\u8fd4\u56de\u3002\u51fd\u6570stringtoslicebyte\u6838\u5fc3\u903b\u8f91\u5982\u4e0b\uff1a<\/p>\n<pre><code>func stringtoslicebyte(s string) []byte {\n    var b []byte\n    \/\/\u7533\u8bf7\u5185\u5b58\n    cap := roundupsize(uintptr(len(s)))\n    p := mallocgc(cap, nil, false)\n\n    \/\/\u6784\u9020\u5207\u7247\u7ed3\u6784 &amp; \u62f7\u8d1d\u6570\u636e\n    *(*slice)(unsafe.Pointer(&amp;b)) = slice{p, len(s), int(cap)}\n    copy(b, s)\n\n    return b\n}<\/code><\/pre>\n<p>&emsp;&emsp;\u5b57\u7b26\u4e32\u8f6c\u5316\u4e3arune\u5207\u7247\u7684\u903b\u8f91\u4e0estringtoslicebyte\u975e\u5e38\u7c7b\u4f3c\uff0c\u53ea\u662frune\u7c7b\u578b\u53604\u4e2a\u5b57\u8282\u7f62\u4e86\u3002\u8fd9\u91cc\u5c31\u4e0d\u518d\u8d58\u8ff0\u4e86\u3002<\/p>\n<h2>\u5e38\u7528\u5e93\u51fd\u6570 &#038; stringBuilder<\/h2>\n<p>&emsp;&emsp;\u5305strings\u5b9a\u4e49\u4e86\u4e00\u4e9b\u5e38\u7528\u7684\u5b57\u7b26\u4e32\u5e93\u51fd\u6570\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code>\/\/\u5b57\u7b26\u4e32\u6bd4\u8f83\nfunc Compare(a, b string) int\n\/\/\u5b57\u7b26\u4e32\u662f\u5426\u4ee5xxx\u5f00\u59cb\nfunc HasPrefix(s, prefix string) bool\n\/\/\u5b57\u7b26\u4e32\u662f\u5426\u4ee5xxx\u7ed3\u675f\nfunc HasSuffix(s, suffix string) bool\n\/\/\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u6307\u5b9a\u5b50\u4e32\nfunc Contains(s, substr string) bool\n\/\/\u8fd4\u56de\u5b50\u4e32\u5728\u5b57\u7b26\u4e32\u662f\u7684\u4f4d\u7f6e\uff0c-1\u5b57\u7b26\u4e32\u4e0d\u5305\u542b\u5b50\u4e32\uff0c\u8fd8\u6709\u66f4\u9ad8\u7ea7\u7684\u5b57\u7b26\u4e32\u67e5\u627estringFinder\nfunc Index(s, substr string) int\n\/\/\u5b57\u7b26\u4e32\u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u6309sep\u5206\u9694\nfunc Join(elems []string, sep string) string\n\/\/\u5b57\u7b26\u4e32\u5206\u9694\u4e3a\u5b57\u7b26\u4e32\u6570\u7ec4\nfunc Split(s, sep string) []string\n\/\/\u5b57\u7b26\u4e32\u66ff\u6362\uff0c\u8fd8\u6709\u66f4\u9ad8\u7ea7\u7684\u5b57\u7b26\u4e32\u66ff\u6362Replacer\nfunc Replace(s, old, new string, n int) string\n\/\/\u5b57\u7b26\u4e32\u5927\u5c0f\u5199\u8f6c\u6362\nfunc ToLower(s string) string\nfunc ToUpper(s string) string\n\n\u2026\u2026<\/code><\/pre>\n<p>&emsp;&emsp;\u8fd9\u4e9b\u5e93\u51fd\u6570\u975e\u5e38\u7b80\u5355\uff0c\u6211\u5c31\u4e0d\u4e00\u4e00\u4ecb\u7ecd\u4e86\uff0c\u8fd9\u91cc\u4e3b\u8981\u63d0\u4e00\u4e0b\u5b57\u7b26\u4e32\u6784\u5efastringBuilder\u3002\u4e0a\u9762\u6211\u4eec\u8bf4\u8fc7Go\u8bed\u8a00\u5b57\u7b26\u4e32\u662f\u53ea\u8bfb\u7684\uff0c\u4e0d\u80fd\u4fee\u6539\u7684\uff0c\u5b57\u7b26\u4e32\u76f8\u52a0\u4e5f\u662f\u901a\u8fc7\u7533\u8bf7\u5185\u5b58\u4e0e\u6570\u636e\u62f7\u8d1d\u65b9\u5f0f\u5b9e\u73b0\uff0c\u90a3\u4e48\u5982\u679c\u5b58\u5728\u5927\u91cf\u7684\u5b57\u7b26\u4e32\u76f8\u52a0\u5462\uff1f\u6bcf\u6b21\u90fd\u7533\u8bf7\u5185\u5b58\u62f7\u8d1d\u6570\u636e\u6548\u7387\u4f1a\u975e\u5e38\u5dee\uff0c\u8fd9\u4e5f\u662fstringBuilder\u5b58\u5728\u7684\u539f\u56e0\u3002stringBuilder\u5e95\u5c42\u7ef4\u62a4\u4e86\u4e00\u4e2a[]byte\uff0c\u8ffd\u52a0\u5b57\u7b26\u4e32\u53ea\u662f\u8ffd\u52a0\u5230\u8be5\u5207\u7247\uff0c\u6700\u7ec8\u4e00\u6b21\u6027\u8f6c\u6362\u8be5\u5207\u7247\u4e3a\u5b57\u7b26\u4e32\uff0c\u907f\u514d\u4e86\u4e2d\u95f4N\u591a\u6b21\u7684\u5185\u5b58\u7533\u8bf7\u4e0e\u6570\u636e\u62f7\u8d1d\u3002<\/p>\n<p>&emsp;&emsp;\u6211\u4eec\u5199\u4e00\u4e2a\u5c0f\u4e8b\u4f8b\uff0c\u6d4b\u8bd5\u9a8c\u8bc1\u5927\u91cf\u5b57\u7b26\u4e32\u76f8\u52a0\u60c5\u51b5\u4e0b\uff0cstringBuilder\u5e26\u6765\u7684\u6027\u80fd\u63d0\u5347\uff1a<\/p>\n<pre><code>package main\n\nimport (\n    &quot;fmt&quot;\n    &quot;strings&quot;\n    &quot;time&quot;\n)\n\nfunc main() {\n    count := 100000\n\n    start := time.Now()\n    s := &quot;&quot;\n    for i := 0; i &lt; count; i ++ {\n        s += &quot;abc&quot;\n    }\n    fmt.Println(time.Now().Sub(start).Microseconds())   \/\/1466286\u5fae\u5999\n\n    start1 := time.Now()\n    b := strings.Builder{}\n    for i := 0; i &lt; count; i ++ {\n        b.WriteString(&quot;abc&quot;)\n    }\n    fmt.Println(time.Now().Sub(start1).Microseconds()) \/\/492\u5fae\u5999\uff0c\u6548\u7387\u63d0\u5347\u975e\u5e38\u660e\u663e\u3002\n}<\/code><\/pre>\n<h2>\u5b57\u7b26\u4e32\u7f16\u7801<\/h2>\n<p>&emsp;&emsp;\u4e0a\u9762\u6211\u4eec\u4ecb\u7ecd\u5230\uff0cGo\u8bed\u8a00\u4e00\u4e2a\u6c49\u5b57\u53603\u5b57\u8282\uff0c\u6240\u6709\u5b57\u7b26\u4e32\u5305\u542b\u6c49\u5b57\u65f6\uff0clen\u8fd4\u56de\u5b57\u7b26\u4e32\u957f\u5ea6\u5927\u4e8e\u5b57\u7b26\u6570\u3002\u6211\u4eec\u90fd\u77e5\u9053\u8ba1\u7b97\u673a\u5b58\u50a8\u53ea\u8bc6\u522b\u4e8c\u8fdb\u5236\uff0c\u6240\u4ee5\u5b57\u7b26\u9700\u8981\u7f16\u7801\u4e3a\u4e8c\u8fdb\u5236\uff0c\u90a3\u4e48Go\u8bed\u8a00\u5b57\u7b26\u4e32\u5230\u5e95\u91c7\u53d6\u54ea\u79cd\u7f16\u7801\u65b9\u5f0f\u5462\uff1f<\/p>\n<p>&emsp;&emsp;\u5148\u7b80\u5355\u4ecb\u7ecd\u4e0b\u51e0\u4e2a\u5e38\u7528\u7f16\u7801\u3002\u6700\u7b80\u5355\u7684\u7f16\u7801\u5c31\u662fASCII\u7801\u4e86\uff0c\u53ea\u9700\u4e00\u4e2a\u5b57\u8282\uff0c\u53ef\u4ee5\u8868\u793a\u4e00\u4e9b\u57fa\u672c\u7684\u5b57\u7b26\u3001\u6570\u5b57\u4e0e\u5b57\u6bcd\uff0c\u5982&#8221;? \/ ! . 10 A b c&#8221;\u3002\u90a3\u4e48\u4e2d\u6587\u600e\u4e48\u529e\uff1f\u4e00\u4e2a\u5b57\u8282\u80af\u5b9a\u662f\u65e0\u6cd5\u6ee1\u8db3\u7684\u3002\u4e8e\u662f\u8bde\u751f\u4e86unicode\u7f16\u7801\uff0c\u5360\u4e24\u4e2a\u5b57\u8282\uff0c\u53ef\u4ee5\u5bb9\u7eb3\u6240\u6709\u8bed\u8a00\u7684\u5927\u90e8\u5206\u6587\u5b57\u3002\u5728unicode\u7f16\u7801\u65b9\u5f0f\u4e0b\uff0c\u6240\u6709\u5b57\u7b26\u90fd\u9700\u8981\u4e24\u4e2a\u5b57\u8282\uff0c\u4e0d\u8bba\u6c49\u5b57\u8fd8\u662f\u5b57\u6bcd\uff08\u9ad8\u5b57\u8282\u51680\uff0c\u4f4e\u5b57\u8282\u5c31\u662fASCII\u7801\uff09\uff0c\u663e\u7136\u5bf9\u4e8e\u5b57\u6bcd\u6709\u4e9b\u6d6a\u8d39\u7a7a\u95f4\u4e86\u3002\u6240\u4ee5\u53c8\u8bde\u751f\u4e86utf-8\u7f16\u7801\uff0c\u8fd9\u65f6\u5019\u5b57\u7b26\u53ef\u4ee5\u53601-4\u5b57\u8282\uff08\u53ef\u53d8\u7684\uff09\uff0c\u4e2d\u6587\u6c49\u5b57\u5728utf-8\u7f16\u7801\u65b9\u5f0f\u53603\u4e2a\u5b57\u8282\uff0c\u82f1\u6587\u5b57\u6bcd\u53601\u4e2a\u5b57\u8282\uff0cGo\u8bed\u8a00\u91c7\u7528\u7684\u5c31\u662f\u8be5\u7f16\u7801\u65b9\u5f0f\u3002\u8fd9\u4e0b\u7ec8\u4e8e\u660e\u767d\u4e86\u5982\u4f55\u8ba1\u7b97\u5305\u542b\u6c49\u5b57\u7684\u5b57\u7b26\u4e32\u957f\u5ea6\u4e86\u3002<\/p>\n<p>&emsp;&emsp;\u8bdd\u4e0d\u591a\u8bf4\uff0c\u518d\u6765\u4e2a\u5c0f\u4e8b\u4f8b\u6d4b\u8bd5\u4e00\u4e0b\uff1a<\/p>\n<pre><code>package main\n\nimport &quot;fmt&quot;\n\nfunc main() {\n    str := &quot;Go\u8bed\u8a00\u8fd8\u662f\u633a\u4e0d\u9519\u7684&quot;\n    r := []rune(str)\n    for _, v := range r{\n        fmt.Printf(&quot;%x &quot;, v)\n    }\n}\n\n\/\/47 6f 8bed 8a00 8fd8 662f 633a 4e0d 9519 7684<\/code><\/pre>\n<p>&emsp;&emsp;\u597d\u50cf\u6709\u4e9b\u8bb8\u4e0d\u5bf9\u52b2\uff0c\u8fd9\u4e9b\u6c49\u5b57\u8c8c\u4f3c\u53ea\u5360\u4e862\u5b57\u8282\uff0c\u8fd9\u4e9b\u662futf-8\u7f16\u7801\u5417\uff1f\u5176\u5b9e\u4e0a\u9762\u8f93\u51fa\u7684\u90fd\u662funicode\u7f16\u7801\uff0c\u6240\u6709\u5b57\u7b26\u90fd\u53602\u5b57\u8282\u3002\u8bfb\u8005\u53ef\u4ee5\u627e\u4e00\u4e9b\u5de5\u5177\u6d4b\u8bd5\u4e0b\uff0c\u5c06\u4e0a\u9762\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aunicode\uff0c\u5bf9\u6bd4\u770b\u7ed3\u679c\u662f\u5426\u4e00\u81f4\u3002<\/p>\n<p>&emsp;&emsp;\u5fc5\u987b\u8981\u8bf4\u660e\u7684\u662f\uff0crune\u5176\u5b9e\u5c31\u662fint32\uff0c\u8be5\u7c7b\u578b\u672c\u6765\u5c31\u53604\u5b57\u8282\u3002Go\u8bed\u8a00\u5b57\u7b26\u4e32\u5728\u5b58\u50a8\u65f6\uff0c\u786e\u5b9e\u662f\u91c7\u7528utf-8\u7f16\u7801\uff0c\u4f46\u662f\u5f53\u8f6c\u5316\u4e3a[]rune\u64cd\u4f5c\u65f6\uff0c\u53c8\u5c06\u6240\u6709\u5b57\u7b26\u8f6c\u5316\u4e3aunicode\u7f16\u7801\u3002\u5b57\u7b26\u4e32\u4e0e[]rune\u8f6c\u5316\u51fd\u6570\u4e3astringtoslicerune\/slicerunetostring\u3002unicode\u7f16\u7801\u4e0eutf-8\u7f16\u7801\u8f6c\u5316\u51fd\u6570\u5b9a\u4e49\u5728\u6587\u4ef6runtime\/utf8.go\uff0c\u5206\u522b\u4e3adecoderune\/encoderune\u3002<\/p>\n<p>&emsp;&emsp;\u8fd9\u91cc\u5c31\u4e0d\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u4e2a\u51fd\u6570\u7684\u5177\u4f53\u5b9e\u73b0\u4e86\u3002\u4e0d\u8fc7\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u4f7f\u7528range\u904d\u5386\u5b57\u7b26\u4e32\u65f6\uff0c\u8fd4\u56de\u7684\u662f\u5b57\u7b26\uff0c\u4e5f\u5b58\u5728utf-8\u5230unicode\u7f16\u7801\u8f6c\u6362\u3002range\u7684\u5b9e\u73b0\u903b\u8f91\u5728\u6e90\u7801\u4e2d\u627e\u4e0d\u5230\uff0c\u662f\u7f16\u8bd1\u9636\u6bb5\u81ea\u52a8\u751f\u6210\u7684\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code>\/\/\u53c2\u8003\uff1acmd\/compile\/internal\/walk\/range.go:walkRange\n\n\/\/ Transform string range statements like &quot;for v1, v2 = range a&quot; into\n\/\/\n\/\/ ha := a\n\/\/ for hv1 := 0; hv1 &lt; len(ha); {\n\/\/   hv1t := hv1\n\/\/   hv2 := rune(ha[hv1])\n\/\/   if hv2 &lt; utf8.RuneSelf {\n\/\/      hv1++\n\/\/   } else {\n\/\/      hv2, hv1 = decoderune(ha, hv1)\n\/\/   }\n\/\/   v1, v2 = hv1t, hv2\n\/\/   \/\/ original body\n\/\/ }<\/code><\/pre>\n<h2>\u603b\u7ed3<\/h2>\n<p>&emsp;&emsp;\u5b57\u7b26\u4e32\u7684\u57fa\u672c\u4f7f\u7528\u4e0e\u5b9e\u73b0\u539f\u7406\u5c31\u8bb2\u89e3\u5230\u8fd9\u91cc\u4e86\uff0c\u8981\u7262\u8bb0\u5b57\u7b26\u4e32\u662f\u4e0d\u53ef\u8bfb\u7684\uff0c\u800c\u5b57\u7b26\u4e32\u76f8\u52a0\uff0c\u5b57\u7b26\u4e32\u4e0e[]byte\/[]rune\u4e92\u76f8\u8f6c\u6362\u90fd\u662f\u901a\u8fc7\u7533\u8bf7\u5185\u5b58\u4ee5\u53ca\u6570\u636e\u62f7\u8d1d\u65b9\u5f0f\u5b9e\u73b0\u7684\u3002\u53e6\u5916\u8981\u6ce8\u610f\u4e2d\u6587\u6c49\u5b57\u7f16\u7801\u53603\u4e2a\u5b57\u8282\uff0c\u6240\u4ee5\u5305\u542b\u4e2d\u6587\u6c49\u5b57\u7684\u5b57\u7b26\u4e32\uff0c\u5176\u957f\u5ea6\u4e0e\u5b57\u7b26\u6570\u662f\u4e0d\u540c\u7684\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042484615\">https:\/\/segmentfault.com\/a\/1190000042484615<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&emsp;&emsp;Go\u8bed\u8a00\u5b57\u7b26\u4e32\u7684\u7528\u6cd5\u8fd8\u662f\u6bd4\u8f83\u7b80\u5355\u7684\uff0c\u5e38\u7528\u4e5f\u5c31\u662f\u5b57\u7b26\u4e32\u76f8\u52a0\uff0c\u5b57\u7b26\u4e32\u4e0ebyte\u5207\u7247\u3001run [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006487","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006487","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006487"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006487\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}