[Golang] Set GOOS=windows is invalid when packaged to different platforms, windows and linux.

When writing small tools in Go, packaging and compiling them into exe executable files for Linux or Windows, it is not smooth. I have encountered this a few times before, but I always forgot to record it.

set CGO_ENABLED=0
set GOOS=windows
set GOARCH=amd64
go build ./合并小文件.go

The executable file with .exe ending in Windows environment is not generated correctly.
Insert image description here

Check go env and find that set GOOS=windows was not set successfully.

E:\PROGRAMMING\GolangStudy\my_utils\1.大文本处理>go env
set GO111MODULE=on
set GOARCH=amd64
# 忽略一些参数......
set GOOS=linux 	# 此处还是linux
set GOPATH=D:\PROGRAMMING\Go;
# 忽略一些参数......

Reason: Currently it is executed in Windows PowerShell, and the set command does not work
Insert image description here
. Solution:

# 在原窗口执行
go env -w GOOS=windows 
# 或新建一个cmd终端窗囗执行
set GOOS=windows

Insert image description here
verify

E:\PROGRAMMING\GolangStudy\> go env
set GO111MODULE=on
set GOARCH=amd64
# 忽略一些参数......
set GOOS=windows
set GOPATH=D:\PROGRAMMING\Go;
# 忽略一些参数......

Re-execute go build packaging and successfully output the exe file
Insert image description here

Guess you like

Origin blog.csdn.net/qq_45277554/article/details/133246629