安裝rsrc
go get github.com/akavel/rsrc
創建manifest文件
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
生成syso文件
rsrc -manifest rsrc.manifest -ico icon.ico -o rsrc.syso
go build -ldflags "-s -w" -o myapp.exe // 以目錄方式進行編譯
兩個程序都成功了,但有一個程序編譯時報錯,好像說架構不同
... i386 architecture of input file 'rsrc.syso' is incompatible with i386:x86-64 out
網上找到的一些方案是編譯時加標籤:
-ldflags "-linkmode internal"
但在我這裏無效,沒有解決
然後看到 rsrc 的 help … 生成 syso 時架構設置成 amd64,再編譯就沒問題了
USAGE:
C:\Users\ANDY\go\bin\rsrc.exe [-manifest FILE.exe.manifest] [-ico FILE.ico[,FILE2.ico...]] -o FILE.syso
Generates a .syso file with specified resources embedded in .rsrc section,
aimed for consumption by Go linker when building Win32 excecutables.
The generated *.syso files should get automatically recognized by 'go build'
command and linked into an executable/library, as long as there are any *.go
files in the same directory.
OPTIONS:
-arch string
architecture of output file - one of: 386, amd64, [EXPERIMENTAL: arm, arm64] (default "386")
-data string
path to raw data file to embed [WARNING: useless for Go 1.4+]
-ico string
comma-separated list of paths to .ico files to embed
-manifest string
path to a Windows manifest file to embed
-o string
name of output COFF (.res or .syso) file (default "rsrc.syso")
rsrc -arch amd64 -manifest rsrc.manifest -ico icon.ico -o rsrc.syso
go build -ldflags "-s -w" -o myapp.exe