GO 编码windows二进制文件,执行时区报错问题解决

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述

需要编译在windows下的可执行文件,当程序跑起来时时区错误

解决方法

  • 确保go版本不可以过低;最好是1.15版本以上
  • 执行命令GOOS=windows GOARCH=amd64 go build -v -tags timetzdata
  • 或者设置环境变量ZONEINFO,指向$GOROOT/lib/time/zoneinfo.zip;也可以代码设置环境变量
    	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))
    
    如果使用的是vscode编辑器;可以在launch.json中来指定临时环境变量:
    {
        // 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": [],
            }
        ]
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值