How can I better understand packages/modules and the go filesystem?

I have a hard time understanding the packaging/modules and filesystem structure of go.

GOROOT=“C:\Program Files\Go”
GOPATH=“C:\Users\name\Documents/go/workspace”

The reason why I am confused also is because I don’t know where to do my work and why I am unable to import gorilla/mux. Also, I’m unsure of this but is gorilla/mux supposed to appear in my src folder? I tried getting it but it doesn’t appear there and I know I’m in the correct workspace because I removed all folders and reinstalled everything.

could not import github.com/gorilla/mux (cannot find package "github.com/gorilla/mux" in any of 
	C:\Program Files\Go\src\github.com\gorilla\mux (from $GOROOT)
	C\src\github.com\gorilla\mux (from $GOPATH)

Ideally, I am trying to make a website that has a go backend. Not sure if I am setting it up correctly but I am learning as I go.

Help is appreciated!
THANKS!

I think in current Go realities, it would be better to use Go Modules instead of GOPATH if you start with Go language.

Go Modules has several benefits:

  • You can store your codebase independently of the GOPATH workspace.
  • It is easier to get started.
  • GOPATH will be deprecated in the future versions of Go. Go Modules is a de-facto standard approach nowadays.

So, a quick example of how to create projects using Go Modules:

  1. Create a new directory for a project. You can create it anywhere on your mounts.
  2. Initialize go.mod file via go mod init command in the terminal.
  3. Create a new package, main.go file, etc.
  4. Import github.com/gorilla/mux in your files.
  5. Run go mod tidy in the terminal to add module requirements and sums.
  6. Run your application, e.g. go run main.go.
1 Like