{"id":207852,"date":"2025-07-08T15:36:32","date_gmt":"2025-07-08T07:36:32","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207852\/"},"modified":"2025-07-08T15:36:32","modified_gmt":"2025-07-08T07:36:32","slug":"%e5%9c%a8-golang-%e4%b8%ad%e7%bb%98%e5%88%b6%e5%85%b7%e6%9c%89%e4%b8%a4%e4%b8%aa%e5%8d%8a%e5%be%84%e7%9a%84%e5%9c%86","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207852\/","title":{"rendered":"\u5728 Golang \u4e2d\u7ed8\u5236\u5177\u6709\u4e24\u4e2a\u534a\u5f84\u7684\u5706"},"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>\u5728 Golang \u4e2d\u7ed8\u5236\u5177\u6709\u4e24\u4e2a\u534a\u5f84\u7684\u5706<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-26 08:54:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5728 Golang \u4e2d\u7ed8\u5236\u5177\u6709\u4e24\u4e2a\u534a\u5f84\u7684\u5706\u300b<\/span>\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u73af\u987e\u56db\u5468\uff0c\u4f46\u627e\u4e0d\u5230\u4efb\u4f55\u5728 golang \u4e2d\u7ed8\u5236\u5706\u5708\u6709\u7528\u7684\u4e1c\u897f\u3002 \u6211\u60f3\u7ed8\u5236\u4e00\u4e2a\u5177\u6709 2 \u4e2a\u7ed9\u5b9a\uff08\u5185\u90e8\u548c\u5916\u90e8\uff09\u534a\u5f84\u7684\u7ed8\u56fe\uff0c\u5e76\u4e3a\u4e4b\u95f4\u7684\u6240\u6709\u50cf\u7d20\u7740\u8272\u3002 <\/p>\n<p>\u4e00\u79cd\u53ef\u80fd\u7684\u65b9\u6cd5\u662f\u8fed\u4ee3\u6bcf\u4e2a\u50cf\u7d20\u5e76\u4e3a\u5176\u7740\u8272\uff0c\u76f4\u5230\u521b\u5efa\u73af\u3002\u867d\u7136\uff0c\u8fd9\u770b\u8d77\u6765\u6548\u7387\u786e\u5b9e\u5f88\u4f4e\u3002 <\/p>\n<p>\u5bf9\u6b64\u7684\u4efb\u4f55\u5e2e\u52a9\u5c06\u4e0d\u80dc\u611f\u6fc0\uff01 \ud83d\ude42<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8bf7\u9605\u8bfb\u6b64\u76f8\u5173\u95ee\u9898\uff1a<\/p>\n<p>\u603b\u7ed3\u4e00\u4e0b\uff1a\u6807\u51c6 go \u5e93\u4e0d\u63d0\u4f9b\u539f\u59cb\u7ed8\u56fe\u6216\u7ed8\u753b\u529f\u80fd\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u662f\u7684\uff0c\u8981\u4e48\u4f60\u5fc5\u987b\u4f7f\u7528\u7b2c 3 \u65b9\u5e93\u6765\u753b\u5706\uff08\u4f8b\u5982 \uff09\uff0c\u8981\u4e48\u4f60\u5fc5\u987b\u81ea\u5df1\u505a\u3002\u522b\u62c5\u5fc3\uff0c\u4e00\u70b9\u4e5f\u4e0d\u96be\u3002<\/p>\n<h2>\u7ed8\u5236\u5355\u4e2a\u5706<\/h2>\n<p>\u9996\u5148\u9009\u62e9\u4e00\u79cd\u7b80\u5355\u9ad8\u6548\u7684\u753b\u5706\u7b97\u6cd5\u3002\u6211\u63a8\u8350\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u5728\u94fe\u63a5\u7684\u7ef4\u57fa\u767e\u79d1\u9875\u9762\u4e0a\u627e\u5230\u8be5\u7b97\u6cd5\u3002\u6ce8\u610f\uff1a\u5982\u679c\u4f60\u60f3\u4f7f\u7528\u5b83\uff0c\u4f60\u4e0d\u5fc5\u7406\u89e3\u5b83\u3002<\/p>\n<p>\u4f46\u662f\u6211\u4eec\u786e\u5b9e\u9700\u8981\u5728 go \u4e2d\u5b9e\u73b0\u8be5\u7b97\u6cd5\u3002\u8fd9\u76f8\u5f53\u7b80\u5355\uff1a<\/p>\n<pre>func drawcircle(img draw.image, x0, y0, r int, c color.color) {\n    x, y, dx, dy := r-1, 0, 1, 1\n    err := dx - (r * 2)\n\n    for x &gt; y {\n        img.set(x0+x, y0+y, c)\n        img.set(x0+y, y0+x, c)\n        img.set(x0-y, y0+x, c)\n        img.set(x0-x, y0+y, c)\n        img.set(x0-x, y0-y, c)\n        img.set(x0-y, y0-x, c)\n        img.set(x0+y, y0-x, c)\n        img.set(x0+x, y0-y, c)\n\n        if err &lt;= 0 {\n            y++\n            err += dy\n            dy += 2\n        }\n        if err &gt; 0 {\n            x--\n            dx += 2\n            err += dx - (r * 2)\n        }\n    }\n}<\/pre>\n<p>\u4ec5\u6b64\u800c\u5df2\u3002\u53ea\u9700\u4f20\u9012\u4e00\u4e2a\u4f60\u60f3\u8981\u7ed8\u5236\u7684\uff0c\u4ee5\u53ca\u4f60\u60f3\u8981\u7ed8\u5236\u7684\u5706\u7684\u53c2\u6570\uff08\u4e2d\u5fc3\u70b9\uff0c\u534a\u5f84\u548c\u989c\u8272\uff09\u3002<\/p>\n<p>\u8ba9\u6211\u4eec\u770b\u770b\u5b83\u7684\u5b9e\u9645\u6548\u679c\u3002\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u56fe\u50cf\uff0c\u5728\u5176\u4e0a\u753b\u4e00\u4e2a\u5706\u5708\uff0c\u7136\u540e\u5c06\u56fe\u50cf\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002\u8fd9\u5c31\u662f\u6240\u9700\u8981\u7684\uff1a<\/p>\n<pre>img := image.newrgba(image.rect(0, 0, 100, 100))\ndrawcircle(img, 40, 40, 30, color.rgba{255, 0, 0, 255})\n\nbuf := &amp;bytes.buffer{}\nif err := png.encode(buf, img); err != nil {\n    panic(err)\n}\nif err := ioutil.writefile(\"circle.png\", buf.bytes(), 0666); err != nil {\n    panic(err)\n}<\/pre>\n<p>\u6ce8\u610f\uff1a\u60a8\u8fd8\u53ef\u4ee5\u5c06\u56fe\u50cf\u76f4\u63a5\u7f16\u7801\u4e3a \u5e76\u201c\u8df3\u8fc7\u201d\u5185\u5b58\u7f13\u51b2\u533a\u3002\u8fd9\u53ea\u662f\u4e3a\u4e86\u6f14\u793a\uff0c\u5e76\u9a8c\u8bc1\u6211\u4eec\u7684\u5b9e\u73b0\u6548\u679c\u3002<\/p>\n<h2>\u7ed8\u5236\u4e00\u4e2a\u5706\u73af\uff08\u586b\u5145\u4e24\u4e2a\u5706\u4e4b\u95f4\u7684\u7a7a\u95f4\uff09<\/h2>\n<p>\u5982\u679c\u60a8\u60f3\u81ea\u5df1\u5b9e\u73b0\u5b83\uff0c\u8fd9\u5e76\u4e0d\u662f\u90a3\u4e48\u7b80\u5355\uff0c\u4f46\u5728\u8fd9\u91cc\u4f7f\u7528\u7b2c 3 \u65b9\u5e93\u786e\u5b9e\u4f1a\u6d3e\u4e0a\u7528\u573a\u3002<\/p>\n<p>\u867d\u7136\u5b83\u4eec\u4e2d\u7684\u5927\u591a\u6570\u4e0d\u5305\u542b\u73af\u5f62\u7ed8\u5236\u652f\u6301\uff0c\u4f46\u5b83\u4eec\u786e\u5b9e\u652f\u6301\u5706\u5f62\u7ed8\u5236\uff0c\u5e76\u4e14\u60a8\u53ef\u4ee5\u8bbe\u7f6e\u7528\u4e8e\u7ed8\u5236\u5706\u5f62\u7684\u7ebf\u6761\u7684\u5bbd\u5ea6\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u5c06\u7ebf\u5bbd\u8bbe\u7f6e\u4e3a\u5706\u7684 2 \u534a\u5f84\u4e4b\u5dee\u7684\u503c\u3002\u5e76\u4ee5\u65b0\u7684\u534a\u5f84\u7ed8\u5236\u4e00\u4e2a\u5706\uff0c\u8be5\u5706\u7684\u534a\u5f84\u4e3a2\u4e2a\u539f\u59cb\u534a\u5f84\u7684\u7b97\u672f\u4e2d\u5fc3\u3002<\/p>\n<p>\u8fd9\u662f\u7b97\u6cd5\uff08\u8fd9\u4e0d\u662f\u53ef\u8fd0\u884c\u7684\u4ee3\u7801\uff09\uff1a<\/p>\n<pre>\/\/ Helper functions abstracting the library you choose:\n\nfunc setColor(c color.Color) {}\nfunc setLineWidth(width float64) {}\nfunc drawCircle(r, x, y float64) {}\n\n\/\/ fillRing draws a ring, where r1 and r2 are 2 concentric circles,\n\/\/ the boundaries of the ring, (x, y) being the center point.\nfunc fillRing(r1, r2, x, y float64, c color.color) {\n    \/\/ Set drawing color:\n    setColor(c)\n\n    \/\/ Set line width:\n    width := r2 - r1\n    if width &lt; 0 {\n        width = -width\n    }\n    setLineWidth(width)\n\n    \/\/ And finally draw a circle which will be a ring:\n    r := (r2 + r1) \/ 2\n    drawCircle(r, x, y)\n}<\/pre>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGolang\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/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-207852","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207852","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=207852"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207852\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}