{"id":1839,"date":"2023-06-27T12:19:21","date_gmt":"2023-06-27T04:19:21","guid":{"rendered":"https:\/\/www.zhidianwl.com\/zhidian\/2023\/1839\/"},"modified":"2023-06-27T12:19:21","modified_gmt":"2023-06-27T04:19:21","slug":"go%e7%94%9f%e6%88%90%e7%9a%84exe%e5%a4%a7%e5%b0%8f%e6%9c%89%e9%99%90%e5%88%b6%e5%98%9b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.com\/zhidian\/2023\/1839\/","title":{"rendered":"go\u751f\u6210\u7684exe\u5927\u5c0f\u6709\u9650\u5236\u561b\uff1f"},"content":{"rendered":"

\u6807\u9898\uff1a\u7406\u89e3\u5e76\u7b80\u5316Go\u7f16\u8bd1\u751f\u6210\u7684exe\u6587\u4ef6\u5927\u5c0f<\/p>\n

\u4f5c\u4e3a\u4e00\u79cd\u5feb\u901f\u3001\u9ad8\u6548\u4e14\u7b80\u5355\u7684\u7f16\u7a0b\u8bed\u8a00\uff0cGo\u5728\u4e92\u8054\u7f51\u9886\u57df\u9010\u6e10\u6d41\u884c\u8d77\u6765\u3002\u7136\u800c\uff0c\u5728Go\u9879\u76ee\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u4f60\u53ef\u80fd\u4f1a\u6ce8\u610f\u5230\u751f\u6210\u7684exe\u6587\u4ef6\u8f83\u5927\u3002\u76f8\u5e94\u5730\uff0c\u7b80\u5316\u8fd9\u4e9b\u6587\u4ef6\u7684\u5927\u5c0f\u53ef\u4ee5\u964d\u4f4e\u78c1\u76d8\u5360\u7528\uff0c\u63d0\u9ad8\u52a0\u8f7d\u901f\u5ea6\uff0c\u4ee5\u53ca\u4f18\u5316\u4e91\u7aef\u90e8\u7f72\u7b49\u65b9\u9762\u7684\u6027\u80fd\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecdGo\u751f\u6210\u7684exe\u6587\u4ef6\u5927\u5c0f\u7684\u539f\u7406\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u5efa\u8bae\u53bb\u4f18\u5316\u5b83\u4eec\u3002<\/p>\n

\u4e00\u3001Go exe\u6587\u4ef6\u7684\u7ec4\u6210<\/p>\n

1. Go\u8fd0\u884c\u65f6\uff08runtime\uff09<\/p>\n

Go\u5728\u7f16\u8bd1\u671f\u751f\u6210\u7684exe\u6587\u4ef6\u5305\u542bGo\u8fd0\u884c\u65f6\uff08runtime\uff09\uff0c\u800cGo\u8fd0\u884c\u65f6\u8d1f<\/p>\n

<\/figure>\n<\/p>\n

\u8d23\u7ba1\u7406\u5185\u5b58\u5206\u914d\u3001\u5783\u573e\u56de\u6536\u3001Goroutines\u8c03\u5ea6\u7b49\u6838\u5fc3\u529f\u80fd\u3002\u56e0\u6b64\uff0c\u5373\u4f7f\u662f\u4e00\u4e2a\u57fa\u672c\u7684”Hello World”\u7a0b\u5e8f\uff0c\u5176\u751f\u6210\u7684exe\u6587\u4ef6\u4e5f\u4f1a\u5305\u542bGo\u8fd0\u884c\u65f6\u3002\u8fd9\u662f\u5bfc\u81f4\u5927\u5c0f\u589e\u52a0\u7684\u4e3b\u8981\u539f\u56e0\u4e4b\u4e00\u3002<\/p>\n

2. \u7a0b\u5e8f\u4ee3\u7801<\/p>\n

\u7f16\u8bd1\u540e\u7684\u7a0b\u5e8f\u4ee3\u7801\u4e5f\u4f1a\u653e\u5165exe\u6587\u4ef6\u3002\u7a0b\u5e8f\u8d8a\u5e9e\u5927\u4e14\u590d\u6742\uff0c\u5bfc\u81f4\u751f\u6210\u7684exe\u6587\u4ef6\u8d8a\u5927\u3002<\/p>\n

3. \u4f9d\u8d56\uff08\u5305\u62ec\u6807\u51c6\u5e93\u548c\u7b2c\u4e09\u65b9\u5e93\uff09<\/p>\n

\u5c3d\u7ba1Go\u5728\u7f16\u8bd1\u65f6\u5177\u6709\u5f3a\u5927\u7684\u4f18\u5316\u529f\u80fd\uff0c\u8bf8\u5982\u6b7b\u4ee3\u7801\u6d88\u9664\uff08DCE\uff09\u7b49\uff0c\u4f46\u8fd9\u4e2a\u7f16\u8bd1\u8fc7\u7a0b\u4f9d\u7136\u4f1a\u6309\u9700\u5305\u542b\u5e94\u7528\u7a0b\u5e8f\u4f9d\u8d56\u7684\u6807\u51c6\u5e93\u548c\u7b2c\u4e09\u65b9\u5e93\u3002\u82e5\u5927\u91cf\u4f7f\u7528\u4e86\u8fd9\u4e9b\u5e93\uff0c\u90a3\u4e48exe\u6587\u4ef6\u7684\u5927\u5c0f\u5c31\u53ef\u80fd\u5927\u5e45\u589e\u52a0\u3002<\/p>\n

\u4e8c\u3001\u4f18\u5316Go exeapp\u5c01\u88c5\u8f6f\u4ef6<\/a>\u6587\u4ef6\u7684\u65b9\u6cd5<\/p>\n

1. \u4f7f\u7528upx\u538b\u7f29\u5de5\u5177<\/p>\n

upx\u5de5\u5177\u63d0\u4f9b\u4e86\u4e00\u79cd\u80fd\u6709\u6548\u51cf\u5c0fGo\u751f\u4ea7\u7684exe\u6587\u4ef6\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u4f60\u53ef\u4ee5\u7b80\u5355\u5730\u538b\u7f29Go\u751f\u6210\u7684exe\u6587\u4ef6\uff1a<\/p>\n

“`shell<\/p>\n

upx -9 your_executable.exe<\/p>\n

“`<\/p>\n

2. \u5f00\u542fGo\u7f16\u8bd1\u5668\u4f18\u5316<\/p>\n

\u5728\u7f16\u8bd1Go\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528`-ldflags`\u9009\u9879\u5f00\u542f`-s`\uff08\u53bb\u6389\u7b26\u53f7\u8868\u4fe1\u606f\uff09\u548c`-w`\uff08\u53bb\u6389DWARF\u8c03\u8bd5\u4fe1\u606f\uff09\u7f16\u8bd1\u5668\u4f18\u5316\uff1a<\/p>\n

“`shell<\/p>\n

go build -ldflags=”-s -w” main.go<\/p>\n

“`<\/p>\n

3. \u5220\u9664\u4e0d\u5fc5\u8981\u7684\u4f9d\u8d56<\/p>\n

\u5b9a\u671f\u68c0\u67e5\u9879\u76ee\u4e2d\u7684\u5e93\uff0c\u5e76\u79fb\u9664\u4e0d\u5fc5\u8981\u7684\u4f9d\u8d56\uff0c\u4ee5\u51cf\u5c11exe\u6587\u4ef6\u5927\u5c0f\u3002<\/p>\n

4. \u4f7f\u7528Go modules\u5e76\u5728github.com\/golang\/go\/blob\/master\/src\/cmd\/go\/testdata\/mod\u4ee3\u66ff\u6807\u51c6\u5e93\u7684\u529f\u80fd<\/p>\n

\u8f83\u65b0\u7684Go\u7248\u672c\u652f\u6301\u4f7f\u7528modules\u7ec4\u7ec7\u548c\u7ba1\u7406\u4f9d\u8d56\u5173\u7cfb\u591a\u4e2a\u7a0b\u5e8f\u5c01\u88c5\u6210exe<\/a>\u3002\u5f53\u4f60\u4e0d\u9700\u8981\u5b8c\u6574\u7684\u6807\u51c6\u5e93\u6216\u8005\u66f4\u8f7b\u91cf\u7ea7\u7684\u5e93\u53ef\u7528\u65f6\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u5728src\/cmd\/go\/testdata\/mod\u66ff\u4ee3\u6807\u51c6\u5e93\u3002<\/p>\n

\u672c\u6587\u8be6\u7ec6\u89e3\u91ca\u4e86Go\u751f\u6210exe\u6587\u4ef6\u7684\u7ec4\u6210\u90e8\u5206\u539f\u7406\u53ca\u6587\u4ef6\u5927\u5c0f\u7684\u4f18\u5316\u65b9\u6cd5\u3002\u901a\u8fc7\u7406\u89e3\u8fd9\u4e9b\u539f\u7406\uff0c\u5e76\u91c7\u53d6\u76f8\u5e94\u7684\u4f18\u5316\u63aa\u65bd\uff0c\u4f60\u53ef\u4ee5\u5927\u5e45\u51cf\u5c0fGo\u751f\u4ea7\u7684exe\u6587\u4ef6\u5927\u5c0f\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u5728\u8bb8\u591a\u65b9\u9762\u7684\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u6807\u9898\uff1a\u7406\u89e3\u5e76\u7b80\u5316Go\u7f16\u8bd1\u751f\u6210\u7684exe\u6587\u4ef6\u5927\u5c0f\u4f5c\u4e3a\u4e00\u79cd\u5feb\u901f\u3001\u9ad8\u6548\u4e14\u7b80\u5355\u7684\u7f16\u7a0b\u8bed\u8a00\uff0cGo\u5728\u4e92\u8054\u7f51\u9886\u57df\u9010\u6e10\u6d41\u884c\u8d77\u6765\u3002\u7136\u800c\uff0c\u5728Go\u9879\u76ee\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u4f60\u53ef\u80fd\u4f1a\u6ce8\u610f\u5230\u751f\u6210\u7684exe\u6587\u4ef6\u8f83\u5927\u3002\u76f8\u5e94\u5730\uff0c\u7b80\u5316\u8fd9\u4e9b\u6587\u4ef6\u7684\u5927<\/p>\n","protected":false},"author":25,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[3909,3910,171,3018,736],"class_list":["post-1839","post","type-post","status-publish","format-standard","hentry","category-zuoexe","tag-plashexe","tag-xpk","tag-171","tag-3018","tag-736"],"_links":{"self":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/1839","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/users\/25"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/comments?post=1839"}],"version-history":[{"count":0,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/1839\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/media?parent=1839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/categories?post=1839"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/tags?post=1839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}