如何确认 $GOPATH 和 $GOROOT 是否正确设置?

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

How to confirm that $GOPATH and $GOROOT are properly set?

问题

这不是重复的问题:

https://stackoverflow.com/questions/7970390/what-should-be-the-values-of-gopath-and-goroot

我不想知道这些值应该是什么。我想知道当我在控制台中输入ls $GOROOTls $GOPATH时应该看到什么。我相当确定我在大约一年前按照教程设置时弄错了一些东西,我想通过简单检查它们指向的内容是否正确来确认这两个变量是否指向了正确的位置。

这是我现在的情况。看起来$GOROOT指向了一个不存在的位置。我相当确定它应该指向usr/local/go,但如果我知道ls $GOROOT的预期结果是什么,确认起来会容易得多。

至于$GOPATH,我不太确定我的“工作空间”是指我的所有Go代码,还是只是GitHub的东西,或者可能是我正在工作的特定文件夹。我知道它应该指向我的“工作空间”,但我不知道我要找的那个工作空间是什么样子的。

  1. Sephs-MBP:ThumbzArt seph$ $GOROOT
  2. Sephs-MBP:ThumbzArt seph$ $GOPATH
  3. -bash: /Users/seph/code/golang: 是一个目录
  4. Sephs-MBP:ThumbzArt seph$ ls $GOROOT
  5. Bman.jpg README.md ThumbzArt.sublime-workspacescripts thumbzart.go
  6. LICENSE.md ThumbzArt.sublime-project public templates ticktock.go
  7. Sephs-MBP:ThumbzArt seph$ $GOPATH
  8. -bash: /Users/seph/code/golang: 是一个目录
  9. Sephs-MBP:ThumbzArt seph$ ls $GOPATH
  10. - bin p pkg src
  11. Sephs-MBP:ThumbzArt seph$ ls /usr/local/go
  12. AUTHORS CONTRIBUTORS PATENTS VERSION bin doc lib pkg src
  13. CONTRIBUTING.md LICENSE README.md api blog favicon.ico misc robots.txt test
  14. Sephs-MBP:ThumbzArt seph$

我知道这个问题看起来很荒谬,但是对于你没有预期结果的事情很难确认。

谢谢

  1. Sephs-MBP:streak seph$ go env
  2. GOARCH="amd64"
  3. GOBIN=""
  4. GOEXE=""
  5. GOHOSTARCH="amd64"
  6. GOHOSTOS="darwin"
  7. GOOS="darwin"
  8. GOPATH="/Users/seph/code/golang"
  9. GORACE=""
  10. GOROOT="/usr/local/go"
  11. GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
  12. GO15VENDOREXPERIMENT=""
  13. CC="clang"
  14. GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
  15. CXX="clang++"
  16. CGO_ENABLED="1"
  17. Sephs-MBP:streak seph$ go version
  18. go version go1.5.2 darwin/amd64
  19. Sephs-MBP:streak seph$ which go
  20. /usr/local/go/bin/go
英文:

This is not a repeat of this question:

https://stackoverflow.com/questions/7970390/what-should-be-the-values-of-gopath-and-goroot

I don't want to know what the values should be. I want to know what I should see when I type ls $GOROOT or ls $GOPATH into console. I'm pretty sure I set things up wrong following a tutorial almost a year ago, and I want to be able to confirm that these two are pointing to where they should be by simply checking that what they point to looks right.

Here's where I am right now. It looks like $GOROOT is pointing nowhere. I'm pretty sure it should be pointing at usr/local/go, but it would be a lot easier to confirm if I knew what the expected result of ls $GOROOT is supposed to be.

As for $GOPATH I'm not totally sure if my "workspace" is where all my go code is, or maybe just the github stuff, or maybe the particular folder I'm working within. I know it's supposed to point to my "work space," but I don't know what that work space I'm looking for looks like.

  1. Sephs-MBP:ThumbzArt seph$ $GOROOT
  2. Sephs-MBP:ThumbzArt seph$ $GOPATH
  3. -bash: /Users/seph/code/golang: is a directory
  4. Sephs-MBP:ThumbzArt seph$ ls $GOROOT
  5. Bman.jpg README.md ThumbzArt.sublime-workspacescripts thumbzart.go
  6. LICENSE.md ThumbzArt.sublime-project public templates ticktock.go
  7. Sephs-MBP:ThumbzArt seph$ $GOPATH
  8. -bash: /Users/seph/code/golang: is a directory
  9. Sephs-MBP:ThumbzArt seph$ ls $GOPATH
  10. - bin p pkg src
  11. Sephs-MBP:ThumbzArt seph$ ls /usr/local/go
  12. AUTHORS CONTRIBUTORS PATENTS VERSION bin doc lib pkg src
  13. CONTRIBUTING.md LICENSE README.md api blog favicon.ico misc robots.txt test
  14. Sephs-MBP:ThumbzArt seph$

