When should you set the $GOPATH variable?

Issue

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> ?

Solution

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.

Answered By – Hymns For Disco

Answer Checked By – Marilyn (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.