英文:
Add module as git submodule inside main package
问题
我对golang还比较新手,遇到了一些创建新模块的问题。
我想在我的主包中添加一个git子模块,这样我就可以同时对两个包进行工作和提交。
将模块http_fs
作为git子模块添加如下:
git submodule add git@github.com:xxx/http_fs.git repo/http_fs
主包如下:
package main
import "repo/http_fs"
http_fs
模块的go.mod
文件如下:
module github.com/xxx/http_fs
go 1.19
当我尝试用go run main.go
运行主包时,出现以下错误:
package repo/http_fs is not in GOROOT (/usr/local/go/src/repo/http_fs)
文件结构如下:
./main.go // 主包
./repo/http_fs/http_fs.go
更新
主包中的go.mod
文件如下:
module main
go 1.19
replace github.com/xxx/http_fs v1 => ./repo/http_fs
英文:
I'm fairly new to golang, and have some issues creating a new module
I want to add a git submodule inside my main package so I can work and make commits to both packages at the same time
The module http_fs
is added as a git submodule like this
git submodule add git@github.com:xxx/http_fs.git repo/http_fs
The main package
package main
import "repo/http_fs"
go.mod
for http_fs
module looks like this
module github.com/xxx/http_fs
go 1.19
When I try to run the main package with go run main.go
I get this error
package repo/http_fs is not in GOROOT (/usr/local/go/src/repo/http_fs)
File structure
./main.go // main package
./repo/http_fs/http_fs.go
update
go.mod
in the main package
module main
go 1.19
replace github.com/xxx/http_fs v1 => ./repo/http_fs
答案1
得分: 3
错误的原因是go.mod
文件中声明的模块是github.com/xxx/http_fs
,而不是repo/http_fs
。你需要导入与go.mod
中指定的模块完全相同的模块,即github.com/xxx/http_fs
。
在你的主模块的go.mod
文件中使用replace
指令:
replace github.com/xxx/http_fs v1.2.3 => ./repo/http_fs
replace
指令告诉编译器在哪里找到模块的源代码。
英文:
The reason for the error
package repo/http_fs is not in GOROOT (/usr/local/go/src/repo/http_fs)
is that go.mod
in /usr/local/go/src/repo/http_fs
declares the modules github.com/xxx/http_fs
, not repo/http_fs
.
You need to import exactly the same module as specified in the go.mod
, i.e. github.com/xxx/http_fs
In go.mod
of your main module use replace
directive:
replace github.com/xxx/http_fs v1.2.3 => ./repo/http_fs
Replace directive tells compiler where to find the sources of the module.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论