When should you set the $GOPATH variable?

3.8k views Asked by At

Can anyone explain when to set $GOPATH? I know it's in the default, C:\Users\Username\go, location, but what if all my coding projects live in Z:<codelocation> ?

1

There are 1 answers

0
Hymns For Disco On BEST ANSWER

From the documentation:

The Go path is used to resolve import statements.

The GOPATH environment variable lists places to look for Go code.

When using modules, GOPATH is no longer used for resolving imports. However, it is still used to store downloaded source code (in GOPATH/pkg/mod) and compiled commands (in GOPATH/bin).

Assuming you're using modules, which you probably should be, the setting of GOPATH will not impact your projects regardless of where you put them. It only needs to be set somewhere so the Go tools have a cache to store libraries and binaries.