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

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

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题