格式错误的模块路径"xxxx/xxxx/uuid"从基于GOPATH的dep迁移到mod时,第一个路径元素中缺少点 [英] malformed module path "xxxx/xxxx/uuid" missing dot in first path element when migrating from GOPATH based dep to go mod

查看:80
本文介绍了格式错误的模块路径"xxxx/xxxx/uuid"从基于GOPATH的dep迁移到mod时,第一个路径元素中缺少点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ go version
1.13.3

我的文件夹结构如下:

GOPATH
+---src
     +--- my-api-server
           +--- my-auth-server
                 +--- main.go
           +--- my-utils
                 +--- uuid
                       +--- uuid.go

my-auth-server使用my-api-server/my-utils/uuid作为依赖体

现在,当我使用基于GOPATH的模块系统时,它可以正常工作.但是当使用go模块时,当我在my-auth-server中运行go run main.go时,它返回错误:

Now, when I used the GOPATH based module system, this worked fine. But when using go modules, when I run go run main.go in my-auth-server it returned error:

build command-line-arguments: cannot load my-api-server/my-utils/uuid: malformed module path "my-api-server/my-utils/uuid": missing dot in first path element

有什么办法解决这个问题吗?

Any idea how to solve this?

推荐答案

go.mod文件应位于项目的根目录(在本例中为my-api-server/go.mod).

The go.mod file should be at the root of your project (in this case, my-api-server/go.mod).

模块路径的第一部分应该是域/路径.例如,完整路径可能是github.com/your-github-username/my-api-server.您看到的错误是因为第一部分不是域(带有句点).您无需发布该模块即可进行开发,但是您需要使用适当的域名.

The first part of the module path should be a domain/path. For example, the full path might be github.com/your-github-username/my-api-server. The error you're seeing is because the first part is not a domain (with a period). You don't have to publish the module to develop it, but you need to use a proper domain name.

一旦有了模块路径,就可以使用完整的模块路径+"/" +软件包的相对路径导入该模块中包含的软件包.例如,

Once you have a module path, you can import packages contained in that module using the full module path + "/" + the relative path of the package. For example,

import "github.com/your-github-username/my-api-server/my-utils/uuid"

由于main.gouuid包含在同一模块中,因此在go.mod文件中不需要require语句即可使用uuid包.您可以像导入其他任何软件包一样导入它.

Since main.go and uuid are contained in the same module, you don't need a require statement in the go.mod file to use the uuid package. You can import it like any other package and it will work.

我建议使用go build并运行生成的可执行文件,而不是使用go run来确保在构建过程中包括了所需的所有文件.

I recommend using go build and running the resulting executable rather than using go run to make sure you include all of the files you need in the build process.

请参见 https://blog.golang.org/using-go-modules 了解如何使用Go模块,包括该系列中的第二篇文章关于如何将项目转换为使用模块的信息.

See https://blog.golang.org/using-go-modules for a walkthrough of how to use Go modules, including the second post in that series about how to convert a project to use modules.

这篇关于格式错误的模块路径"xxxx/xxxx/uuid"从基于GOPATH的dep迁移到mod时,第一个路径元素中缺少点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