Trouble with using gorilla/mux package in mac

674 Views Asked by At

I am trying to learn how to build a webserver using go and mux. I am importing mux to the main.go file as import github.com/gorilla/mux. However, when I am trying to run the code. I get the following error

no required module provides package github.com/gorilla/mux: go.mod file not found in current directory or any parent directory; see 'go help modules'

My GOPATH is /Users/michiokaku/Study/go

The overall structure of my directories is

go___
     pkg
     bin
     my_codes___
                main.go

Inside pkg, I found a directory named [email protected] in the path pkg/mod/github.com/gorilla. I think this is what I downloaded using go get -u github.com/gorilla/mux. But when the code is running, I am getting errors.

What is the issue here? How do I solve this?

PS: I am using mac.

1

There are 1 best solutions below

5
On

Read through Tutorial: Getting Started with Go, if you haven't seen it already. It matches your situation pretty closely.

In short:

  • Run go mod init example.com/projectname, replacing the last argument with the name for your module. This will create a go.mod file in the current directory that will track your dependencies. Your module's name will be a prefix for all packages within your module.
  • Run go mod tidy or go get github.com/gorilla/mux to add github.com/gorilla/mux as a dependency.

You mentioned you saw a directory pkg/mod/github.com/gorilla earlier. This is part of Go's module cache, shared by all projects.