{"id":1711,"date":"2023-09-25T23:36:53","date_gmt":"2023-09-25T15:36:53","guid":{"rendered":"https:\/\/blog.iotot.com\/?p=1711"},"modified":"2023-09-25T23:36:53","modified_gmt":"2023-09-25T15:36:53","slug":"go%e8%af%ad%e8%a8%80%e6%80%8e%e4%b9%88%e5%b0%86%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%88%86%e5%89%b2%e4%b8%ba%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/blog.iotot.com\/?p=1711","title":{"rendered":"go\u8bed\u8a00\u600e\u4e48\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u4e3a\u6570\u7ec4"},"content":{"rendered":"<p>\u5728Go\u8bed\u8a00\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>strings<\/code>\u5305\u4e2d\u7684<code>Split<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u4e3a\u6570\u7ec4\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package main\r\n\r\nimport (\r\n    \"fmt\"\r\n    \"strings\"\r\n)\r\n\r\nfunc main() {\r\n    \/\/ \u8981\u5206\u5272\u7684\u5b57\u7b26\u4e32\r\n    str := \"Hello,World,Go\"\r\n\r\n    \/\/ \u4f7f\u7528\u9017\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u4e3a\u6570\u7ec4\r\n    arr := strings.Split(str, \",\")\r\n\r\n    \/\/ \u6253\u5370\u5206\u5272\u540e\u7684\u6570\u7ec4\r\n    fmt.Println(arr)\r\n}\r\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86<code>strings<\/code>\u5305\uff0c\u7136\u540e\u4f7f\u7528<code>strings.Split<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32<code>str<\/code>\u4ee5\u9017\u53f7<code>,<\/code>\u4f5c\u4e3a\u5206\u9694\u7b26\u5206\u5272\u4e3a\u6570\u7ec4<code>arr<\/code>\u3002\u6700\u540e\uff0c\u6211\u4eec\u6253\u5370\u4e86\u5206\u5272\u540e\u7684\u6570\u7ec4\u3002<\/p>\n<p>\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">[Hello World Go]\r\n<\/pre>\n<p>\u8fd9\u6837\uff0c\u4f60\u5c31\u6210\u529f\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u4e0d\u540c\u7684\u5206\u9694\u7b26\u6765\u5206\u5272\u5b57\u7b26\u4e32\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>0:250;1:205;2:223;3:225;4:0;5:0; \u8fd9\u6837\u7684\u5b57\u7b26\u4e32\u5206\u5272\u4e3a\u6570\u7ec4???<\/p>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u5206\u53f7(<code>;<\/code>)\u548c\u5192\u53f7(<code>:<\/code>)\u4f5c\u4e3a\u5206\u9694\u7b26\u6765\u5c06\u8fd9\u6837\u7684\u5b57\u7b26\u4e32\u5206\u5272\u4e3a\u6570\u7ec4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package main\r\n\r\nimport (\r\n    \"fmt\"\r\n    \"strings\"\r\n)\r\n\r\nfunc main() {\r\n    \/\/ \u8981\u5206\u5272\u7684\u5b57\u7b26\u4e32\r\n    str := \"0:250;1:205;2:223;3:225;4:0;5:0;\"\r\n\r\n    \/\/ \u4f7f\u7528\u5206\u53f7\u548c\u5192\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u4e3a\u6570\u7ec4\r\n    parts := strings.Split(str, \";\")\r\n\r\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u7528\u4e8e\u5b58\u50a8\u5206\u5272\u7ed3\u679c\u7684\u5207\u7247\uff08\u6570\u7ec4\uff09\r\n    result := make([]string, len(parts))\r\n\r\n    \/\/ \u904d\u5386\u5206\u5272\u540e\u7684\u90e8\u5206\uff0c\u8fdb\u4e00\u6b65\u5206\u5272\u5e76\u5b58\u50a8\u5728result\u5207\u7247\u4e2d\r\n    for i, part := range parts {\r\n        keyValue := strings.Split(part, \":\")\r\n        if len(keyValue) == 2 {\r\n            result[i] = keyValue[1]\r\n        } else {\r\n            result[i] = \"\"\r\n        }\r\n    }\r\n\r\n    \/\/ \u6253\u5370\u5206\u5272\u540e\u7684\u6570\u7ec4\r\n    fmt.Println(result)\r\n}\r\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528\u5206\u53f7(<code>;<\/code>)\u5c06\u539f\u59cb\u5b57\u7b26\u4e32\u5206\u5272\u4e3a\u90e8\u5206\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u5207\u7247\uff08<code>result<\/code>\uff09\u6765\u5b58\u50a8\u5206\u5272\u7ed3\u679c\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u904d\u5386\u5206\u5272\u540e\u7684\u90e8\u5206\uff0c\u518d\u6b21\u4f7f\u7528\u5192\u53f7(<code>:<\/code>)\u5c06\u6bcf\u4e2a\u90e8\u5206\u5206\u5272\u6210\u952e\u503c\u5bf9\uff0c\u5e76\u5c06\u503c\u90e8\u5206\u5b58\u50a8\u5728<code>result<\/code>\u5207\u7247\u4e2d\u3002\u6700\u7ec8\uff0c\u6211\u4eec\u6253\u5370\u4e86\u5206\u5272\u540e\u7684\u6570\u7ec4\u3002<\/p>\n<p>\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">[250 205 223 225 0 0]\r\n<\/pre>\n<p>\u8fd9\u6837\uff0c\u4f60\u6210\u529f\u5c06\u5305\u542b\u952e\u503c\u5bf9\u7684\u5b57\u7b26\u4e32\u5206\u5272\u4e3a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\u3002\u6839\u636e\u9700\u8981\uff0c\u4f60\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5904\u7406\u8fd9\u4e2a\u6570\u7ec4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Go\u8bed\u8a00\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528strings\u5305\u4e2d\u7684Split\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u4e3a\u6570\u7ec4\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a package [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.iotot.com\/index.php?rest_route=\/wp\/v2\/posts\/1711"}],"collection":[{"href":"https:\/\/blog.iotot.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.iotot.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.iotot.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.iotot.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1711"}],"version-history":[{"count":1,"href":"https:\/\/blog.iotot.com\/index.php?rest_route=\/wp\/v2\/posts\/1711\/revisions"}],"predecessor-version":[{"id":1712,"href":"https:\/\/blog.iotot.com\/index.php?rest_route=\/wp\/v2\/posts\/1711\/revisions\/1712"}],"wp:attachment":[{"href":"https:\/\/blog.iotot.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iotot.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iotot.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}