将模块作为git子模块添加到主包中。

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

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.

huangapple
  • 本文由 发表于 2022年10月24日 03:18:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/74174098.html
匿名

发表评论

匿名网友

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

确定