{"id":57606,"date":"2024-10-10T19:39:15","date_gmt":"2024-10-10T11:39:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/57606\/"},"modified":"2024-10-10T19:39:15","modified_gmt":"2024-10-10T11:39:15","slug":"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/57606\/","title":{"rendered":"GO\u6559\u7a0b: Go \u8a9e\u8a00\u5b57\u7b26\u4e32"},"content":{"rendered":"<h1 id=\"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e5%ad%97%e7%ac%a6%e4%b8%b2-fuvebjuRtl\">GO\u6559\u7a0b: Go \u8a9e\u8a00\u5b57\u7b26\u4e32<\/h1>\n<p>Go \u8a9e\u8a00\uff08\u53c8\u7a31 Golang\uff09\u662f\u4e00\u7a2e\u7531 Google \u958b\u767c\u7684\u958b\u6e90\u7de8\u7a0b\u8a9e\u8a00\uff0c\u4ee5\u5176\u7c21\u6f54\u6027\u548c\u9ad8\u6548\u6027\u800c\u805e\u540d\u3002\u5728 Go \u8a9e\u8a00\u4e2d\uff0c\u5b57\u7b26\u4e32\u662f\u4e00\u500b\u91cd\u8981\u7684\u6578\u64da\u985e\u578b\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u7a0b\u5e8f\u4e2d\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e Go \u8a9e\u8a00\u4e2d\u7684\u5b57\u7b26\u4e32\uff0c\u5305\u62ec\u5176\u57fa\u672c\u7279\u6027\u3001\u64cd\u4f5c\u65b9\u6cd5\u4ee5\u53ca\u5e38\u898b\u7684\u4f7f\u7528\u5834\u666f\u3002<\/p>\n<h2 id=\"%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e5%9f%ba%e6%9c%ac%e7%89%b9%e6%80%a7-fuvebjuRtl\">\u5b57\u7b26\u4e32\u7684\u57fa\u672c\u7279\u6027<\/h2>\n<p>\u5728 Go \u8a9e\u8a00\u4e2d\uff0c\u5b57\u7b26\u4e32\u662f\u4e00\u500b\u4e0d\u53ef\u8b8a\u7684\u5b57\u5143\u5e8f\u5217\u3002\u9019\u610f\u5473\u8457\u4e00\u65e6\u5275\u5efa\uff0c\u5b57\u7b26\u4e32\u7684\u5167\u5bb9\u5c31\u7121\u6cd5\u66f4\u6539\u3002Go \u8a9e\u8a00\u4e2d\u7684\u5b57\u7b26\u4e32\u4f7f\u7528 UTF-8 \u7de8\u78bc\uff0c\u9019\u4f7f\u5f97\u5b83\u80fd\u5920\u652f\u6301\u591a\u7a2e\u8a9e\u8a00\u7684\u5b57\u7b26\u3002<\/p>\n<p>\u5b57\u7b26\u4e32\u7684\u57fa\u672c\u5b9a\u7fa9\u5982\u4e0b\uff1a<\/p>\n<pre><code>var str string = \"Hello, World!\"<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba <code>str<\/code> \u7684\u5b57\u7b26\u4e32\u8b8a\u91cf\uff0c\u4e26\u8ce6\u503c\u70ba &#8220;Hello, World!&#8221;\u3002<\/p>\n<h2 id=\"%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e9%95%b7%e5%ba%a6-fuvebjuRtl\">\u5b57\u7b26\u4e32\u7684\u9577\u5ea6<\/h2>\n<p>\u8981\u7372\u53d6\u5b57\u7b26\u4e32\u7684\u9577\u5ea6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5167\u5efa\u7684 <code>len()<\/code> \u51fd\u6578\u3002\u9019\u500b\u51fd\u6578\u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u5b57\u5143\u7684\u6578\u91cf\u3002<\/p>\n<pre><code>length := len(str) \/\/ length \u5c07\u6703\u662f 13<\/code><\/pre>\n<h2 id=\"%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e6%8b%bc%e6%8e%a5-fuvebjuRtl\">\u5b57\u7b26\u4e32\u7684\u62fc\u63a5<\/h2>\n<p>\u5728 Go \u8a9e\u8a00\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u52a0\u865f\uff08<code>+<\/code>\uff09\u4f86\u62fc\u63a5\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>greeting := \"Hello, \" + \"World!\" \/\/ greeting \u5c07\u6703\u662f \"Hello, World!\"<\/code><\/pre>\n<p>\u6b64\u5916\uff0cGo \u8a9e\u8a00\u9084\u63d0\u4f9b\u4e86 <code>fmt.Sprintf()<\/code> \u51fd\u6578\u4f86\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre><code>formatted := fmt.Sprintf(\"%s %s\", \"Hello,\", \"World!\") \/\/ formatted \u5c07\u6703\u662f \"Hello, World!\"<\/code><\/pre>\n<h2 id=\"%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e5%88%87%e7%89%87-fuvebjuRtl\">\u5b57\u7b26\u4e32\u7684\u5207\u7247<\/h2>\n<p>Go \u8a9e\u8a00\u4e2d\u7684\u5b57\u7b26\u4e32\u53ef\u4ee5\u901a\u904e\u5207\u7247\u4f86\u7372\u53d6\u5b50\u5b57\u7b26\u4e32\u3002\u4f7f\u7528\u7d22\u5f15\u4f86\u6307\u5b9a\u8d77\u59cb\u548c\u7d50\u675f\u4f4d\u7f6e\uff1a<\/p>\n<pre><code>substring := str[0:5] \/\/ substring \u5c07\u6703\u662f \"Hello\"<\/code><\/pre>\n<p>\u9019\u88e1\u7684 <code>str[0:5]<\/code> \u8868\u793a\u5f9e\u7d22\u5f15 0 \u5230 4 \u7684\u5b57\u5143\u3002<\/p>\n<h2 id=\"%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e9%81%8d%e6%ad%b7-fuvebjuRtl\">\u5b57\u7b26\u4e32\u7684\u904d\u6b77<\/h2>\n<p>\u53ef\u4ee5\u4f7f\u7528 <code>for<\/code> \u5faa\u74b0\u4f86\u904d\u6b77\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u500b\u5b57\u5143\uff1a<\/p>\n<pre><code>for i, c := range str {\n    fmt.Printf(\"Index: %d, Character: %cn\", i, c)\n}<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c07\u6703\u6253\u5370\u51fa\u5b57\u7b26\u4e32\u4e2d\u6bcf\u500b\u5b57\u5143\u7684\u7d22\u5f15\u548c\u5b57\u7b26\u3002<\/p>\n<h2 id=\"%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e6%af%94%e8%bc%83-fuvebjuRtl\">\u5b57\u7b26\u4e32\u7684\u6bd4\u8f03<\/h2>\n<p>\u5728 Go \u8a9e\u8a00\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>==<\/code> \u548c <code>!=<\/code> \u4f86\u6bd4\u8f03\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>if str == \"Hello, World!\" {\n    fmt.Println(\"Strings are equal\")\n}<\/code><\/pre>\n<h2 id=\"%e5%b8%b8%e8%a6%8b%e7%9a%84%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%93%8d%e4%bd%9c%e5%87%bd%e6%95%b8-fuvebjuRtl\">\u5e38\u898b\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u51fd\u6578<\/h2>\n<p>Go \u8a9e\u8a00\u7684 <code>strings<\/code> \u5305\u63d0\u4f9b\u4e86\u8a31\u591a\u6709\u7528\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u51fd\u6578\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li><code>strings.ToUpper(str)<\/code> &#8211; \u5c07\u5b57\u7b26\u4e32\u8f49\u63db\u70ba\u5927\u5beb\u3002<\/li>\n<li><code>strings.ToLower(str)<\/code> &#8211; \u5c07\u5b57\u7b26\u4e32\u8f49\u63db\u70ba\u5c0f\u5beb\u3002<\/li>\n<li><code>strings.Contains(str, \"World\")<\/code> &#8211; \u6aa2\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u7279\u5b9a\u5b50\u5b57\u7b26\u4e32\u3002<\/li>\n<li><code>strings.Split(str, \",\")<\/code> &#8211; \u6839\u64da\u6307\u5b9a\u5206\u9694\u7b26\u5c07\u5b57\u7b26\u4e32\u5206\u5272\u70ba\u5b50\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-fuvebjuRtl\">\u7d50\u8ad6<\/h2>\n<p>Go \u8a9e\u8a00\u4e2d\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u7c21\u55ae\u800c\u9ad8\u6548\uff0c\u9069\u5408\u7528\u65bc\u5404\u7a2e\u61c9\u7528\u5834\u666f\u3002\u7121\u8ad6\u662f\u57fa\u672c\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\u3001\u5207\u7247\uff0c\u9084\u662f\u66f4\u8907\u96dc\u7684\u5b57\u7b26\u4e32\u8655\u7406\uff0cGo \u8a9e\u8a00\u90fd\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u652f\u6301\u3002\u638c\u63e1\u9019\u4e9b\u57fa\u672c\u64cd\u4f5c\u5f8c\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u9748\u6d3b\u5730\u8655\u7406\u6587\u672c\u6578\u64da\uff0c\u63d0\u5347\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u53ef\u8b80\u6027\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u6709\u95dc <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u548c\u5176\u4ed6\u670d\u52d9\u7684\u4fe1\u606f\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 Go \u8a9e\u8a00\u4e2d\u7684\u5b57\u7b26\u4e32\u8655\u7406\uff0c\u5b78\u7fd2\u5982\u4f55\u5275\u5efa\u3001\u64cd\u4f5c\u548c\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\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-57606","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57606","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=57606"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57606\/revisions"}],"predecessor-version":[{"id":57607,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57606\/revisions\/57607"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=57606"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=57606"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=57606"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}