代码之家  ›  专栏  ›  技术社区  ›  Everton

如何从Go模块导入特定的包?

  •  0
  • Everton  · 技术社区  · 7 年前

    Golang 1.11beta2为 Modules .

    我无法从go模块导入特定包。

    这是生成应用程序时的错误:

    $ go install
    go: downloading github.com/udhos/modhello/modlib/lib v1.0.0
    go: finding github.com/udhos/modhello latest
    go: import "github.com/udhos/modhello/modapp" ->
        import "github.com/udhos/modhello/modlib/lib": cannot find module providing package github.com/udhos/modhello/modlib/lib
    

    为什么上面显示的导入失败?

    这是模块“modlib”中的包“lib”:

    # repo: modhello
    # module: modlib
    # package: lib
    $ cat modhello/modlib/lib/modlib.go
    package lib
    func Sum(a, b int) int {
        return a + b
    }
    $ cat modhello/modlib/go.mod
    module github.com/udhos/modhello/modlib
    

    这是应用程序“modapp”:

    $ cat modhello/modapp/main.go
    package main
    import (
            "log"
            "github.com/udhos/modhello/modlib/lib"
    )
    func main() {
            run(1, 2)
    }
    func run(a, b int) {
            log.Printf("Sum(%d,%d) = %d", a, b, lib.Sum(a, b))
    }
    $ cat modhello/modapp/go.mod
    module github.com/udhos/modhello/modapp
    require github.com/udhos/modhello/modlib v1.0.0
    

    git存储库被标记为“modlib/v1.0.0”。这就是发布模块版本的方式。

    Go版本:

    $ go version
    go version go1.11beta2 linux/amd64
    $ git --version
    git version 2.18.0
    

    我把这个疑问也贴在 golang-nuts : Host two distinct modules in one git repo?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Everton    7 年前

    go clean -modcache 解决了这个问题。

    更多详情请点击此处: https://github.com/golang/go/issues/26695