WSL缺少Golang time.Time的方法。

huangapple go评论72阅读模式
英文:

WSL missing method for Golang time.Time

问题

我一直在使用Go编写一个命令行工具,但遇到了这个问题。当我在Windows命令提示符或PowerShell中运行工具时,它可以编译和正常工作,但是当我尝试在WSL(Ubuntu版本1)中运行时,出现了以下错误信息:

time.Now().UnixMilli undefined (type time.Time has no field or method UnixMilli)

使用time.Now().UnixMilli()的文件已经包含了time包import(..."time"...,并且没有在其他地方重新定义,因为代码可以在Windows终端中正确编译和运行。因此,我认为这与Linux for Windows有关。

我尝试更新$GOROOT$GOPATH到不同的位置,包括尝试连接到安装在挂载的Windows系统/mnt/c/上的go(这会导致不知道如何编译的不同错误,因为它是Windows版本的go)。我还尝试使用apt-gettar卸载和重新安装了Ubuntu上的各个版本的Go。

我不确定为什么它不能正常工作,希望其他人能提供一些见解。

谢谢!

go env (windows-cmd):

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\<user>\AppData\Local\go-build
set GOENV=C:\Users\<user>\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\<user>\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\<user>\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.17.3
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\<user>\Documents\Firmware\Git\<project>\cli\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\<user>\AppData\Local\Temp\go-build2245089512=/tmp/go-build -gno-record-gcc-switches

go env (wsl-ubuntu):

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/<user>/.cache/go-build"
GOENV="/home/<user>/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/<user>/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go-1.13"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.13/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/mnt/c/Users/<user>/Documents/Firmware/Git/<project>/cli/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build414455523=/tmp/go-build -gno-record-gcc-switches"
英文:

I've been working on a cli tool in Go when I ran into this issue. The tool compiles and works perfectly fine when I use the Windows Command Prompt or PowerShell to run it but when I try using WSL (Ubuntu version 1) I get this error message.

time.Now().UnixMilli undefined (type time.Time has no field or method UnixMilli)

The file using time.Now().UnixMilli() has the package time included import(...&quot;time&quot;...) and it is not redefined elsewhere because the code compiles and runs correctly in the Windows terminals. Because of this I assume that it has something to do with Linux for Windows.

I have tried updating the $GOROOT and $GOPATH to various different places including trying to connect to go installed on the mounted Windows system /mnt/c/ (this gives a different error of not knowing how compile which makes sense since it's the Windows version of go). I have also tried uninstalling and reinstalling various versions of Go for Ubuntu using both apt-get and tar.

I'm not sure why it isn't working correctly and would love any insight that anyone else might have.

Thanks!

go env (windows-cmd):

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\&lt;user&gt;\AppData\Local\go-build
set GOENV=C:\Users\&lt;user&gt;\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\&lt;user&gt;\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\&lt;user&gt;\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.17.3
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\&lt;user&gt;\Documents\Firmware\Git\&lt;project&gt;\cli\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\&lt;user&gt;\AppData\Local\Temp\go-build2245089512=/tmp/go-build -gno-record-gcc-switches

go env (wsl-ubuntu):

GOARCH=&quot;amd64&quot;
GOBIN=&quot;&quot;
GOCACHE=&quot;/home/&lt;user&gt;/.cache/go-build&quot;
GOENV=&quot;/home/&lt;user&gt;/.config/go/env&quot;
GOEXE=&quot;&quot;
GOFLAGS=&quot;&quot;
GOHOSTARCH=&quot;amd64&quot;
GOHOSTOS=&quot;linux&quot;
GONOPROXY=&quot;&quot;
GONOSUMDB=&quot;&quot;
GOOS=&quot;linux&quot;
GOPATH=&quot;/home/&lt;user&gt;/go&quot;
GOPRIVATE=&quot;&quot;
GOPROXY=&quot;https://proxy.golang.org,direct&quot;
GOROOT=&quot;/usr/lib/go-1.13&quot;
GOSUMDB=&quot;sum.golang.org&quot;
GOTMPDIR=&quot;&quot;
GOTOOLDIR=&quot;/usr/lib/go-1.13/pkg/tool/linux_amd64&quot;
GCCGO=&quot;gccgo&quot;
AR=&quot;ar&quot;
CC=&quot;gcc&quot;
CXX=&quot;g++&quot;
CGO_ENABLED=&quot;1&quot;
GOMOD=&quot;/mnt/c/Users/&lt;user&gt;/Documents/Firmware/Git/&lt;project&gt;/cli/go.mod&quot;
CGO_CFLAGS=&quot;-g -O2&quot;
CGO_CPPFLAGS=&quot;&quot;
CGO_CXXFLAGS=&quot;-g -O2&quot;
CGO_FFLAGS=&quot;-g -O2&quot;
CGO_LDFLAGS=&quot;-g -O2&quot;
PKG_CONFIG=&quot;pkg-config&quot;
GOGCCFLAGS=&quot;-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build414455523=/tmp/go-build -gno-record-gcc-switches&quot;

答案1

得分: 4

你在WSL中运行的Go版本是1.13,但UnixMilli函数是在Go版本1.17中最近引入的。

如果你想安装多个Go版本,请参考:https://go.dev/doc/manage-install

否则,请参考:https://go.dev/doc/install

英文:

The version of go you are running in WSL is 1.13, but UnixMilli was only introduced recently in go version 1.17.

If you wish to install multiple versions of go see: https://go.dev/doc/manage-install

Else: https://go.dev/doc/install

huangapple
  • 本文由 发表于 2021年12月7日 06:42:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/70252861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定