在Mac上使用gorilla/mux包遇到了问题。

huangapple go评论77阅读模式
英文:

Trouble with using gorilla/mux package in mac

问题

我正在尝试学习如何使用Go和mux构建一个Web服务器。我在main.go文件中导入mux,代码如下:import github.com/gorilla/mux。然而,当我尝试运行代码时,出现以下错误:

未找到所需的模块,无法提供github.com/gorilla/mux包:在当前目录或任何父目录中找不到go.mod文件;请参阅'go help modules'

我的GOPATH是/Users/michiokaku/Study/go

我的目录结构如下:

go___
     pkg
     bin
     my_codes___
                main.go

在pkg目录中,我发现了一个名为mux@v1.8.0的目录,路径为pkg/mod/github.com/gorilla。我认为这就是我使用go get -u github.com/gorilla/mux下载的内容。但是当代码运行时,我遇到了错误。

问题出在哪里?我该如何解决?

PS:我使用的是Mac系统。

英文:

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 mux@v1.8.0 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

得分: 2

阅读教程:Go入门,如果你还没有看过的话。它与你的情况非常相似。

简而言之:

  • 运行go mod init example.com/projectname,将最后一个参数替换为你的模块名称。这将在当前目录中创建一个go.mod文件,用于跟踪你的依赖关系。你的模块名称将成为你的模块中所有包的前缀。
  • 运行go mod tidygo get github.com/gorilla/mux,将github.com/gorilla/mux添加为依赖项。

你提到你之前看到了一个pkg/mod/github.com/gorilla目录。这是Go的模块缓存,被所有项目共享。

英文:

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.

huangapple
  • 本文由 发表于 2021年7月8日 00:40:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/68289971.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定