0

如何指定从供应商而不是从 GOPATH/GOROOT 导入/使用包?

$GOPATH/src/
$GOPATH/src/github.com/
$GOPATH/src/github.com/myproject/mypkg
$GOPATH/src/github.com/myproject/mypkg/mypkgfile1.go
package mypkg
import "github.com/someproject/somepkg" // importing from vendor

type MyStruct struct {
  Config somepkg.SomeStruct1
}
func New(config somepkg.SomeStruct1) MyStruct {...} 
func (m *MyStruct) DoSomething() {
  a := somepkg.SomeStruct1{}
  b := somepkg.SomeStruct2{}
  // do something with 'a' and 'b'
  out := somepkg.SomeFunc(a) 
}
func (m *MyStruct) MyFunc(input SomeStruct1) (output SomeStruct2, err error) {...}
$GOPATH/src/github.com/myproject/mypkg/mypkgfile2.go
$GOPATH/src/github.com/myproject/mypkg/vendor/github.com/someproject/somepkg/
$GOPATH/src/github.com/myproject/mypkg/vendor/github.com/someproject/somepkg/somepkgfile1.go
package somepkg
type SomeStruct1 struct {...}
type SomeStruct2 struct {...}
func SomeFunc(input SomeStruct1) (output SomeStruct2) {...}
$GOPATH/src/github.com/myproject/mypkg/go.mod
module gitHub.com/myproject/mypkg
go 1.1.4

require github.com/someproject/somepkg v1.0.0
$GOPATH/src/github.com/someproject/somepkg/somepkgfile1.go
package somepkg
type SomeStruct1 struct {...}
type SomeStruct2 struct {...}
func SomeFunc() {...}
$GOPATH/src/github.com/someproject/somepkg/go.mod
module gitHub.com/someproject/somepkg
go 1.1.4

require github.com/someproject/somepkg v1.0.0
$GOPATH/src/github.com/anotherproject/anotherpkpg/somepkgfile1.go
package main
import (
  "github.com/someproject/somepkg"
  "github.com/myproject/mypkg"
)
func main() {
  // do something with somepkg
  somepkg.SomeFunc()
  s := somepkg.SomeStruct1{...} 
  myData := mypkg.New(s) 
  m := mypkg.MyFunc()
  x := somepkg.SomeStruct1{...}
  y := mypkg.MyFunc(x)
}
$GOPATH/src/github.com/someproject/somepkg/go.mod
module gitHub.com/someproject/somepkg
go 1.1.4

require (
  github.com/myproject/mypkg v1.0.0
  github.com/someproject/somepkg v1.0.0
)

当我构建/运行 anotherpkpg/main.go 时,我不断收到类型不匹配错误,例如:

cannot use &s (type *"someproject/somepkg".SomeStruct1) as type *"myproject/mypkg/vendor/github.com/someproject/somepkg".SomeStruct1 in argument to mypkg.New

根本不可能做到这一点?我知道如果 somepkg 的版本/发行版不同,则可能会发生类型不匹配。但是没有办法引用 vendored somepkg?我认为当我这样做时它会变得更加复杂

4

1 回答 1

1

vendor目录在 GOPATH 模式下的工作方式与在模块模式下不同。

由于github.com/myproject/mypkg/go.mod存在,您可能github.com/myproject/mypkg正在模块模式下构建及其依赖项。在模块模式下,只vendor使用主模块的内容,vendor其他包的目录总是被忽略,所以每个包只有一个位置和一个定义。(-mod=vendor每个包都是从您的vendor目录中加载的;-mod=readonly每个-mod=mod包都是从模块缓存中加载的。)

由于github.com/anotherproject/anotherpkg/go.mod存在,Go 1.15 及更早版本将默认以 GOPATH 模式构建它。在GOPATHmode 中,import每个目录中的语句引用所有父目录的子树中的包,并且即使共享相同的导入路径vendor,供应商的包也被视为不同的包。

对于您的情况,正确的长期解决方案可能是开始github.com/anotherproject/anotherpkg以模块模式构建,并在需要时使用replace指令将其指向您的github.com/myproject/mypkg.

您可以使用以下命令进行设置:

# Enable module mode for anotherpkg.
cd $GOPATH/src/github.com/anotherproject/anotherpkg
go mod init github.com/anotherproject/anotherpkg

# Use the local copy of github.com/myproject/mypkg instead of the latest GitHub snapshot.
go mod edit -replace github.com/myproject/mypkg=$GOPATH/src/github.com/myproject/mypkg
go get -d github.com/myproject/mypkg

# Resolve any additional missing dependencies to their latest versions.
go mod tidy
于 2021-06-25T19:00:15.057 回答