{"id":799,"date":"2026-02-04T09:36:58","date_gmt":"2026-02-04T09:36:58","guid":{"rendered":"https:\/\/opshub.com.cn\/?p=799"},"modified":"2026-02-04T09:38:16","modified_gmt":"2026-02-04T09:38:16","slug":"go%e8%af%ad%e8%a8%80%e4%ba%a4%e5%8f%89%e7%bc%96%e8%af%91%e5%91%bd%e4%bb%a4%e5%92%8c%e5%8f%82%e6%95%b0%e4%bc%98%e5%8c%96%ef%bc%88%e9%93%be%e6%8e%a5%e9%9d%99%e6%80%81%e5%8c%96%e3%80%81%e7%bc%a9%e5%b0%8f","status":"publish","type":"post","link":"https:\/\/opshub.com.cn\/?p=799","title":{"rendered":"go\u8bed\u8a00\u4ea4\u53c9\u7f16\u8bd1\u547d\u4ee4\u548c\u53c2\u6570\u4f18\u5316\uff08\u94fe\u63a5\u9759\u6001\u5316\u3001\u7f29\u5c0f\u6587\u4ef6\u4f53\u79ef\uff09"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>CGO_ENABLED=0 GOOS=linux GOARCH=amd64 ..\/go\/bin\/go build -ldflags=\"-s -w\" -trimpath -v -o \/app\/testapp .\/main<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6761\u547d\u4ee4\u662f\u7528\u4e8e\u5728\u975e Linux \u73af\u5883\u4e0b\uff0c\u7f16\u8bd1\u51fa\u9002\u7528\u4e8e Linux amd64 \u67b6\u6784\u7684 Xray \u53ef\u6267\u884c\u6587\u4ef6\uff0c\u5e76\u4e14\u505a\u4e86\u4e00\u7cfb\u5217\u4f18\u5316\u3002<\/p>\n\n\n\n<p>\u90e8\u5206\u542b\u4e49<\/p>\n\n\n\n<p><code>CGO_ENABLED=0<\/code>\u7981\u7528 CGO \u7279\u6027\uff0c\u7f16\u8bd1\u51fa<strong>\u7eaf\u9759\u6001\u94fe\u63a5<\/strong>\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\uff08\u4e0d\u4f9d\u8d56\u7cfb\u7edf\u7684 C \u5e93\uff09\uff0c\u4fdd\u8bc1\u5728\u5404\u79cd Linux \u53d1\u884c\u7248\u4e0a\u90fd\u80fd\u8fd0\u884c<\/p>\n\n\n\n<p><code>GOOS=linux<\/code>\u6307\u5b9a\u7f16\u8bd1\u76ee\u6807\u64cd\u4f5c\u7cfb\u7edf\u4e3a Linux<\/p>\n\n\n\n<p><code>GOARCH=amd64<\/code>\u6307\u5b9a\u7f16\u8bd1\u76ee\u6807\u67b6\u6784\u4e3a 64 \u4f4d x86 \u67b6\u6784\uff08\u4e5f\u5c31\u662f\u5e38\u8bf4\u7684 x86_64\/amd64\uff09<\/p>\n\n\n\n<p><code>..\/go\/bin\/go<\/code>\u6307\u5b9a\u4f7f\u7528\u7684 Go \u7f16\u8bd1\u5668\u8def\u5f84\uff08\u4e0d\u662f\u7cfb\u7edf\u9ed8\u8ba4\u7684 go\uff0c\u800c\u662f\u81ea\u5b9a\u4e49\u8def\u5f84\u4e0b\u7684 go \u7a0b\u5e8f\uff09<\/p>\n\n\n\n<p><code>build<\/code>Go \u7f16\u8bd1\u5668\u7684\u7f16\u8bd1\u547d\u4ee4\uff0c\u7528\u4e8e\u6784\u5efa\u53ef\u6267\u884c\u6587\u4ef6<\/p>\n\n\n\n<p><code>-ldflags=\"-s -w\"<\/code>\u94fe\u63a5\u5668\u53c2\u6570\u4f18\u5316\uff1a- <code>-s<\/code>\uff1a\u79fb\u9664\u7b26\u53f7\u8868\uff08\u51cf\u5c11\u6587\u4ef6\u4f53\u79ef\uff0c\u65e0\u6cd5\u7528 <code>nm<\/code> \u67e5\u770b\u7b26\u53f7\uff09- <code>-w<\/code>\uff1a\u79fb\u9664\u8c03\u8bd5\u4fe1\u606f\uff08\u65e0\u6cd5\u7528 <code>gdb<\/code> \u8c03\u8bd5\uff0c\u8fdb\u4e00\u6b65\u51cf\u5c0f\u4f53\u79ef\uff09<\/p>\n\n\n\n<p><code>-trimpath<\/code>\u79fb\u9664\u7f16\u8bd1\u4ea7\u7269\u4e2d\u7684\u6587\u4ef6\u7edd\u5bf9\u8def\u5f84\u4fe1\u606f\uff0c\u8ba9\u7f16\u8bd1\u7ed3\u679c\u66f4\u5e72\u51c0\uff0c\u4e5f\u907f\u514d\u66b4\u9732\u7f16\u8bd1\u73af\u5883\u7684\u8def\u5f84<\/p>\n\n\n\n<p><code>-v<\/code>\u7f16\u8bd1\u65f6\u8f93\u51fa\u8be6\u7ec6\u65e5\u5fd7\uff0c\u663e\u793a\u6b63\u5728\u7f16\u8bd1\u7684\u5305\u540d\uff0c\u65b9\u4fbf\u6392\u67e5\u7f16\u8bd1\u95ee\u9898<\/p>\n\n\n\n<p><code>-o \/app\/testapp<\/code>\u6307\u5b9a\u7f16\u8bd1\u8f93\u51fa\u7684\u53ef\u6267\u884c\u6587\u4ef6\u8def\u5f84\u548c\u540d\u79f0\uff08\u8f93\u51fa\u5230 <code>\/app\/testapp<\/code>\uff09<\/p>\n\n\n\n<p><code>.\/main<\/code>\u6307\u5b9a\u7f16\u8bd1\u7684\u5165\u53e3\u6587\u4ef6\u4e3a\u5f53\u524d\u76ee\u5f55\u4e0b\u7684 <code>main.go<\/code>\uff08\u6216 main \u5305\uff09<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u8865\u5145\u8bf4\u660e<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u524d\u7f6e\u6761\u4ef6<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u4f60\u9700\u8981\u786e\u4fdd <code>..\/go\/bin\/go<\/code> \u8fd9\u4e2a\u8def\u5f84\u4e0b\u6709\u53ef\u6267\u884c\u7684 Go \u7f16\u8bd1\u5668\uff08\u7248\u672c\u9700\u517c\u5bb9 Xray \u6e90\u7801\uff09\u3002<\/li>\n\n\n\n<li>\u6e90\u7801\u76ee\u5f55\u4e0b\u5fc5\u987b\u6709 <code>main.go<\/code> \u6587\u4ef6\uff08\u6216 main \u5305\uff09\uff0c\u4e14\u4ee3\u7801\u80fd\u6b63\u5e38\u7f16\u8bd1\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u4f18\u5316\u6548\u679c<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u7981\u7528 CGO + <code>-ldflags=\"-s -w\"<\/code> + <code>-trimpath<\/code> \u7ec4\u5408\uff0c\u80fd\u663e\u8457\u51cf\u5c0f\u751f\u6210\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\u4f53\u79ef\uff08\u901a\u5e38\u80fd\u51cf\u5c11 30% \u4ee5\u4e0a\uff09\u3002<\/li>\n\n\n\n<li>\u7eaf\u9759\u6001\u7f16\u8bd1\u7684\u6587\u4ef6\uff0c\u5728 Docker \u955c\u50cf\uff08\u5982 alpine \u57fa\u7840\u955c\u50cf\uff09\u4e2d\u65e0\u9700\u4f9d\u8d56\u4efb\u4f55\u52a8\u6001\u5e93\uff0c\u8fd0\u884c\u66f4\u7a33\u5b9a\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u5e38\u89c1\u6269\u5c55<\/strong>\uff1a\u5982\u679c\u9700\u8981\u8fdb\u4e00\u6b65\u538b\u7f29\uff0c\u7f16\u8bd1\u540e\u53ef\u4f7f\u7528 <code>upx<\/code> \u5de5\u5177\uff08\u9700\u63d0\u524d\u5b89\u88c5\uff09\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>upx -9 -k \/app\/testapp<\/code><\/pre>\n\n\n\n<p>\u603b\u7ed3<br>\u8fd9\u6761\u547d\u4ee4\u7684\u6838\u5fc3\u662f\u8de8\u5e73\u53f0\u7f16\u8bd1\uff1a\u5728\u975e Linux \u73af\u5883\u4e0b\u751f\u6210 Linux amd64 \u67b6\u6784\u7684 Xray \u53ef\u6267\u884c\u6587\u4ef6\u3002<br>\u5173\u952e\u4f18\u5316\u70b9\uff1a\u7981\u7528 CGO \u5b9e\u73b0\u7eaf\u9759\u6001\u7f16\u8bd1\uff0c\u901a\u8fc7\u94fe\u63a5\u5668\u53c2\u6570\u548c\u8def\u5f84\u88c1\u526a\u51cf\u5c0f\u6587\u4ef6\u4f53\u79ef\u3002<br>\u8f93\u51fa\u7ed3\u679c\uff1a\u5728 \/app \u76ee\u5f55\u4e0b\u751f\u6210\u540d\u4e3a testapp \u7684\u4f18\u5316\u540e\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u53ef\u76f4\u63a5\u5728 Linux x86_64 \u7cfb\u7edf\u8fd0\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u6761\u547d\u4ee4\u662f\u7528\u4e8e\u5728\u975e Linux \u73af\u5883\u4e0b\uff0c\u7f16\u8bd1\u51fa\u9002\u7528\u4e8e Linux amd64 \u67b6\u6784\u7684 Xray \u53ef\u6267\u884c\u6587\u4ef6\uff0c\u5e76\u4e14\u505a\u4e86\u4e00\u7cfb\u5217\u4f18\u5316\u3002 \u90e8\u5206\u542b\u4e49 CGO_ENABLED=0\u7981\u7528 CGO \u7279\u6027\uff0c\u7f16\u8bd1\u51fa\u7eaf\u9759\u6001\u94fe\u63a5\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\uff08\u4e0d\u4f9d\u8d56\u7cfb\u7edf\u7684 C \u5e93\uff09\uff0c\u4fdd\u8bc1\u5728\u5404\u79cd Linux \u53d1\u884c\u7248\u4e0a\u90fd\u80fd\u8fd0\u884c GOOS=linux\u6307\u5b9a\u7f16\u8bd1\u76ee\u6807\u64cd\u4f5c\u7cfb\u7edf\u4e3a Linux GOARCH=amd64\u6307\u5b9a\u7f16\u8bd1\u76ee\u6807\u67b6\u6784\u4e3a 64 \u4f4d x86 \u67b6\u6784\uff08\u4e5f\u5c31\u662f\u5e38\u8bf4\u7684 x86_64\/amd64\uff09 ..\/go\/bin\/go\u6307\u5b9a\u4f7f\u7528\u7684 Go \u7f16\u8bd1\u5668\u8def\u5f84\uff08\u4e0d\u662f\u7cfb\u7edf\u9ed8\u8ba4\u7684 go\uff0c\u800c\u662f\u81ea\u5b9a\u4e49\u8def\u5f84\u4e0b\u7684 go \u7a0b\u5e8f\uff09 buildGo \u7f16\u8bd1\u5668\u7684\u7f16\u8bd1\u547d\u4ee4\uff0c\u7528\u4e8e\u6784\u5efa\u53ef\u6267\u884c\u6587\u4ef6 -ldflags=&#8221;-s -w&#8221;\u94fe\u63a5\u5668\u53c2\u6570\u4f18\u5316\uff1a- -s\uff1a\u79fb\u9664\u7b26\u53f7\u8868\uff08\u51cf\u5c11\u6587\u4ef6\u4f53\u79ef\uff0c\u65e0\u6cd5\u7528 nm \u67e5\u770b\u7b26\u53f7\uff09- -w\uff1a\u79fb\u9664\u8c03\u8bd5\u4fe1\u606f\uff08\u65e0\u6cd5\u7528 gdb \u8c03\u8bd5\uff0c\u8fdb\u4e00\u6b65\u51cf\u5c0f\u4f53\u79ef\uff09 -trimpath\u79fb\u9664\u7f16\u8bd1\u4ea7\u7269\u4e2d\u7684\u6587\u4ef6\u7edd\u5bf9\u8def\u5f84\u4fe1\u606f\uff0c\u8ba9\u7f16\u8bd1\u7ed3\u679c\u66f4\u5e72\u51c0\uff0c\u4e5f\u907f\u514d\u66b4\u9732\u7f16\u8bd1\u73af\u5883\u7684\u8def\u5f84 -v\u7f16\u8bd1\u65f6\u8f93\u51fa\u8be6\u7ec6\u65e5\u5fd7\uff0c\u663e\u793a\u6b63\u5728\u7f16\u8bd1\u7684\u5305\u540d\uff0c\u65b9\u4fbf\u6392\u67e5\u7f16\u8bd1\u95ee\u9898 -o \/app\/testapp\u6307\u5b9a\u7f16\u8bd1\u8f93\u51fa\u7684\u53ef\u6267\u884c\u6587\u4ef6\u8def\u5f84\u548c\u540d\u79f0\uff08\u8f93\u51fa\u5230 \/app\/testapp\uff09 .\/main\u6307\u5b9a\u7f16\u8bd1\u7684\u5165\u53e3\u6587\u4ef6\u4e3a\u5f53\u524d\u76ee\u5f55\u4e0b\u7684 main.go\uff08\u6216 main \u5305\uff09 \u8865\u5145\u8bf4\u660e \u603b\u7ed3\u8fd9\u6761\u547d\u4ee4\u7684\u6838\u5fc3\u662f\u8de8\u5e73\u53f0\u7f16\u8bd1\uff1a\u5728\u975e Linux \u73af\u5883\u4e0b\u751f\u6210 Linux amd64 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-799","post","type-post","status-publish","format-standard","hentry","category-9"],"_links":{"self":[{"href":"https:\/\/opshub.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/799","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/opshub.com.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/opshub.com.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/opshub.com.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/opshub.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=799"}],"version-history":[{"count":1,"href":"https:\/\/opshub.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/799\/revisions"}],"predecessor-version":[{"id":801,"href":"https:\/\/opshub.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/799\/revisions\/801"}],"wp:attachment":[{"href":"https:\/\/opshub.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/opshub.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/opshub.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}