无法导入 Go 模块。

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

Can't import a Go module

问题

你好!根据你提供的信息,你在学习Go语言并尝试构建一个Go文件。你遇到的问题是在使用命令go build main.go构建程序时出现了错误。错误信息提示缺少github.com/go-chi/chi/@v1.5.4go.sum条目。

为了解决这个问题,你可以尝试执行以下步骤:

  1. 确保你的Go版本是1.16或更高版本,因为你的go.mod文件中指定了go 1.16
  2. 打开终端或命令提示符,并导航到你的项目目录。
  3. 运行以下命令来下载缺少的依赖项:
    go mod tidy
    

    这将根据你的go.mod文件下载并更新所有依赖项。

如果上述步骤没有解决问题,你可以尝试手动编辑go.mod文件,将require行修改为:

require github.com/go-chi/chi v1.5.4

然后再次运行go mod tidy命令。

希望这些步骤能够帮助你解决问题!如果还有其他疑问,请随时提问。

英文:

I'm learning Go ad I'm trying to build go file:

package main

import (
	"github.com/go-chi/chi/v5"
	"github.com/go-chi/chi/v5/middleware"
	"net/http"
)

func main() {
	r := chi.NewRouter()
	r.Use(middleware.Logger)
	r.Get("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("welcome"))
	})
	http.ListenAndServe(":3000", r)
}

But when I build the program with the command go build main.go, it outputs:

go: github.com/go-chi/chi/@v1.5.4: missing go.sum entry; to add it:
        go mod download github.com/go-chi/chi/

go.mod:

module exprog

go 1.16

require github.com/go-chi/chi/ v1.5.4

when I execute go mod download github.com/go-chi/chi/, I get this error:

go: github.com/go-chi/chi/@v1.5.4: malformed module path "github.com/go-chi/chi/": trailing slash

What I should do?

答案1

得分: 2

这是错误不在命令中,而是在 go.mod 文件中。你可以通过以下方式修复:

module exprog

go 1.16

require github.com/go-chi/chi/v5 v5
英文:

It's error not in command, it's error in go.mod file. You can fix by:

module exprog

go 1.16

require github.com/go-chi/chi/v5 v5

答案2

得分: -1

假设你想下载这个模块,你应该执行go mod download github.com/go-chi/chi/v5。模块的名称是依赖的go.mod文件中的标题。

你可以从go.mod文件中删除该条目,然后简单地执行go mod download github.com/go-chi/chi/v5

或者

你可以从go.mod文件中删除该条目,然后执行go mod tidy。Go将根据你的导入填充go.mod文件。然后你可以执行go mod download

英文:

Assuming you're looking to download this module, You should do go mod download github.com/go-chi/chi/v5. The name of the module is the header in the dependency's go.mod file.

You can remove the entry from go.mod and simply do go mod download github.com/go-chi/chi/v5

or

You can remove the entry from go.mod, and do go mod tidy. Go will fill your go.mod file based on your imports. You can do go mod download then.

huangapple
  • 本文由 发表于 2021年9月27日 22:25:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/69348359.html
匿名

发表评论

匿名网友

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

确定