I know this question seems ridiculous, but it's hard to confirm things for which you have no expected results.

Thank you

  1. Sephs-MBP:streak seph$ go env
  2. GOARCH="amd64"
  3. GOBIN=""
  4. GOEXE=""
  5. GOHOSTARCH="amd64"
  6. GOHOSTOS="darwin"
  7. GOOS="darwin"
  8. GOPATH="/Users/seph/code/golang"
  9. GORACE=""
  10. GOROOT="/usr/local/go"
  11. GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
  12. GO15VENDOREXPERIMENT=""
  13. CC="clang"
  14. GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
  15. CXX="clang++"
  16. CGO_ENABLED="1"
  17. Sephs-MBP:streak seph$ go version
  18. go version go1.5.2 darwin/amd64
  19. Sephs-MBP:streak seph$ which go
  20. /usr/local/go/bin/go

答案1

得分: -4

编辑:我刚刚偶然发现的另一个非常有用的功能是:go help gopath。这可能应该是某人的答案。

$GOPATH指向的文件夹应该像这样:

  1. Sephs-MBP:streak seph$ ls $GOPATH
  2. - bin p pkg src

另一方面,如果你使用ls $GOROOTls $GOPATH进行比较,$GOROOT将产生意外的结果。这是因为在这个上下文中没有设置$GOROOT,我想。

如果你使用go env,你会看到$GOROOT的真实情况

  1. Sephs-MBP:streak seph$ go env
  2. GOARCH="amd64"
  3. GOBIN=""
  4. GOEXE=""
  5. GOHOSTARCH="amd64"
  6. GOHOSTOS="darwin"
  7. GOOS="darwin"
  8. GOPATH="/Users/seph/code/golang"
  9. GORACE=""
  10. GOROOT="/usr/local/go" //这是它实际指向的位置
  11. GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
  12. GO15VENDOREXPERIMENT=""
  13. CC="clang"
  14. GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused- arguments -fmessage-length=0 -fno-common"
  15. CXX="clang++"
  16. CGO_ENABLED="1"

如果你从go env获取$GOROOT的结果并执行ls,你应该看到类似这样的内容:

  1. Sephs-MBP:streak seph$ ls /usr/local/go
  2. AUTHORS CONTRIBUTORS PATENTS VERSION bin doc lib pkg src
  3. CONTRIBUTING.md LICENSE README.md api blog favicon.ico misc robots.txt test

如果所有这些都符合要求,那么你的$GOPATH$GOROOT已经正确设置。

英文:

EDIT: Another very useful feature I just stumbled across is this: go help gopath. This should have probably been somebodies answer.

The folder $GOPATH points to should like this:

  1. Sephs-MBP:streak seph$ ls $GOPATH
  2. - bin p pkg src

$GOROOT, on the other hand, will yield unexpected results if you use ls $GOROOT as compared to ls $GOPATH. This is because $GOROOT is not set within this context, I think.

  1. Sephs-MBP:helloworld seph$ ls $GOROOT
  2. helloworld.go

If you use go env you'll see the true nature of $GOROOT

  1. Sephs-MBP:streak seph$ go env
  2. GOARCH="amd64"
  3. GOBIN=""
  4. GOEXE=""
  5. GOHOSTARCH="amd64"
  6. GOHOSTOS="darwin"
  7. GOOS="darwin"
  8. GOPATH="/Users/seph/code/golang"
  9. GORACE=""
  10. GOROOT="/usr/local/go" //this is where it actually points
  11. GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
  12. GO15VENDOREXPERIMENT=""
  13. CC="clang"
  14. GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused- arguments -fmessage-length=0 -fno-common"
  15. CXX="clang++"
  16. CGO_ENABLED="1"

And if you get the result of $GOROOT from go env and do ls you should see something like this:

  1. Sephs-MBP:streak seph$ ls /usr/local/go
  2. AUTHORS CONTRIBUTORS PATENTS VERSION bin doc lib pkg src
  3. CONTRIBUTING.md LICENSE README.md api blog favicon.ico misc robots.txt test

If all of these things check out, then your $GOPATH and $GOROOT are properly set.

huangapple
  • 本文由 发表于 2016年3月10日 23:53:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/35921255.html
匿名

发表评论

匿名网友

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

确定