{"id":4126,"date":"2018-10-21T13:52:05","date_gmt":"2018-10-21T05:52:05","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4126"},"modified":"2018-10-21T13:52:05","modified_gmt":"2018-10-21T05:52:05","slug":"go%e8%af%ad%e8%a8%80%e5%ad%a6%e4%b9%a01-%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4126.html","title":{"rendered":"Go\u8bed\u8a00\u5b66\u4e60#1-\u5b57\u7b26\u4e32\u64cd\u4f5c"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u6700\u8fd1\u5728\u8ddf\u7740\u90dd\u6797\u8001\u5e08\u7684\u300aGo\u8bed\u8a00\u6838\u5fc336\u8bb2\u300b\u8fd9\u95e8\u8bfe\u7a0b\u91cd\u62feGo\u8bed\u8a00\u5b66\u4e60\uff08\u4e4b\u524d\u572815\u5e74\u7684\u65f6\u5019\u6211\u81ea\u5b66\u8fc7\u4e00\u6bb5\u65f6\u95f4\uff0c\u4e0d\u8fc7\u540e\u6765\u6ca1\u600e\u4e48\u7528Go\u6765\u5199\u4ee3\u7801\uff0c\u6240\u4ee5\u6709\u4e9b\u5185\u5bb9\u90fd\u5fd8\u4e86\uff0c\u8fd9\u6b21\u5e0c\u671b\u80fd\u575a\u6301\u4e0b\u53bb\uff09\uff0c\u6240\u4ee5\u51c6\u5907\u628a\u4e00\u4e9b\u5e38\u89c1\u529f\u80fd\u7528Go\u8bed\u8a00\u8bd5\u7740\u5b9e\u8df5\u4e00\u904d\uff0c\u52a0\u5f3a\u5b66\u4e60\u6548\u679c\u548c\u5bf9\u8fd9\u95e8\u8bed\u8a00\u7684\u611f\u89c9\u3002<\/p>\n<h4 id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<p>\u4e0d\u8bba\u662f\u54ea\u95e8\u8bed\u8a00\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u7f16\u7801\u8fc7\u7a0b\u4e2d\uff0c\u5b57\u7b26\u4e32\u64cd\u4f5c\u5e94\u8be5\u662f\u7ed5\u4e0d\u8fc7\u53bb\u7684\u4e00\u7c7b\u57fa\u7840\u64cd\u4f5c\uff0c\u800c\u4e14\u4f1a\u5360\u5f88\u5927\u4e00\u90e8\u5206\u6bd4\u4f8b\uff0c\u6240\u4ee5\uff0c\u8fd9\u6b21\u5728\u5b66\u4e60\u8fc7\u7a0b\u4e2d\u4e5f\u5c1d\u8bd5\u5bf9\u4e00\u4e9b\u5e38\u89c4\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u8fdb\u884c\u6574\u7406\uff0c\u65b9\u4fbf\u4ee5\u540e\u53c2\u8003\u3002<\/p>\n<p><strong>\u4e3a\u4e86\u51cf\u5c11\u6587\u7ae0\u7bc7\u5e45\uff0c\u540c\u65f6\u4e0d\u5f71\u54cd\u7406\u89e3\uff0c\u5148\u5728\u8fd9\u91cc\u8bf4\u660e\u4e00\u4e0b\u6f14\u793a\u4ee3\u7801\u7684\u5927\u4f53\u7ed3\u6784\uff1a<\/strong><\/p>\n<pre class=\"lang:default decode:true\">package main\r\n\r\nimport (\r\n\t\"fmt\"\r\n\t\"strings\"\r\n)\r\n\r\nfunc learn_str_operation() {\r\n\t\/\/ put verify code here\r\n}\r\n\r\nfunc main() {\r\n\tlearn_str_operation()\r\n}<\/pre>\n<hr \/>\n<h6>\u5982\u4f55\u83b7\u53d6\u5b57\u7b26\u4e32\u957f\u5ea6\uff1f\u2014\u2014\u7528\u5185\u5efa\u7684 len() \u51fd\u6570\u3002<\/h6>\n<pre class=\"lang:default decode:true \">str_in := \"count=1000] uid=123456]\" \/\/ \u5b57\u7b26\u4e32\u8981\u7528\u300c\u53cc\u5f15\u53f7\u300d\u5305\u88f9\u8d77\u6765\r\nfmt.Printf(\"the length of 'str_in': %d\\n\", len(str_in)) \/\/ 23<\/pre>\n<h6>\u5faa\u73af\u6253\u5370\uff08for..range\u8bed\u6cd5\uff09<\/h6>\n<pre class=\"lang:default decode:true \">str_in := \"count=1000] uid=123456]\" \/\/ \u5b57\u7b26\u4e32\u8981\u7528\u300c\u53cc\u5f15\u53f7\u300d\u5305\u88f9\u8d77\u6765\r\n\r\n\/\/ \u7528for\u5faa\u73af\u6253\u5370\u51fa\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\r\nfor idx, value := range str_in {\r\n    fmt.Printf(\"idx: %d, value: %q\\n\", idx, value)\r\n}<\/pre>\n<h6><span style=\"color: #ff0000;\">\u5b50\u4e32\u622a\u53d6<\/span><\/h6>\n<pre class=\"lang:default decode:true \">str_in := \"count=1000] uid=123456]\" \/\/ \u5b57\u7b26\u4e32\u8981\u7528\u300c\u53cc\u5f15\u53f7\u300d\u5305\u88f9\u8d77\u6765\r\nstr_temp := str_in[:5] \/\/ \u622a\u53d6\u524d5\u4e2a\u5b57\u7b26\uff0c\u7528\u7684\u662fslice\u5207\u7247\u64cd\u4f5c\r\nfmt.Printf(\"the first 5 charters of 'str_in': %q\\n\", str_temp) \/\/ count\r\n<\/pre>\n<h6><span style=\"color: #ff0000;\">\u5b57\u7b26\u4e32\u67e5\u627e<\/span><\/h6>\n<ul>\n<li>func <span style=\"color: #ff0000;\"><strong>Contains<\/strong><\/span>(s, substr string) bool<\/li>\n<li>func <strong><span style=\"color: #ff0000;\">ContainsAny<\/span><\/strong>(s, chars string) bool \/\/\u53c2\u6570chars\u4e2d\u6709\u6ca1\u6709\u4efb\u4f55\u4e00\u4e2a\u5b57\u7b26\u5728\u5b57\u7b26\u4e32s\u4e2d\uff0c\u6709\u5373\u8fd4\u56detrue<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">\/\/ \u5b57\u7b26\u4e32\u67e5\u627e\r\nfmt.Println(strings.Contains(\"hello world\", \"hello\")) \/\/ true\r\nfmt.Println(strings.Contains(\"goodbye\", \"good\"))      \/\/ true\r\nfmt.Println(strings.Contains(\"good\", \"goodbye\"))      \/\/ false\r\n\r\nfmt.Println(strings.ContainsAny(\"team\", \"i\"))        \/\/ false\r\nfmt.Println(strings.ContainsAny(\"failure\", \"u &amp; i\")) \/\/ true\r\nfmt.Println(strings.ContainsAny(\"failure\", \"u &amp; x\")) \/\/ true\r\nfmt.Println(strings.ContainsAny(\"failure\", \"u | x\")) \/\/ true\r\nfmt.Println(strings.ContainsAny(\"failure\", \"x &amp; y\")) \/\/ false\r\n<\/pre>\n<h6><span style=\"color: #ff0000;\">\u5b57\u7b26\u4e32\u62fc\u63a5<\/span><\/h6>\n<ul>\n<li>func Join(a []string, sep string) string<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">s := []string{\"foo\", \"bar\", \"baz\"}\r\nfmt.Printf(\"value: %q\\n\", s)                    \/\/ [\"foo\" \"bar\" \"baz\"]\r\nfmt.Printf(\"value: %q\\n\", strings.Join(s, \",\")) \/\/ \"foo,bar,baz\"\r\n<\/pre>\n<h6><span style=\"color: #ff0000;\">\u5b57\u7b26\u4e32\u66ff\u6362<\/span><\/h6>\n<ul>\n<li>func Replace(s, old, new string, n int) string<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">fmt.Println(strings.Replace(\"link link link\", \"k\", \"es\", 2))      \/\/ lines lines link\r\nfmt.Println(strings.Replace(\"link link link\", \"link\", \"moo\", -1)) \/\/ moo moo moo\r\n<\/pre>\n<h6><span style=\"color: #ff0000;\">\u6309\u7279\u5b9a\u5b57\u7b26\u4e32\u8fdb\u884c\u5207\u5206<\/span><\/h6>\n<ul>\n<li>func Split(s, sep string) []string<\/li>\n<li>func SplitN(s, sep string, n int) []string<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">fmt.Printf(\"value: %q\\n\", strings.Split(\"a,b,c\", \",\"))                        \/\/ [\"a\" \"b\" \"c\"]\r\nfmt.Printf(\"value: %q\\n\", strings.Split(\"a man a plan a canal panama\", \"a \")) \/\/ [\"\" \"man \" \"plan \" \"canal panama\"]\r\nfmt.Printf(\"value: %q\\n\", strings.Split(\" xyz \", \"\"))                         \/\/ [\" \" \"x\" \"y\" \"z\" \" \"]\r\nfmt.Printf(\"value: %q\\n\", strings.Split(\"\", \"test\"))                          \/\/ [\"\"]\r\n\r\nfmt.Printf(\"%q\\n\", strings.SplitN(\"a,b,c\", \",\", 1)) \/\/ [\"a,b,c\"]\r\nfmt.Printf(\"%q\\n\", strings.SplitN(\"a,b,c\", \",\", 2)) \/\/ [\"a\" \"b,c\"]\r\nz := strings.SplitN(\"a,b,c\", \",\", 0)                \/\/ \u5f53\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u4e3a0\u65f6\uff0c\u8868\u793a\u8fd4\u56de nil\r\nfmt.Printf(\"%q (nil = %v)\\n\", z, z == nil)          \/\/ [] (nil = true)\r\n<\/pre>\n<h6>\u56fa\u5b9a\u957f\u5ea6\u7684\u968f\u673a\u5b57\u7b26\u4e32\u751f\u6210<\/h6>\n<ul>\n<li>\u53c2\u8003\u300c<a href=\"https:\/\/ixyzero.com\/blog\/archives\/2535.html\" target=\"_blank\" rel=\"noopener\">\u7528Go\u751f\u6210\u6307\u5b9a\u957f\u5ea6\u7684\u968f\u673a\u5b57\u7b26\u4e32<\/a>\u300d\u3002<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li>golang\u4e2d\u5b57\u7b26\u4e32\u7684\u67e5\u627e\u65b9\u6cd5\u5c0f\u7ed3<br \/>\n<a href=\"https:\/\/www.cnblogs.com\/unqiang\/p\/6682281.html\">https:\/\/www.cnblogs.com\/unqiang\/p\/6682281.html<\/a><\/li>\n<li>\u672c\u5730\u7528\u547d\u4ee4 <code>godoc -http=:8080<\/code>\u00a0\u642d\u5efa\u7684\u6587\u6863\u67e5\u8be2\u9875<br \/>\n<a href=\"http:\/\/localhost:8080\/pkg\/strings\/#pkg-examples\" target=\"_blank\" rel=\"noopener\">http:\/\/localhost:8080\/pkg\/strings\/#pkg-examples<\/a><\/li>\n<li>\u7528Go\u751f\u6210\u6307\u5b9a\u957f\u5ea6\u7684\u968f\u673a\u5b57\u7b26\u4e32<br \/>\n<a href=\"https:\/\/ixyzero.com\/blog\/archives\/2535.html\">https:\/\/ixyzero.com\/blog\/archives\/2535.html<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u6700\u8fd1\u5728\u8ddf\u7740\u90dd\u6797\u8001\u5e08\u7684\u300aGo\u8bed\u8a00\u6838\u5fc336\u8bb2\u300b\u8fd9\u95e8\u8bfe\u7a0b\u91cd\u62feGo\u8bed\u8a00\u5b66\u4e60\uff08\u4e4b\u524d\u572815\u5e74\u7684\u65f6\u5019 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[563,564,1232],"class_list":["post-4126","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-go","tag-golang","tag-strings"],"views":2524,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4126","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=4126"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4126\/revisions"}],"predecessor-version":[{"id":4127,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4126\/revisions\/4127"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}