{"id":207816,"date":"2025-07-08T09:35:29","date_gmt":"2025-07-08T01:35:29","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207816\/"},"modified":"2025-07-08T09:35:29","modified_gmt":"2025-07-08T01:35:29","slug":"go-%e4%b8%ad%e5%a4%84%e7%90%86%e6%b7%b7%e5%90%88%e7%b1%bb%e5%9e%8b%e7%9a%84%e5%93%88%e5%b8%8c%e6%9b%bf%e4%bb%a3%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207816\/","title":{"rendered":"Go \u4e2d\u5904\u7406\u6df7\u5408\u7c7b\u578b\u7684\u54c8\u5e0c\u66ff\u4ee3\u65b9\u6848"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>Go \u4e2d\u5904\u7406\u6df7\u5408\u7c7b\u578b\u7684\u54c8\u5e0c\u66ff\u4ee3\u65b9\u6848<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-25 20:09:37<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u5c31\u6574\u7406\u5206\u4eab\u300aGo \u4e2d\u5904\u7406\u6df7\u5408\u7c7b\u578b\u7684\u54c8\u5e0c\u66ff\u4ee3\u65b9\u6848\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9Golang\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u505a\u4e00\u4e2a\u7f16\u7a0b\u7ec3\u4e60\u6765\u719f\u6089 go\u3002\u6211\u76ee\u524d\u6b63\u5728\u7f16\u5199\u4e00\u4e2a\u89e3\u6790\u5668\uff0c\u5b83\u5c06\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u5e26\u6709\u53c2\u6570\u7684\u547d\u4ee4\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>c w h           should create a new canvas of width w and height h.\nb x y c         should fill the entire area connected to (x,y) with \"colour\" c.\nq               should quit the program.<\/pre>\n<p>\u9996\u5148\uff0c\u6211\u5f00\u59cb\u4f7f\u7528\u54c8\u5e0c\u6765\u4fdd\u5b58\u53c2\u6570\uff0c\u4f8b\u5982<code>w<\/code> <code>h<\/code>\u3002\u4f46\u8fd9\u662f\u4e0d\u7075\u6d3b\u7684\uff0c\u6b63\u5982\u60a8\u6240\u770b\u5230\u7684 <code>c<\/code> \u662f\u4e00\u79cd\u989c\u8272\uff0c\u5b83\u5c06\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u800c\u5176\u4ed6\u53c2\u6570\u662f\u6574\u6570\u3002<\/p>\n<p>\u6211\u662f\u8fd9\u6837\u5f00\u59cb\u7684\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"errors\"\n    \"strconv\"\n    \"strings\"\n)\n\ntype command struct {\n    id   string\n    args map[string]int \/\/ won't work because args can be of mixed types\n}\n\nfunc parsecommand(input string) (command, error) {\n    if input == \"\" {\n        return command{}, errors.new(\"no input\")\n    }\n\n    commandparts := strings.split(input, \" \")\n\n    switch commandparts[0] {\n    case \"c\":\n        if (len(commandparts)) != 3 {\n            return command{}, errors.new(\"c (create) requires 2 arguments\")\n        }\n\n        w, err := strconv.atoi(commandparts[1])\n\n        if err != nil {\n            return command{}, errors.new(\"width must be an integer\")\n        }\n\n        h, err := strconv.atoi(commandparts[2])\n\n        if err != nil {\n            return command{}, errors.new(\"height must be an integer\")\n        }\n\n        return command{\n            id: \"create\",\n            args: map[string]int{\n                \"w\": w,\n                \"h\": h,\n            },\n        }, nil\n    case \"b\":\n        if (len(commandparts)) != 4 {\n            return command{}, errors.new(\"b (bucket fill) requires 3 arguments\")\n        }\n\n        x, err := strconv.atoi(commandparts[1])\n\n        if err != nil {\n            return command{}, errors.new(\"x must be an integer\")\n        }\n\n        y, err := strconv.atoi(commandparts[2])\n\n        if err != nil {\n            return command{}, errors.new(\"y must be an integer\")\n        }\n\n        return command{\n            id: \"bucketfill\",\n            args: map[string]int{\n                \"x\": x,\n                \"y\": y,\n                \"c\": commandparts[3], \/\/ this should be a string!\n            },\n        }, nil\n    case \"q\":\n        return command{\n            id: \"quit\",\n        }, nil\n    default:\n        return command{}, errors.new(\"command not supported\")\n    }\n}<\/pre>\n<p>\u6211\u7684\u95ee\u9898\u662f\uff0c\u5982\u679c\u6211\u60f3\u8fd4\u56de\u7684\u53c2\u6570\u662f\u53d8\u91cf\u5e76\u4e14\u5177\u6709\u6df7\u5408\u7c7b\u578b\uff0c\u6211\u5e94\u8be5\u5982\u4f55\u5c06\u8f93\u5165\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u547d\u4ee4\uff1f\u8c22\u8c22\u3002<\/p>\n<p>p.s \u547d\u4ee4\u53ef\u4ee5\u81ea\u7531\u8f93\u5165\u5e76\u4fee\u6539\u7ec8\u7aef\u4e2d\u7684\u5047\u753b\u5e03\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>enter command: C 20 4\n----------------------\n|                    |\n|                    |\n|                    |\n|                    |\n----------------------\n\n\/\/ Didn't mention this one but it's a Line if you didn't guess\nenter command: L 1 2 6 2\n----------------------\n|                    |\n|xxxxxx              |\n|                    |\n|                    |\n----------------------<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u5bf9 <code>command<\/code> \u7684\u65b9\u6cd5\u4e0d\u6b63\u786e\u3002 <code>command<\/code> \u662f\u53ef\u4ee5\u5e94\u7528\u4e8e\u753b\u5e03\u7684\u4e1c\u897f\u3002\u6240\u4ee5\u6211\u4eec\u8fd9\u6837\u8bf4\uff1a<\/p>\n<pre>type canvas struct{ ... }\n\ntype command interface {\n    apply(canvas *canvas)\n}<\/pre>\n<p>\u73b0\u5728\u6709\u591a\u79cd\u547d\u4ee4\uff0c\u6bcf\u79cd\u547d\u4ee4\u90fd\u6709\u81ea\u5df1\u7684\u53c2\u6570\u3002\u4f46\u662f\uff0c\u5f53\u7528\u4f5c\u547d\u4ee4\u65f6\uff0c\u8c03\u7528\u8005\u4e0d\u5fc5\u5173\u5fc3\u8fd9\u4e9b\u53c2\u6570\u662f\u4ec0\u4e48\u3002<\/p>\n<pre>type createcommand struct {\n    width  int\n    height int\n}\n\nfunc (c createcommand) apply(canvas *canvas) { ... }\n\ntype bucketfillcommand struct {\n    x     int\n    y     int\n    color string\n}\n\nfunc (c bucketfillcommand) apply(canvas *canvas) { ... }\n\ntype quitcommand struct{}\n\nfunc (c quitcommand) apply(canvas *canvas) { ... }<\/pre>\n<p>\u7136\u540e\u4f60\u53ef\u4ee5\u89e3\u6790\u5b83\u4eec\uff08\u6211\u53ef\u80fd\u4f1a\u5c06\u6240\u6709\u89e3\u6790\u653e\u5165\u51fd\u6570\u4e2d\uff0c\u4f46\u8fd9\u5f88\u597d\uff09\u3002<\/p>\n<pre>func parseCommand(input string) (command, error) {\n    if input == \"\" {\n        return nil, errors.New(\"No input\")\n    }\n\n    commandParts := strings.Split(input, \" \")\n\n    switch commandParts[0] {\n    case \"C\":\n        if (len(commandParts)) != 3 {\n            return nil, errors.New(\"C (create) requires 2 arguments\")\n        }\n\n        w, err := strconv.Atoi(commandParts[1])\n\n        if err != nil {\n            return nil, errors.New(\"width must be an integer\")\n        }\n\n        h, err := strconv.Atoi(commandParts[2])\n\n        if err != nil {\n            return nil, errors.New(\"height must be an integer\")\n        }\n\n        return createCommand{width: w, height: h}, nil\n    case \"B\":\n        if (len(commandParts)) != 4 {\n            return nil, errors.New(\"B (Bucket Fill) requires 3 arguments\")\n        }\n\n        x, err := strconv.Atoi(commandParts[1])\n\n        if err != nil {\n            return nil, errors.New(\"x must be an integer\")\n        }\n\n        y, err := strconv.Atoi(commandParts[2])\n\n        if err != nil {\n            return nil, errors.New(\"y must be an integer\")\n        }\n\n        return bucketFillCommand{x: x, y: y, color: commandParts[3]}, nil\n    case \"Q\":\n        return quitCommand{}, nil\n    default:\n        return nil, errors.New(\"Command not supported\")\n    }\n}<\/pre>\n<p>\u8bf7\u6ce8\u610f\uff0c\u5f53\u51fa\u73b0\u6545\u969c\u65f6\uff0c\u6b64\u547d\u4ee4\u4f1a\u8fd4\u56de <code>nil<\/code> \u4f5c\u4e3a\u547d\u4ee4\uff0c\u800c\u4e0d\u662f <code>command{}<\/code>\u3002<\/p>\n<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300aGo \u4e2d\u5904\u7406\u6df7\u5408\u7c7b\u578b\u7684\u54c8\u5e0c\u66ff\u4ee3\u65b9\u6848\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207816","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207816","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=207816"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207816\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}