将子目录导入为go模块

时间:2019-10-19 15:46:10

标签: go

我正在尝试从子目录导入go模块。我的存储库看起来像

+$ tree .
.
└── bar
    ├── bar.go
    └── go.mod

1 directory, 2 files
+$ cat bar/go.mod
module github.com/graywolf/foo/bar

go 1.13
+$ cat bar/bar.go
package bar

func Bar() string {
        return "bar"
}

当推送到github时,此源代码运行正常

+$ cat github.go
package main

import (
        "fmt"
        "github.com/graywolf/foo/bar"
)

func main() {
        fmt.Println(bar.Bar())
}
+$ go run github.go
go: finding github.com/graywolf/foo latest
go: finding github.com/graywolf/foo/bar latest
go: downloading github.com/graywolf/foo v0.0.0-20191019144834-ffb419608ae6
go: extracting github.com/graywolf/foo v0.0.0-20191019144834-ffb419608ae6
bar

但是,当我将其推送到其他git托管时,它将停止工作。我调整了网址

diff --git a/bar/go.mod b/bar/go.mod
index 7407848..6134a24 100644
--- a/bar/go.mod
+++ b/bar/go.mod
@@ -1,3 +1,3 @@
-module github.com/graywolf/foo/bar
+module git.sr.ht/~graywolf/foo/bar

 go 1.13

提交并推送并尝试运行

+$ cat sr.go
package main

import (
        "fmt"
        "git.sr.ht/~graywolf/foo/bar"
)

func main() {
        fmt.Println(bar.Bar())
}
+$ go run sr.go
go: finding git.sr.ht/~graywolf/foo latest
go: downloading git.sr.ht/~graywolf/foo v0.0.0-20191019153505-33a4721605aa
go: extracting git.sr.ht/~graywolf/foo v0.0.0-20191019153505-33a4721605aa
build command-line-arguments: cannot load git.sr.ht/~graywolf/foo/bar: module git.sr.ht/~graywolf/foo@latest (v0.0.0-20191019153505-33a4721605aa) found, but does not contain package git.sr.ht/~graywolf/foo/bar

看起来github和源小屋提供了不同的go-import元标记,请注意源小屋缺少的.git

+$ curl -sSf https://github.com/graywolf/foo?go-get=1 | grep -A1 go-import
  <meta name="go-import" content="github.com/graywolf/foo git https://github.com/graywolf/foo.git">

+$ curl -sSf https://git.sr.ht/~graywolf/foo?go-get=1 | grep -A1 go-import
<meta name="go-import"
  content="git.sr.ht/~graywolf/foo git https://git.sr.ht/~graywolf/foo">

当我编辑源代码以使用foo.git时,它确实找到了模块,但是提供了不同的错误消息:

+$ go run sr.go
go: finding git.sr.ht/~graywolf/foo.git latest
go: finding git.sr.ht/~graywolf/foo.git/bar latest
go: downloading git.sr.ht/~graywolf/foo.git v0.0.0-20191019153505-33a4721605aa
go: downloading git.sr.ht/~graywolf/foo.git/bar v0.0.0-20191019153505-33a4721605aa
go: extracting git.sr.ht/~graywolf/foo.git v0.0.0-20191019153505-33a4721605aa
go: extracting git.sr.ht/~graywolf/foo.git/bar v0.0.0-20191019153505-33a4721605aa
go: git.sr.ht/~graywolf/foo.git/bar: git.sr.ht/~graywolf/foo.git/bar@v0.0.0-20191019153505-33a4721605aa: parsing go.mod:
        module declares its path as: git.sr.ht/~graywolf/foo/bar
                but was required as: git.sr.ht/~graywolf/foo.git/bar

go.mod位于存储库的根目录中时,(github和源代码小屋)都可以正常工作,仅当它位于子目录中时,问题才发生。

我想我的问题是我该怎么办。有人遇到过这样的事情吗?为什么不只克隆~graywolf/foo而不检查bar子目录?

0 个答案:

没有答案