问题描述
需要编译在windows下的可执行文件,当程序跑起来时时区错误
解决方法
- 确保go版本不可以过低;最好是1.15版本以上
- 执行命令GOOS=windows GOARCH=amd64 go build -v -tags timetzdata
- 或者设置环境变量ZONEINFO,指向$GOROOT/lib/time/zoneinfo.zip;也可以代码设置环境变量
如果使用的是vscode编辑器;可以在launch.json中来指定临时环境变量:err := syscall.Setenv("ZONEINFO", `C:\Go\lib\time\zoneinfo.zip`) if err != nil { log.Fatal(err) } command := exec.Command("xxx.exe") output, _ := command.Output() fmt.Println(string(output)){ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "LaunchGo", "type": "go", "request": "launch", "mode": "auto", "program": "${workspaceFolder}", "env": { //临时变量 "GOROOT": "c:\\Program Files\\Go", "GOPATH": "C:\\Users\\LM-LL\\go", }, "args": [], } ] }

该博客主要介绍了在Windows环境下,针对Go语言编译的可执行文件出现时区错误的问题及其解决方法。建议使用Go 1.15及以上版本,并通过设置环境变量`ZONEINFO`指向`$GOROOT/lib/time/zoneinfo.zip`来修复。在VSCode中,可在`launch.json`配置文件中设置临时环境变量以避免时区问题。

被折叠的 条评论
为什么被折叠?



