GOPATH没有正确设置

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

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

huangapple
  • 本文由 发表于 2016年2月15日 10:05:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/35400455.html
匿名

发表评论

匿名网友

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

确定