问题描述
需要编译在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": [], } ] }