英文:
GOPATH not set correctly
问题
我在Xubuntu上安装了Go。在那之后,我修改了GOPATH:
$ export GOPATH=$HOME/go
$ echo $GOPATH
$ /home/rangga/go
如果我运行以下命令:
$ go run /home/rangga/go/src/Test/testpath.go
$ /home/rangga
我使用os.Getwd()
来测试当前路径,应该输出/home/rangga/go/src/Test
。
以下是我的go环境信息:
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/rangga/go"
GORACE=""
GOROOT="/usr"
GOTOOLDIR="/usr/lib/gccgo/tool"
TERM="dumb"
CC="gcc"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread"
CXX="g++"
CGO_ENABLED="1"
我在导出GOPATH时是否出错了?请问如何使GOPATH在Go语言中可用?
谢谢
编辑
好的,这是我的错误,我应该在运行之前切换到当前工作目录。
感谢peterSO的帮助。
英文:
i was installed go on xubuntu. after that iI change the GOPATH on
$ export GOPATH=$HOME/go
$ echo $GOPATH
$ /home/rangga/go
if I do go run
$ go run /home/rangga/go/src/Test/testpath.go
$ /home/rangga
i used os.Getwd()
for test the current path
it should be the output like these /home/rangga/go/src/Test
FYI, here's my go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/rangga/go"
GORACE=""
GOROOT="/usr"
GOTOOLDIR="/usr/lib/gccgo/tool"
TERM="dumb"
CC="gcc"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread"
CXX="g++"
CGO_ENABLED="1"
do i made wrong in export GOPATH?, please how to make the GOPATH is workable on go language?
thank you
EDIT
ok, this is my mistake i should change to the current work directory after then do go run
thanks to peterSO
答案1
得分: 2
> 包 os
>
> func Getwd
>
> func Getwd() (dir string, err error)
>
> Getwd 返回当前目录对应的根路径名。如果当前目录可以通过多个路径到达(由于符号链接),Getwd 可能返回其中任意一个。
$ go run /home/rangga/go/src/Test/testpath.go
$ /home/rangga
你得到了你所要求的,你的当前目录是:/home/rangga
。
更改你的当前目录,
$ cd /home/rangga/go/src/Test/
$ go run testpath.go
$ /home/rangga/go/src/Test
英文:
> Package os
>
> func Getwd
>
> func Getwd() (dir string, err error)
>
> Getwd returns a rooted path name corresponding to the current
> directory. If the current directory can be reached via multiple paths
> (due to symbolic links), Getwd may return any one of them.
$ go run /home/rangga/go/src/Test/testpath.go
$ /home/rangga
You got what you asked for, your current directory: /home/rangga
.
Change your current directory,
$ cd /home/rangga/go/src/Test/
$ go run testpath.go
$ /home/rangga/go/src/Test
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论