{"id":207464,"date":"2025-07-08T09:44:37","date_gmt":"2025-07-08T01:44:37","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207464\/"},"modified":"2025-07-08T09:44:37","modified_gmt":"2025-07-08T01:44:37","slug":"golang-int-%e5%88%b0-uint8-%e8%bd%ac%e6%8d%a2%ef%bc%8c%e6%9c%aa%e6%a3%80%e6%b5%8b%e5%88%b0%e6%ba%a2%e5%87%ba","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207464\/","title":{"rendered":"Golang int \u5230 uint8 \u8f6c\u6362\uff0c\u672a\u68c0\u6d4b\u5230\u6ea2\u51fa"},"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>Golang int \u5230 uint8 \u8f6c\u6362\uff0c\u672a\u68c0\u6d4b\u5230\u6ea2\u51fa<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-22 20:15:29<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5c0f\u4f19\u4f34\u4eec\u6709\u6ca1\u6709\u89c9\u5f97\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u5f88\u6709\u610f\u601d\uff1f\u6709\u610f\u601d\u5c31\u5bf9\u4e86\uff01\u4eca\u5929\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGolang int \u5230 uint8 \u8f6c\u6362\uff0c\u672a\u68c0\u6d4b\u5230\u6ea2\u51fa\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u5728\u5b66\u4e60\u4e2d\u5bf9\u5176\u4e2d\u90e8\u5206\u77e5\u8bc6\u70b9\u6709\u7591\u95ee\uff0c\u6216\u8bb8\u770b\u4e86\u672c\u6587\u5c31\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u4e00\u76f4\u5728\u5236\u4f5c\u201ca tour of go\u201d\uff0c\u5e76\u5728 pic \u51fd\u6570\u4e2d\u53d1\u73b0\u4e86\u4e00\u4e9b\u5947\u602a\u7684\u884c\u4e3a\u3002\u5b83\u6d89\u53ca <strong>int -&gt; uint8<\/strong> \u8f6c\u6362\u3002\u7a0b\u5e8f\u6267\u884c\u65f6\uff0c<strong>dx<\/strong> \u548c <strong>dy<\/strong> \u7684\u9ed8\u8ba4\u503c\u4e3a <strong>256<\/strong>\u3002\u8fd9\u6837\u5d4c\u5957 for \u5faa\u73af\u5185\u7684 <strong>x+y<\/strong> \u5c31\u4f1a\u8fbe\u5230 <strong>510<\/strong>\uff01 (255+255) <\/p>\n<\/p>\n<p> \u5c3d\u7ba1\u5982\u6b64\uff0cgolang \u5728\u5c06\u6ea2\u51fa\u7684 <strong>x+y<\/strong> \u8f6c\u6362\u4e3a uint8 \u65f6\u6ca1\u6709\u53d1\u73b0\u4efb\u4f55\u95ee\u9898\uff0c\u4f46\u662f\u5f53\u6211\u5c06\u5176\u66f4\u6539\u4e3a\u67d0\u4e2a\u786c\u7f16\u7801\u503c\uff08\u4f8b\u5982 uint8(321)\uff09\u65f6\uff0c\u6211\u7acb\u5373\u6536\u5230\u6ea2\u51fa\u9519\u8bef\u3002 <\/p>\n<\/p>\n<p> \u6709\u4eba\u80fd\u7ed9\u6211\u89e3\u91ca\u4e00\u4e0b\u8fd9\u79cd\u5947\u602a\u7684\u884c\u4e3a\u5417\uff1f<\/p>\n<pre>package main\n\nimport \"golang.org\/x\/tour\/pic\"\n\nfunc pic(dx, dy int) [][]uint8 {\n    canvas := make([][]uint8, dy)\n\n    for y := 0; y &lt; dy; y++ {\n        canvas[y] = make([]uint8, dx)\n        for x := 0; x &lt; dx; x++ {\n            canvas[y][x] = uint8(x+y) \/\/ &lt;- here it is\n        }\n    }\n    \n    return canvas;\n}\n\nfunc main() {\n    pic.show(pic)\n}<\/pre>\n<p>\u83b7\u5f97\u9519\u8bef\uff1a<\/p>\n<pre>go: finding module for package golang.org\/x\/tour\/pic\ngo: downloading golang.org\/x\/tour v0.0.0-20200508155540-0608babe047d\ngo: found golang.org\/x\/tour\/pic in golang.org\/x\/tour v0.0.0-20200508155540-0608babe047d\n.\/prog.go:11:24: constant 321 overflows uint8\n\nGo build failed.<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8fd9\u6765\u81ea\u8bed\u8a00\u89c4\u8303\uff1a<\/p>\n<p>\u7c7b\u578b\u5e38\u91cf\u7684\u503c\u5fc5\u987b\u59cb\u7ec8\u53ef\u4ee5\u7531\u5e38\u91cf\u7c7b\u578b\u7684\u503c\u51c6\u786e\u8868\u793a\u3002\u4ee5\u4e0b\u5e38\u91cf\u8868\u8fbe\u5f0f\u662f\u975e\u6cd5\u7684\uff1a<\/p>\n<p>uint(-1) \/\/ -1\u4e0d\u80fd\u8868\u793a\u4e3a\u5355\u4f4d<\/p>\n<p>int(3.14) \/\/ 3.14 \u4e0d\u80fd\u8868\u793a\u4e3a int<\/p>\n<p>\u5728\u60a8\u7684\u60c5\u51b5\u4e0b\uff0c <code>x<\/code> \u548c <code>y<\/code> \u662f <code>int<\/code>\uff0c\u56e0\u6b64 <code>x+y<\/code> \u4e5f\u662f <code>int<\/code>\uff0c\u800c <code>uint8(x+y)<\/code> \u53ea\u662f\u622a\u65ad\u7ed3\u679c\u3002\u4f46\u662f\uff0c\u6839\u636e\u8bed\u8a00\u89c4\u8303\uff0c<code>uint8(321)<\/code> \u65e0\u6548\u3002\u7136\u800c\uff0c\u8fd9\u662f\u6709\u6548\u7684\uff1a<\/p>\n<pre>i:=321\nx:=uint8(i)<\/pre>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aGolang int \u5230 uint8 \u8f6c\u6362\uff0c\u672a\u68c0\u6d4b\u5230\u6ea2\u51fa\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\uff01<\/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-207464","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207464","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=207464"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207464\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}