{"id":206960,"date":"2025-07-08T08:59:12","date_gmt":"2025-07-08T00:59:12","guid":{"rendered":"https:\/\/server.hk\/cnblog\/206960\/"},"modified":"2025-07-08T08:59:12","modified_gmt":"2025-07-08T00:59:12","slug":"%e5%9c%a8-go-%e4%b8%ad%ef%bc%8c%e5%bd%93%e5%90%8d%e7%a7%b0%e4%b8%ba%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%97%b6%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e6%9e%9a%e4%b8%be%ef%bc%88const-iota-int%ef%bc%89%e5%80%bc","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/206960\/","title":{"rendered":"\u5728 Go \u4e2d\uff0c\u5f53\u540d\u79f0\u4e3a\u5b57\u7b26\u4e32\u65f6\u5982\u4f55\u83b7\u53d6\u679a\u4e3e\uff08const iota int\uff09\u503c"},"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 Go \u4e2d\uff0c\u5f53\u540d\u79f0\u4e3a\u5b57\u7b26\u4e32\u65f6\u5982\u4f55\u83b7\u53d6\u679a\u4e3e\uff08const iota int\uff09\u503c<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-19 08:24:33<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5728 Go \u4e2d\uff0c\u5f53\u540d\u79f0\u4e3a\u5b57\u7b26\u4e32\u65f6\u5982\u4f55\u83b7\u53d6\u679a\u4e3e\uff08const iota int\uff09\u503c\u300b<\/span>\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6709\u4e00\u4e2a golang\u201c\u679a\u4e3e\u201d<\/p>\n<pre>type mytype int \nconst (\n  thing1 mytype = iota\n  thing2\n  thing3\n)<\/pre>\n<p>\u540e\u6765\uff0c\u5728\u53e6\u4e00\u4e2a go \u6587\u4ef6\/\u5305\u4e2d\uff0c\u6211\u5c06\u4e0a\u9762\u7684\u540d\u79f0\u5b9a\u4e49\u4e3a<em>\u5b57\u7b26\u4e32<\/em>\u3002<\/p>\n<p>\u5982\u4f55\u4ece<em>\u5b57\u7b26\u4e32<\/em>\u8f6c\u6362\u4e3a<em>\u679a\u4e3e<\/em>\uff1f\u6211\u77e5\u9053\u5982\u679c mytype \u662f\u5b57\u7b26\u4e32\u800c\u4e0d\u662f int\uff0c\u6211\u53ef\u4ee5\u8fd9\u6837\u505a<\/p>\n<pre>MyType(myString)<\/pre>\n<p>\u4f46\u5b83\u4e0d\u9002\u7528\u4e8e int \uff08\u56e0\u4e3a mystring \u4e0d\u662f int\uff09<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u5982\u679c\u60a8\u4e0d\u60f3\u66f4\u6539 <code>mytype<\/code> \u7684\u7c7b\u578b\uff0c\u60a8\u53ef\u4ee5\u4e3a\u5b57\u7b26\u4e32\u5b9a\u4e49\u4e00\u4e2a\u5230\u679a\u4e3e\u7684\u6620\u5c04\uff0c\u5e76\u50cf\u8fd9\u6837\u4f7f\u7528\u5b83\uff1a<\/p>\n<pre>type MyType int\nconst (\n    Thing1 MyType = iota\n    Thing2\n    Thing3\n)\n\nvar MyTypeMappings = map[string]MyType{\n    \"Thing1\": Thing1,\n    \"Thing2\": Thing2,\n    \"Thing3\": Thing3,\n}\n\nfunc main() {\n    myType := MyTypeMappings[\"Thing1\"]\n    fmt.Print(myType)\n}<\/pre>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5728 Go \u4e2d\uff0c\u5f53\u540d\u79f0\u4e3a\u5b57\u7b26\u4e32\u65f6\u5982\u4f55\u83b7\u53d6\u679a\u4e3e\uff08const iota int\uff09\u503c\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\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-206960","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206960","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=206960"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206960\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=206960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=206960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=206960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}