douzhi6365 2018-07-27 14:44
浏览 75
已采纳

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

Golang 1.11beta2 introduced experimental support for Modules.

I am failing to import a specific package from a go module.

This is the error when building the application:

$ 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

Why is the import shown above failing?

This is the package 'lib' from module 'modlib':

# 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

This is the application '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

The git repository is tagged with 'modlib/v1.0.0'. This is how one publishes a version for a module.

Go version:

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

I have posted this doubt also on golang-nuts: Host two distinct modules in one git repo?

  • 写回答

1条回答 默认 最新

  • duan5362 2018-08-02 22:50
    关注

    go clean -modcache fixed the issue.

    More details here: https://github.com/golang/go/issues/26695

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条