没有VCS/Git的模块初始化失败,无法确定模块路径 [英] Go module init without VCS/Git fails with cannot determine module path
问题描述
我正在尝试使用go模块(使用go 1.11)初始化一个新的go项目.我不打算在github或其他地方发布它,这只是一个临时/测试项目,只有主软件包.
每当我尝试在目录(在我的 $ GOPATH
之外)中运行 go mod init
时,都会出现此错误:>
go:无法确定源目录/Users/...的模块路径(在GOPATH外部,没有导入注释)
不使用git(或其他VCS)就不可能初始化模块吗?还是有任何解决方法?
不使用git(或其他VCS)就不可能初始化模块吗?或者有什么解决方法吗?
是的,可以在不使用VSC的情况下初始化模块,初始化模块无需使用git或任何其他VCS进行任何操作.
在初始化
模块时未输入模块名称,从而生成模块时,将发生此错误.
modulename 编写此命令.
$ go mod init模块名
go.mod
的内容应为
模块模块名称
要使用本地存储库中的模块,请使用 replace
指令
在要检查本地模块的主模块中,添加以下行
替换"X" v0.0.0 =>"{位置到您的本地模块}"需要"X" v0.0.0
然后在您的主项目中,从模块X导入软件包 util 即可:
导入"X/util"
现在,当您进行 go build
时,它将在您在主项目的mod文件中指定的位置上查找此本地模块.
I'm trying to initialize a new go project with go module (using go 1.11). I don't plan to publish it in github or elsewhere, it is just a temporary/test project with only main package.
Whenever I try to run go mod init
in a directory (that's outside my $GOPATH
), I get this error:
go: cannot determine module path for source directory /Users/... (outside GOPATH, no import comments)
Is it not possible to init module without using git (or other VCS)? Or is there any workaround?
Is it not possible to init module without using git (or other VCS)? Or is there any workaround?
Yes, it is possible to init the modules without using VSC, initializing the module does not have to do anything with git or any other VCS.
This error occurs when the module name is not entered while init
the module so to generate a module modulename
write this command.
$ go mod init modulename
The content of the go.mod
would be
module modulename
EDIT:
To use the modules from local repository use the replace
directive
In your main module where you are checking your local module add the following lines
replace "X" v0.0.0 => "{location To your local module}"
require "X" v0.0.0
And then in your main project, import package util from module X you can simply do:
import "X/util"
Now when you will do go build
it will look for this local module on the location you have given in the mod file of the main project.
这篇关于没有VCS/Git的模块初始化失败,无法确定模块路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!