如何在外部包中对函数使用售卖参数?

How do I use a vendored argument to a function in an external package?

我有包a,依赖外部包,语言包:

package a

import (
    "fmt"

    "golang.org/x/text/language"
)

// Machine is a printer
type Machine struct{}

// Printer prints
type Printer interface {
    Print(lang language.Tag)
}

// Print prints the language
func (p *Machine) Print(l language.Tag) {
    fmt.Println(l.String())
}

对于套餐 a,我有 运行 "dep init" 然后 "dep ensure"。

在另一个包中,我有一个 main.go 文件,它导入包 a:

package main

import (
    "testing/a"

    "golang.org/x/text/language"
)

func main() {
    m := a.Machine{}
    m.Print(language.MustParse("en"))
}

我收到一个错误:

cannot use "golang.org/x/text/language".MustParse("en") (type "golang.org/x/text/language".Tag) as type "testing/a/vendor/golang.org/x/text/language".Tag in argument to m.Print

如果我把主包放在包a里,就没问题了。为什么从外部包调用时不起作用?

Go 版本为 1.10.2

编辑: 我对包 a 有完全的控制权,所以我可以改变我在那里卖东西的方式。如果更新的 Go 版本可以轻松修复,我也可以升级我的 Go 版本。

更新: 我已经升级到 Go 1.12.1 并删除了现有的供应商目录。我 运行 "go mod init" 和 "go mod vendor" 用于包 a 但当我 运行 main.go 包 b.

时仍然得到相同的错误
cannot use "testing/b/vendor/golang.org/x/text/language".MustParse("en") (type "testing/b/vendor/golang.org/x/text/language".Tag) as type "testing/a/vendor/golang.org/x/text/language".Tag in argument to m.Print

如果我尝试直接从包 a 导入 vendored 包,我得到:

use of vendored package not allowed

如果需要使用依赖项的出售副本,则标志 -mod=vendor 将传递给 go run

例如,

> tree
.
|-- a
|   `-- machine.go
`-- main.go

1 directory, 2 files

creating the module with the name testing because package a is imported as testing/a in main

> go mod init testing
go: creating new go.mod: module testing
> go mod vendor #this creates a vendor directory inside the project
go: finding golang.org/x/text/language latest
> tree -L 4
.
|-- a
|   `-- machine.go
|-- go.mod
|-- go.sum
|-- main.go
`-- vendor
    |-- golang.org
    |   `-- x
    |       `-- text
    `-- modules.txt

5 directories, 5 files  
> cat go.mod
module testing

go 1.12

require golang.org/x/text v0.3.0
> go run -mod=vendor main.go
en

上一个答案

下面是我的目录结构:

>tree /F
sample
│---go.mod
│---go.sum
│---main.go
│
└───a
    |---machine.go

转到版本

> go version
go version go1.12 windows/amd64

模块创建

creating the module with the name testing because package a is imported as testing/a in main

> go mod init testing
go: creating new go.mod: module testing

程序执行

> go run main.go
go: finding golang.org/x/text/language latest
en