Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do all Go projects need to live below GOPATH? [duplicate]

Tags:

go

project

I am starting with Go and trying to get my head around GOPATH (and probably GOBIN).

When trying to fetch external libraries via go get I get the error

go get: no install location for directory D:\Seafile\dev-perso\domotiqueNG\services\dispatcher-go\src\dispatcher-go outside GOPATH

This error is apparently solved by having a project structure below $GOPATH/src.

Does this mean that all my Go programs must live there? If GOPATH is d:\hello then the projects bonjour and aurvoir really need to be in

d:\hello\src\bonjour
d:\hello\src\aurevoir

only ?

In this is the case how can I

  • split, say, personal and professional projects when the personal must stay at d:\home and professional at x:\work ?
  • have multilanguage projects where d:\home\domotique\dispatch is in Go, d:\home\domotique\whatever is in Python, and I have several such combos in d:\home?
like image 920
WoJ Avatar asked May 25 '18 15:05

WoJ


People also ask

Do all Go projects have to be in Gopath?

You can actually have Go projects outside of GOPATH. However, some tools do not work well or do not work at all in this kind of situation. For example, goimports , which formats your code and add missing imports, will not be able to find packages that are outside of GOPATH.

Is Gopath still needed?

Since 1.12 version Go modules is enabled by default and the GOPATH will be deprecated in 1.13 version. For those who are getting started with Go 1.12, the installation and set up goes will be as follows.

Can I have multiple Gopath?

Yes. The Go path is used to resolve import statements. It is implemented by and documented in the go/build package. The GOPATH environment variable lists places to look for Go code.

What is the point of Gopath?

GOPATH is a variable that defines the root of your workspace. By default, the workspace directory is a directory that is named go within your user home directory (~/go for Linux and MacOS, %USERPROFILE%/go for Windows). GOPATH stores your code base and all the files that are necessary for your development.


Video Answer


2 Answers

You can actually have Go projects outside of GOPATH. However, some tools do not work well or do not work at all in this kind of situation. For example, goimports, which formats your code and add missing imports, will not be able to find packages that are outside of GOPATH. You'll have to write the imports manually using a relative path: ./path/to/your/package.

how can I split, say, personal and professional projects when the personal must stay at d:\home and professional at x:\work ?

You actually can have multiple Go workspaces (https://github.com/golang/go/wiki/GOPATH). You just need to set GOPATH to the list of their location joined with the list separator of your OS. e.g. on Linux it would look like this:

GOPATH="/home/nobody/perso:/home/nobody/work"

Though, I'm not sure how go and other tools such as dependency managers handle multiple workspaces.

like image 154
fische Avatar answered Oct 17 '22 15:10

fische


When using the GOPATH you can make subdirectories in the /src folder, for example, adding both home and work directories. In fact, the Go project has an example of how to organize code. While packages need to be in their own folders, a folder itself in the GOPATH is not automatically a package.

If you'd rather not work within the confines of the GOPATH, you can change it to the path you do want to work in, or set it to your home directory.

like image 20
thisischuck Avatar answered Oct 17 '22 15:10

thisischuck