{"id":57898,"date":"2024-10-10T22:04:15","date_gmt":"2024-10-10T14:04:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/57898\/"},"modified":"2024-10-10T22:04:15","modified_gmt":"2024-10-10T14:04:15","slug":"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e5%ad%97%e7%ac%a6%e9%a1%9e%e5%9e%8b%ef%bc%88byte-%e5%92%8c-rune%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/57898\/","title":{"rendered":"GO\u6559\u7a0b: Go \u8a9e\u8a00\u5b57\u7b26\u985e\u578b\uff08byte \u548c rune\uff09"},"content":{"rendered":"<h1 id=\"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e5%ad%97%e7%ac%a6%e9%a1%9e%e5%9e%8b%ef%bc%88byte-%e5%92%8c-rune%ef%bc%89-zTvCQwJpGn\">GO\u6559\u7a0b: Go \u8a9e\u8a00\u5b57\u7b26\u985e\u578b\uff08byte \u548c rune\uff09<\/h1>\n<p>\u5728\u5b78\u7fd2 Go \u8a9e\u8a00\u7684\u904e\u7a0b\u4e2d\uff0c\u7406\u89e3\u5b57\u7b26\u985e\u578b\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u74b0\u3002Go \u8a9e\u8a00\u63d0\u4f9b\u4e86\u5169\u7a2e\u4e3b\u8981\u7684\u5b57\u7b26\u985e\u578b\uff1a<code>byte<\/code> \u548c <code>rune<\/code>\u3002\u9019\u5169\u7a2e\u985e\u578b\u5728\u8655\u7406\u6587\u672c\u6578\u64da\u6642\u5404\u6709\u5176\u7279\u9ede\u548c\u7528\u9014\uff0c\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u9019\u5169\u7a2e\u5b57\u7b26\u985e\u578b\u7684\u5b9a\u7fa9\u3001\u7279\u6027\u53ca\u5176\u4f7f\u7528\u5834\u666f\u3002<\/p>\n<h2 id=\"1-byte-%e9%a1%9e%e5%9e%8b-zTvCQwJpGn\">1. byte \u985e\u578b<\/h2>\n<p><code>byte<\/code> \u662f Go \u8a9e\u8a00\u4e2d\u7684\u4e00\u7a2e\u57fa\u672c\u6578\u64da\u985e\u578b\uff0c\u5be6\u969b\u4e0a\u5b83\u662f <code>uint8<\/code> \u7684\u5225\u540d\u3002\u6bcf\u500b <code>byte<\/code> \u4ee3\u8868\u4e00\u500b 8 \u4f4d\u7684\u7121\u7b26\u865f\u6574\u6578\uff0c\u901a\u5e38\u7528\u4f86\u8868\u793a ASCII \u5b57\u7b26\u6216\u5176\u4ed6\u55ae\u5b57\u7bc0\u7de8\u78bc\u7684\u5b57\u7b26\u3002<\/p>\n<h3 id=\"1-1-byte-%e7%9a%84%e7%89%b9%e6%80%a7-zTvCQwJpGn\">1.1 byte \u7684\u7279\u6027<\/h3>\n<ul>\n<li><strong>\u7bc4\u570d\uff1a<\/strong> <code>byte<\/code> \u7684\u503c\u7bc4\u570d\u662f 0 \u5230 255\u3002<\/li>\n<li><strong>\u7528\u9014\uff1a<\/strong> \u901a\u5e38\u7528\u65bc\u8655\u7406\u4e8c\u9032\u5236\u6578\u64da\u6216 ASCII \u5b57\u7b26\u4e32\u3002<\/li>\n<li><strong>\u5167\u5b58\u4f54\u7528\uff1a<\/strong> \u6bcf\u500b <code>byte<\/code> \u4f54\u7528 1 \u500b\u5b57\u7bc0\u7684\u5167\u5b58\u3002<\/li>\n<\/ul>\n<h3 id=\"1-2-byte-%e7%9a%84%e4%bd%bf%e7%94%a8%e7%af%84%e4%be%8b-zTvCQwJpGn\">1.2 byte \u7684\u4f7f\u7528\u7bc4\u4f8b<\/h3>\n<pre><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    var b byte = 'A' \/\/ ASCII \u503c 65\n    fmt.Println(b)   \/\/ \u8f38\u51fa: 65\n    fmt.Printf(\"%cn\", b) \/\/ \u8f38\u51fa: A\n}\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5c07\u5b57\u7b26 &#8216;A&#8217; \u8ce6\u503c\u7d66\u8b8a\u6578 <code>b<\/code>\uff0c\u4e26\u4e14\u53ef\u4ee5\u770b\u5230\u5b83\u7684 ASCII \u503c\u70ba 65\u3002<\/p>\n<h2 id=\"2-rune-%e9%a1%9e%e5%9e%8b-zTvCQwJpGn\">2. rune \u985e\u578b<\/h2>\n<p><code>rune<\/code> \u662f Go \u8a9e\u8a00\u4e2d\u7528\u4f86\u8868\u793a Unicode \u5b57\u7b26\u7684\u985e\u578b\uff0c\u5be6\u969b\u4e0a\u5b83\u662f <code>int32<\/code> \u7684\u5225\u540d\u3002\u6bcf\u500b <code>rune<\/code> \u53ef\u4ee5\u8868\u793a\u4e00\u500b Unicode \u5b57\u7b26\uff0c\u9019\u4f7f\u5f97 Go \u8a9e\u8a00\u80fd\u5920\u8655\u7406\u591a\u7a2e\u8a9e\u8a00\u7684\u5b57\u7b26\u3002<\/p>\n<h3 id=\"2-1-rune-%e7%9a%84%e7%89%b9%e6%80%a7-zTvCQwJpGn\">2.1 rune \u7684\u7279\u6027<\/h3>\n<ul>\n<li><strong>\u7bc4\u570d\uff1a<\/strong> <code>rune<\/code> \u7684\u503c\u7bc4\u570d\u662f -2147483648 \u5230 2147483647\u3002<\/li>\n<li><strong>\u7528\u9014\uff1a<\/strong> \u7528\u65bc\u8655\u7406 Unicode \u5b57\u7b26\uff0c\u9069\u5408\u591a\u8a9e\u8a00\u6587\u672c\u7684\u8655\u7406\u3002<\/li>\n<li><strong>\u5167\u5b58\u4f54\u7528\uff1a<\/strong> \u6bcf\u500b <code>rune<\/code> \u4f54\u7528 4 \u500b\u5b57\u7bc0\u7684\u5167\u5b58\u3002<\/li>\n<\/ul>\n<h3 id=\"2-2-rune-%e7%9a%84%e4%bd%bf%e7%94%a8%e7%af%84%e4%be%8b-zTvCQwJpGn\">2.2 rune \u7684\u4f7f\u7528\u7bc4\u4f8b<\/h3>\n<pre><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    var r rune = '\u4e2d' \/\/ Unicode \u503c 20013\n    fmt.Println(r)    \/\/ \u8f38\u51fa: 20013\n    fmt.Printf(\"%cn\", r) \/\/ \u8f38\u51fa: \u4e2d\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5c07\u5b57\u7b26 &#8216;\u4e2d&#8217; \u8ce6\u503c\u7d66\u8b8a\u6578 <code>r<\/code>\uff0c\u4e26\u4e14\u53ef\u4ee5\u770b\u5230\u5b83\u7684 Unicode \u503c\u70ba 20013\u3002<\/p>\n<h2 id=\"3-byte-%e8%88%87-rune-%e7%9a%84%e6%af%94%e8%bc%83-zTvCQwJpGn\">3. byte \u8207 rune \u7684\u6bd4\u8f03<\/h2>\n<p>\u5728\u9078\u64c7\u4f7f\u7528 <code>byte<\/code> \u9084\u662f <code>rune<\/code> \u6642\uff0c\u958b\u767c\u8005\u9700\u8981\u6839\u64da\u5177\u9ad4\u7684\u9700\u6c42\u4f86\u6c7a\u5b9a\uff1a<\/p>\n<ul>\n<li><strong>\u5b57\u7b26\u96c6\uff1a<\/strong> \u5982\u679c\u53ea\u9700\u8981\u8655\u7406 ASCII \u5b57\u7b26\uff0c\u5247\u53ef\u4ee5\u4f7f\u7528 <code>byte<\/code>\u3002\u5982\u679c\u9700\u8981\u8655\u7406\u591a\u8a9e\u8a00\u5b57\u7b26\uff0c\u5247\u61c9\u4f7f\u7528 <code>rune<\/code>\u3002<\/li>\n<li><strong>\u5167\u5b58\u8003\u91cf\uff1a<\/strong> <code>byte<\/code> \u4f54\u7528\u7684\u5167\u5b58\u8f03\u5c11\uff0c\u9069\u5408\u8655\u7406\u5927\u91cf\u7684 ASCII \u5b57\u7b26\u4e32\uff1b\u800c <code>rune<\/code> \u96d6\u7136\u4f54\u7528\u66f4\u591a\u5167\u5b58\uff0c\u4f46\u80fd\u5920\u652f\u6301\u66f4\u5ee3\u6cdb\u7684\u5b57\u7b26\u96c6\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-zTvCQwJpGn\">\u7e3d\u7d50<\/h2>\n<p>\u5728 Go \u8a9e\u8a00\u4e2d\uff0c<code>byte<\/code> \u548c <code>rune<\/code> \u662f\u5169\u7a2e\u91cd\u8981\u7684\u5b57\u7b26\u985e\u578b\uff0c\u5206\u5225\u7528\u65bc\u8655\u7406 ASCII \u5b57\u7b26\u548c Unicode \u5b57\u7b26\u3002\u7406\u89e3\u9019\u5169\u8005\u7684\u7279\u6027\u53ca\u5176\u9069\u7528\u5834\u666f\uff0c\u5c0d\u65bc\u958b\u767c\u8005\u5728\u9032\u884c\u6587\u672c\u8655\u7406\u6642\u81f3\u95dc\u91cd\u8981\u3002\u7121\u8ad6\u662f\u9078\u64c7 <code>byte<\/code> \u9084\u662f <code>rune<\/code>\uff0c\u90fd\u61c9\u6839\u64da\u5177\u9ad4\u9700\u6c42\u4f86\u505a\u51fa\u660e\u667a\u7684\u9078\u64c7\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bc <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u670d\u52d9\u6709\u8208\u8da3\uff0c\u6b61\u8fce\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3 Go \u8a9e\u8a00\u4e2d\u7684\u5b57\u7b26\u985e\u578b\uff0c\u6df1\u5165\u63a2\u8a0e byte \u548c rune \u7684\u5340\u5225\u53ca\u7528\u6cd5\uff0c\u63d0\u5347\u4f60\u7684\u7de8\u7a0b\u6280\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-57898","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57898","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=57898"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57898\/revisions"}],"predecessor-version":[{"id":57899,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57898\/revisions\/57899"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=57898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=57898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=57898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}