{"id":5049,"date":"2023-09-01T12:18:54","date_gmt":"2023-09-01T04:18:54","guid":{"rendered":"https:\/\/www.zhidianwl.net\/zhidianwl\/?p=5049"},"modified":"2023-09-01T12:18:54","modified_gmt":"2023-09-01T04:18:54","slug":"go%e6%89%93%e5%8c%85exe%e6%98%af%e4%bb%80%e4%b9%88%e6%84%8f%e6%80%9d%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.net\/zhidianwl\/2023\/09\/01\/go%e6%89%93%e5%8c%85exe%e6%98%af%e4%bb%80%e4%b9%88%e6%84%8f%e6%80%9d%ef%bc%9f\/","title":{"rendered":"go\u6253\u5305exe\u662f\u4ec0\u4e48\u610f\u601d\uff1f"},"content":{"rendered":"

\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Go\uff08\u53c8\u540dGolang\uff09\u7f16\u7a0b\u8bed\u8a00\u6253\u5305\u60a8\u7684\u7a0b\u5e8f\u4e3a\u4e00\u4e2a\u72ec\u7acb\u7684.exe\u6587\u4ef6\u3002\u8fd9\u4f7f\u5f97\u5728Windows\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884c\u60a8\u7684\u7a0b\u5e8f\u65e0\u9700\u5b89\u88c5\u4efb\u4f55\u989d\u5916\u7684\u4f9d\u8d56\u5e93\u6216\u8f6f\u4ef6\u3002<\/p>\n

Go\u652f\u6301\u4ea4\u53c9\u7f16\u8bd1, \u8fd9\u610f\u5473\u7740\u60a8\u53ef\u4ee5\u5728\u4e00\u4e2a\u5e73\u53f0(\u4f8b\u5982Linux\u6216Mac)\u4e0a\u6784\u5efa\u8fd0\u884c\u5728\u53e6\u4e00\u4e2a\u5e73\u53f0(\u4f8b\u5982Windows)\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\u3002 \u6253\u5305Go\u7a0b\u5e8f\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684.exe\u6587\u4ef6\u53ef\u4ee5\u8ba9\u4f60\u8f7b\u677e\u5206\u53d1\u4f60\u7684\u7a0b\u5e8f\uff0c\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n

## \u539f\u7406<\/p>\n

Go\u7f16\u8bd1\u5668\u901a\u8fc7\u5c06\u6e90\u4ee3\u7801\u7f16\u8bd1\u6210\u672c\u5730\u673a\u5668\u4ee3\u7801\u6765\u751f\u6210\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u3002\u8fd9\u4f7f\u5f97Go\u7a0b\u5e8f\u5177\u6709\u826f\u597d\u7684\u6027\u80fd\uff0c\u5e76\u51cf\u5c11\u4e86\u8fd0\u884c\u65f6\u7684\u4f9d\u8d56\u3002Go\u7f16\u8bd1\u5668\u4f1a\u5c06\u7a0b\u5e8f\u4e2d\u9700\u8981\u7684\u6240\u6709\u4f9d\u8d56\u6253\u5305\u5230\u751f\u6210\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e2d\uff0c\u56e0\u6b64\u60a8\u65e0\u9700\u62c5\u5fc3\u5b89\u88c5\u989d\u5916\u7684\u4f9d\u8d56\u5e93\u3002\u7f16\u8bd1\u65f6\u9759\u6001\u94fe\u63a5\u5e93\u786e\u4fdd\u7528\u6237\u8ba1\u7b97\u673a\u4e0a\u4e0d\u9700\u8981\u5b89\u88c5\u989d\u5916\u7684\u52a8\u6001\u94fe\u63a5\u5e93\uff08\u5982DLLs\uff09\u3002<\/p>\n

## \u8be6\u7ec6\u6b65\u9aa4<\/p>\n

1. \u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5Go\u7f16\u7a0b\u8bed\u8a00\u3002\u5982\u679c\u60a8\u5c1a\u672a\u5b89\u88c5\uff0c\u8bf7\u8bbf\u95ee\u5b98\u65b9\u6587\u6863\u4ee5\u83b7\u53d6\u6709\u5173\u5b89\u88c5\u8bf4\u660e\u7684\u8be6\u7ec6\u4fe1\u606f\uff1ahttps:\/\/golang.org\/doc\/install<\/p>\n

2. \u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684Go\u7a0b\u5e8f\u4f5c\u4e3a\u793a\u4f8b\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a`main.go`\u7684\u6587\u4ef6\uff0c\u8be5\u6587\u4ef6\u5305\u542b\u4e00\u4e2awindows\u6279\u91cfexe\u6587\u4ef6<\/a>\u7b80\u77ed\u7684\u6253\u5370\u6d88\u606f\u7684\u7a0b\u5e8f\u3002<\/p>\n

“`go<\/p>\n

package main<\/p>\n

import “fmt”<\/p>\n

func main() {<\/p>\n

fmt.Println(“Hello, World!”)<\/p>\n

}<\/p>\n

“`<\/p>\n

3. \u4f7f\u7528`go build`\u547d\u4ee4\u6784\u5efa\u6b64\u7a0b\u5e8f\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u6784\u5efa\u4e00\u4e2a\u9488\u5bf9Windows\u5e73\u53f0\u7684.exe\u6587\u4ef6<\/p>\n

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

\u3002<\/p>\n

\u5728Mac\u6216Linux\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n

“`sh<\/p>\n

GOOS=windows GOARCH=amd64 go build -o myapp.exe main.go<\/p>\n

“`<\/p>\n

\u5728Windows\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff08\u5728Cmd\u6216PowerShell\u4e2d\u6267\u884c\uff09\uff1a<\/p>\n

“`sh<\/p>\n

set GOOS=windows<\/p>\n

set GOARCH=amd64<\/p>\n

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

“`<\/p>\n

\u8fd9\u5c06\u751f\u6210\u4e00\u4e2a\u540d\u4e3a`myapp.exe`\u7684Windows\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n

– `GOOS=windows` – \u6307\u5b9a\u76ee\u6807\u64cd\u4f5c\u7cfb\u7edf\u4e3aWindows\u3002<\/p>\n

– `GOARCH=amd64` – \u6307\u5b9a\u76ee\u6807\u5904\u7406\u5668\u67b6\u6784\u4e3a amd64\u3002\u5982\u679c\u9700\u898132\u4f4d\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528`GOARCH=386`\u9009\u9879\u3002<\/p>\n

– `-o myapp.exe` – \u544a\u8bc9go\u7f16\u8bd1\u5668\u5c06\u8f93\u51fa\u7684\u53ef\u6267\u884c\u6587\u4ef6\u547d\u540d\u4e3a`myapp.exe`\u3002<\/p>\n

4. \u6700\u540evue\u5f00\u53d1exe<\/a>\uff0c\u5c06`myapp.exe`\u6587\u4ef6\u5206\u53d1\u7ed9\u60a8\u7684\u7528\u6237\u3002\u4ed6\u4eec\u65e0\u9700\u5b89\u88c5\u4efb\u4f55\u989d\u5916\u7684\u5e93\u6216\u8f6f\u4ef6\uff0c\u53ea\u9700\u53cc\u51fb.exe\u6587\u4ef6\u5c31\u53ef\u4ee5\u8fd0\u884c\u60a8\u7684Go\u7a0b\u5e8f\u3002<\/p>\n

\u8fd9\u5c31\u662f\u4f7f\u7528Go\u8bed\u8a00\u6253\u5305EXE\u7684\u8be6\u7ec6\u4ecb\u7ecd\u3002\u603b\u4e4b\uff0cGo\u7684\u4ea4\u53c9\u7f16\u8bd1\u529f\u80fd\u548c\u5bf9\u5355\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u7684\u652f\u6301\u4f7f\u5f97\u5728\u4e0d\u540c\u5e73\u53f0\u4e0a\u5206\u53d1\u548c\u90e8\u7f72Go\u7a0b\u5e8f\u53d8\u5f97\u76f8\u5f53\u7b80\u5355\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Go\uff08\u53c8\u540dGolang\uff09\u7f16\u7a0b\u8bed\u8a00\u6253\u5305\u60a8\u7684\u7a0b\u5e8f\u4e3a\u4e00\u4e2a\u72ec\u7acb\u7684.exe\u6587\u4ef6\u3002\u8fd9\u4f7f\u5f97\u5728Windows\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884c\u60a8\u7684\u7a0b\u5e8f\u65e0\u9700\u5b89\u88c5\u4efb\u4f55\u989d\u5916\u7684\u4f9d\u8d56\u5e93\u6216\u8f6f\u4ef6\u3002Go\u652f\u6301\u4ea4\u53c9\u7f16\u8bd1, \u8fd9<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[9500,241,12,9501,34],"topic":[],"class_list":["post-5049","post","type-post","status-publish","format-standard","hentry","category-zhuomianruanjian","tag-guideexe","tag-241","tag-12","tag-exe","tag-34"],"_links":{"self":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/5049","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/comments?post=5049"}],"version-history":[{"count":0,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/5049\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/media?parent=5049"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/categories?post=5049"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/tags?post=5049"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/topic?post=5049"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}