gpt4 book ai didi

go - 如何拥有不能用于具有不同 namespace 的同一软件包的两个副本

转载 作者:行者123 更新时间:2023-12-01 22:05:52 25 4
gpt4 key购买 nike

这是博客文章的引文:

Enforce vanity URLs go get supports getting packages by a URL that is

different than the URL of the package's repo. These URLs are called vanity URLs and require you to serve a page with specific meta tags the Go tools recognize. You can serve a package with a custom domain and path using vanity URLs.

For example,

$ go get cloud.google.com/go/datastore

checks out the source code from https://code.googlesource.com/gocloud behind the scenes and puts it in your workspace under $GOPATH/src/cloud.google.com/go/datastore.

Given code.googlesource.com/gocloud is already serving this package, would it be possible to go get the package from that URL? The answer is no, if you enforce the vanity URL.

To do that, add an import statement to the package. The go tool will reject any import of this package from any other path and will display a friendly error to the user. If you don't enforce your vanity URLs, there will be two copies of your package that cannot work together due to the different namespace.

package datastore // import "cloud.google.com/go/datastore"



有人可以解释最后一行的含义吗?

If you don't enforce your vanity URLs, there will be two copies of your package that cannot work together due to the different namespace.



还是在示例中进行演示?

最佳答案

很简单

如果您的软件包托管在,例如github.com/foo/bar,并且您有个性化网址foo.com/bar,那么有人可以同时执行以下两种操作:

import "github.com/foo/bar"


import "foo.com/bar"

如果您有不同的文件或程序包在不同的路径导入同一程序包,这将是有问题的。

想象这两个文件在同一个包中:
// foo.go
package foo

import "github.com/foo/bar"

func frobnicate(x *bar.Something) { /* ... */ }
// bar.go
package foo

import "foo.com/bar"

func widget() {
x := *bar.Something{}
frobnicate(x) // compilation error: cannot use x (type foo.com/bar.Something) as type github.com/foo/bar.Something
}

关于go - 如何拥有不能用于具有不同 namespace 的同一软件包的两个副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61956477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com