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.
Read through Tutorial: Getting Started with Go, if you haven't seen it already. It matches your situation pretty closely.
In short:
go mod init example.com/projectname
, replacing the last argument with the name for your module. This will create ago.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.go mod tidy
orgo get github.com/gorilla/mux
to addgithub.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.