{"id":1343,"date":"2023-07-31T15:48:20","date_gmt":"2023-07-31T07:48:20","guid":{"rendered":"https:\/\/www.yimenyun.cn\/emen\/?p=1343"},"modified":"2023-07-31T15:48:20","modified_gmt":"2023-07-31T07:48:20","slug":"go-build-linux%e6%98%af%e5%81%9a%e5%95%a5%e7%9a%84","status":"publish","type":"post","link":"https:\/\/www.yimenyun.cn\/emen\/1343\/","title":{"rendered":"go build linux\u662f\u505a\u5565\u7684?"},"content":{"rendered":"

GoBuildLinux\u662f\u4e00\u6b3e\u7528\u4e8e\u5728Linux\u7cfb\u7edf\u4e2d\u6784\u5efaGo\u8bed\u8a00\u5e94\u7528\u7a0b\u5e8f\u7684\u5de5\u5177\u3002\u5b83\u80fd\u591f\u81ea\u52a8\u5316\u6784\u5efa\u3001\u6253\u5305\u548c\u53d1\u5e03Go\u8bed\u8a00\u5e94\u7528\u7a0b\u5e8f\uff0c\u7b80\u5316\u4e86\u5f00\u53d1\u4eba\u5458\u7684\u5de5\u4f5c\u6d41\u7a0b\uff0c\u63d0\u9ad8\u4e86\u5f00\u53d1\u6548\u7387\u3002<\/p>\n

GoBuildLinux\u7684\u539f\u7406\u4e3b\u8981\u662f\u57fa\u4e8eLinux\u7cfb\u7edf\u4e2d\u7684\u4e00\u4e9b\u5e38\u7528\u5de5\u5177\u548c\u6280\u672f\uff0c\u5982Makefile\u3001Git\u3001Docker\u7b49\u3002\u4e0b\u9762\u662fGoBuildLinux\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff1a<\/p>\n

1. Makefile<\/p>\n

Makefile\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6784\u5efa\u5de5\u5177\uff0c\u5b83\u80fd\u591f\u81ea\u52a8\u5316\u6267\u884c\u4e00\u7cfb\u5217\u6784\u5efa\u4efb\u52a1\uff0c\u4f8b\u5982\u7f16\u8bd1\u3001\u94fe\u63a5\u3001\u6253\u5305\u7b49\u3002\u5728GoBuildLinux\u4e2d\uff0cMakefile\u88ab\u7528\u4f5c\u6784\u5efa\u811a\u672c\uff0c\u7528\u4e8e\u81ea\u52a8\u5316\u6267\u884cGo\u8bed\u8a00\u5e94\u7528\u7a0b\u5e8f\u7684\u6784\u5efa\u4efb\u52a1\u3002<\/p>\n

\u5728Makefile\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u7cfb\u5217\u6784\u5efa\u4efb\u52a1\u53ca\u5176\u4f9d\u8d56\u5173\u7cfb\uff0c\u4f8b\u5982\uff1a<\/p>\n

“`<\/p>\n

build: deps<\/p>\n

go build -o app main.go<\/p>\n

deps:<\/p>\n

go mod download<\/p>\n

“`<\/p>\n

\u4e0a\u8ff0\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3abuild\u7684\u6784\u5efa\u4efb\u52a1\uff0c\u5b83\u4f9d\u8d56\u4e8e\u53e6\u4e00\u4e2a\u540d\u4e3adeps\u7684\u4efb\u52a1\u3002\u5728\u6267\u884cbuild\u4efb\u52a1\u4e4b\u524d\uff0cMakefile\u4f1a\u5148\u6267\u884cdeps\u4efb\u52a1\uff0c\u4e0b\u8f7dGo\u8bed\u8a00\u5e94\u7528\u7a0b\u5e8f\u6240\u9700\u7684\u4f9d\u8d56\u5305\u3002\u7136\u540e\uff0cMakefile\u4f1a\u6267\u884cgo build\u547d\u4ee4\uff0c\u5c06main.go\u6587\u4ef6\u7f16\u8bd1\u6210\u53ef\u6267\u884c\u6587\u4ef6app\u3002<\/p>\n

2. Git<\/p>\n

Git\u662f\u4e00\u79cd\u5206\u5e03\u5f0f\u7248\u672c\u63a7\u5236\u7cfb\u7edf\uff0c\u5b83\u80fd\u591f\u8bb0\u5f55\u4ee3\u7801\u7684\u4fee\u6539\u5386\u53f2\uff0c\u5e76\u534f\u4f5c\u7ba1\u7406\u591a\u4e2a\u5f00\u53d1\u8005\u4e4b\u95f4\u7684\u4ee3\u7801\u3002\u5728GoBuildLinux\u4e2d\uff0cGit\u88ab\u7528\u4f5c\u4ee3\u7801\u7ba1\u7406\u5de5\u5177\uff0c\u7528\u4e8e\u7ba1\u7406Go\u8bed\u8a00\u5e94\u7528\u7a0b\u5e8f\u7684\u6e90\u4ee3\u7801\u3002<\/p>\n

\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Git\u5c06Go\u8bed\u8a00\u5e94\u7528\u7a0b\u5e8f\u7684\u6e90\u4ee3\u7801\u4e0a\u4f20\u5230\u8fdc\u7a0b\u4ee3\u7801\u4ed3\u5e93\uff0c\u6216\u8005\u4ece\u8fdc\u7a0b\u4ee3\u7801\u4ed3\u5e93\u4e2d\u62c9\u53d6\u6700\u65b0\u7684\u4ee3\u7801\u3002\u8fd9\u6837\uff0c\u591a\u4e2a\u5f00\u53d1\u8005\u5c31\u53ef\u4ee5\u534f\u4f5c\u5f00\u53d1\u540c\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\uff0c\u800c\u4e0d\u4f1a\u51fa\u73b0\u4ee3\u7801\u51b2\u7a81\u7b49\u95ee\u9898\u3002<\/p>\n

3. Docker<\/p>\n

Docker\u662f\u4e00\u79cd\u5bb9\u5668\u5316\u6280\u672f\uff0c\u5b83\u80fd\u591f\u5c06\u5e94\u7528\u7a0b\u5e8f\u53ca\u5176\u4f9d\u8d56\u6253\u5305\u6210\u4e00\u4e2a\u72ec\u7acb\u7684\u5bb9\u5668\uff0c\u5e76\u5728\u4e0d\u540c\u7684\u73af\u5883\u4e2d\u8fd0\u884c\u3002\u5728GoBuildLinux\u4e2d\uff0cDocker\u88ab\u7528\u4f5c\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u5de5\u5177\uff0c\u7528\u4e8e\u5c06Go\u8bed\u8a00\u5e94\u7528\u7a0b\u5e8f\u53ca\u5176\u4f9d\u8d56\u6253\u5305\u6210\u4e00\u4e2aDocker\u5bb9\u5668\u3002<\/p>\n

\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Dockerfile\u5b9a\u4e49Docker\u5bb9\u5668\u7684\u6784\u5efa\u8fc7\u7a0b\uff0c\u4f8b\u5982\uff1a<\/p>\n

“`<\/p>\n

php\u9879\u76ee\u6253\u5305\u6210exe<\/a>FROM golang:1.16-alpine<\/p>\n

WORKDIR \/<\/p>\n

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

app<\/p>\n

COPY . .<\/p>\n

RUN go build -o app main.go<\/p>\n

CMD [“.\/app”]<\/p>\n

“`<\/p>\n

\u4e0a\u8ff0Dockerfile\u5b9a\u4e49\u4e86\u4e00\u4e2a\u57fa\u4e8eAlpine Linux\u7684Docker\u5bb9\u5668\uff0c\u5b83\u4f1a\u5c06Go\u8bed\u8a00\u5e94\u7528\u7a0b\u5e8f\u7684\u6e90\u4ee3\u7801\u590d\u5236\u5230\u5bb9\u5668\u4e2d\uff0c\u7136\u540e\u6267\u884cgo build\u547d\u4ee4\u7f16\u8bd1\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u901a\u8fc7CMD\u6307\u4ee4\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n

\u4f7f\u7528Docker\u53ef\u4ee5\u5c06\u5e94\u7528\u7a0b\u5e8f\u53ca\u5176\u4f9d\u8d56\u6253\u5305\u6210\u4e00\u4e2a\u72ec\u7acb\u7684\u5bb9\u5668\uff0c\u65b9\u4fbf\u5728\u4e0d\u540c\u7684\u73af\u5883\u4e2d\u90e8\u7f72\u548c\u8fd0\u884c\u3002<\/p>\n

\u603b\u7ed3<\/p>\n

GoBuildLinux\u662f\u4e00\u6b3e\u7528\u4e8e\u5728L\u628a\u8f6f\u4ef6\u6253\u5305\u6210\u5de5\u5177\u7bb1<\/a>inux\u7cfb\u7edf\u4e2d\u6784\u5efaGo\u8bed\u8a00\u5e94\u7528\u7a0b\u5e8f\u7684\u5de5\u5177\uff0c\u5b83\u57fa\u4e8eMakefile\u3001Git\u3001Docker\u7b49\u5e38\u7528\u5de5\u5177\u548c\u6280\u672f\uff0c\u80fd\u591f\u81ea\u52a8\u5316\u6784\u5efa\u3001\u6253\u5305\u548c\u53d1\u5e03Go\u8bed\u8a00\u5e94\u7528\u7a0b\u5e8f\uff0c\u7b80\u5316\u4e86\u5f00\u53d1\u4eba\u5458\u7684\u5de5\u4f5c\u6d41\u7a0b\uff0c\u63d0\u9ad8\u4e86\u5f00\u53d1\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

GoBuildLinux\u662f\u4e00\u6b3e\u7528\u4e8e\u5728Linux\u7cfb\u7edf\u4e2d\u6784\u5efaGo\u8bed\u8a00\u5e94\u7528\u7a0b\u5e8f\u7684\u5de5\u5177\u3002\u5b83\u80fd\u591f\u81ea\u52a8\u5316\u6784\u5efa\u3001\u6253\u5305\u548c\u53d1\u5e03Go\u8bed\u8a00\u5e94\u7528\u7a0b\u5e8f\uff0c\u7b80\u5316\u4e86\u5f00\u53d1\u4eba\u5458\u7684\u5de5\u4f5c\u6d41\u7a0b\uff0c\u63d0\u9ad8\u4e86\u5f00\u53d1\u6548\u7387\u3002GoBuildLinux\u7684\u539f\u7406<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[3028,3029,12,593,3027],"topic":[],"class_list":["post-1343","post","type-post","status-publish","format-standard","hentry","category-exekf","tag-exe","tag-3029","tag-12","tag-593"],"_links":{"self":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/1343","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/comments?post=1343"}],"version-history":[{"count":0,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/1343\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/media?parent=1343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/categories?post=1343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/tags?post=1343"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/topic?post=1343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}