使用throttled(“gopkg.in/throttled/throttled.v2”)库时出现错误。

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

Go get error when using throttled ("gopkg.in/throttled/throttled.v2") library

问题

当我尝试使用go get命令安装throttled时:

go get "github.com/throttled/throttled"

我遇到了以下错误:

无法加载包:包github.com/throttled/throttled的代码在目录/Users/litanhua/GoglandProjects/cloudstorage/src/github.com/throttled/throttled中期望导入"gopkg.in/throttled/throttled.v2"

英文:

When I attempt to install throttled using go get command:

go get "github.com/throttled/throttled"

I get the error:

can't load package: package github.com/throttled/throttled: code in directory /Users/litanhua/GoglandProjects/cloudstorage/src/github.com/throttled/throttled expects import "gopkg.in/throttled/throttled.v2"

答案1

得分: 4

你看到的是Go 1.4中引入的规范导入路径

语法是在包声明中的一行注释,例如:

package pdf // import "rsc.io/pdf"

如果指定了规范导入路径,你只能使用其规范导入路径导入和获取该包,该路径可能与托管服务的URL不同,就像你的情况一样。

你所提到的包使用了规范导入路径,请参见github.com/throttled/throttled/doc.go

// Package throttled implements rate limiting access to resources such
// as HTTP endpoints.
package throttled // import "gopkg.in/throttled/throttled.v2"

规范导入路径由go工具强制执行。删除标记规范导入路径的注释是解决问题的一个非常糟糕的方法。

只需按照规范导入路径进行获取:

go get gopkg.in/throttled/throttled.v2

然后当然可以使用/引用规范导入路径的包,例如:

import "gopkg.in/throttled/throttled.v2"

这也在throttled的主页上有说明,安装部分

> throttled使用gopkg.in进行语义化版本控制:go get gopkg.in/throttled/throttled.v2

有关该主题的更多帖子:

Go 1.4自定义导入路径检查-设计文档

Golang中的规范导入路径

英文:

What you see is a Canonical import path introduced in Go 1.4.

The syntax is a line comment in the package declaration, e.g.:

package pdf // import "rsc.io/pdf"

If canonical import path is specified, you are only allowed to import and go-get the package using its canonical import path, which may be different from the hosting service URL, such as your case.

The package you refer to uses canonical import path, see github.com/throttled/throttled/doc.go:

// Package throttled implements rate limiting access to resources such
// as HTTP endpoints.
package throttled // import "gopkg.in/throttled/throttled.v2"

Canonical import paths are enforced by the go tool. Deleting the comments denoting the the canonical import paths is a really bad way to go about solving your problem.

Simply go get by the canonical import path:

go get gopkg.in/throttled/throttled.v2

And then of course use / refer to the packages by the canonical import path, e.g.

import "gopkg.in/throttled/throttled.v2"

This is also noted on throttled's home page, Installation section:

> throttled uses gopkg.in for semantic versioning: go get gopkg.in/throttled/throttled.v2

Further posts about the topic:

Go 1.4 Custom Import Path Checking - Design document

Canonical import paths in Golang

huangapple
  • 本文由 发表于 2017年4月26日 16:57:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/43629475.html
匿名

发表评论

匿名网友

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

确定