英文:
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-get
和tar
卸载和重新安装了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(..."time"...)
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\<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"
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论