Golang中找不到提供github.io/xxx包的模块怎么办

Golang中找不到提供github.io/xxx包的模块怎么办 我正在尝试通过这篇文章学习Go语言中的gRPC,因此我使用了上传到此处的代码,其中包含3个文件夹:

  • server
  • client
  • hellopb(这是由proto3代码生成的Go代码)

server.goclient.go 中,我都有以下导入:

package main

import (
	"context"
	"fmt"
	"log"

	"github.io/hajsf/grpc/hellopb"
	"google.golang.org/grpc"
)

但是,当我运行 go mod tidy 时,遇到了以下错误:

PS D:\Documents\grpc\client> go mod tidy
go: finding module for package github.io/hajsf/grpc/hellopb
github.io/hajsf/grpc/client imports
        github.io/hajsf/grpc/hellopb: cannot find module providing package github.io/hajsf/grpc/hellopb: unrecognized import path "github.io/hajsf/grpc/hellopb": parse https://github.io/hajsf/grpc/hellopb?go-get=1: no go-import meta tags ()

这里可能是什么问题?该如何解决?


更多关于Golang中找不到提供github.io/xxx包的模块怎么办的实战教程也可以访问 https://www.itying.com/category-94-b0.html

2 回复

抱歉,这是我的错误,我误用了 github.io,而本应使用 gethub.com。因此,我修正了模块名称和路径,并使用以下命令清理了模块导入:

go clean -modcache

然后,使用以下命令重新导入了所需的模块:

go mod tidy

更多关于Golang中找不到提供github.io/xxx包的模块怎么办的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


这个错误是因为使用了错误的模块路径。正确的GitHub模块路径应该是 github.com 而不是 github.io

在你的代码中,需要将导入路径从:

"github.io/hajsf/grpc/hellopb"

修改为:

"github.com/hajsf/grpc/hellopb"

同时,你需要在项目根目录初始化go module并设置正确的模块路径。以下是具体步骤:

  1. 在项目根目录创建go.mod文件:
go mod init github.com/hajsf/grpc
  1. 更新server.go和client.go中的导入语句:
package main

import (
    "context"
    "fmt"
    "log"

    "github.com/hajsf/grpc/hellopb"
    "google.golang.org/grpc"
)
  1. 运行go mod tidy下载依赖:
go mod tidy

如果hellopb包是本地生成的protobuf代码,你还需要确保hellopb目录中有正确的go.mod文件,或者将其作为子模块处理。如果是本地包,可以在go.mod中使用replace指令:

module github.com/hajsf/grpc

go 1.21

replace github.com/hajsf/grpc/hellopb => ./hellopb

require (
    github.com/hajsf/grpc/hellopb v0.0.0
    google.golang.org/grpc v1.60.1
)

然后运行:

go mod tidy
回到顶部