项目之间的类型冲突

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

Type conflicts between projects

问题

我想将特定子域的代码移动到自己的项目中,并将其导入到当前所在的主代码库中。我能够成功地将子域的代码导入到主项目中,直到我添加Gorilla Mux代码为止。例如,以下代码是有效的:

// 简化起见,省略了导入和与问题无关的路由
r := mux.NewRouter()
// 主站点路由在这里...
s := r.Host("子域名正则表达式").Subrouter()
s.HandleFunc("/", people.Index)
http.ListenAndServe("localhost:8080", r)

但是,当我将子域移动到自己的项目并导入它时,然后调用LoadRoutes()函数并传入来自主站点的mux.Router对象时,我收到一个错误。以下是代码:

// 主项目
r := mux.NewRouter()
// 主站点路由在这里...

// 位于子域Go项目中的函数,已被导入
func LoadRoutes(host string, r *m.Router) {

s := r.Host(host).Subrouter()
s.HandleFunc("/", people.Index)
s.HandleFunc("/people", people.Index)
s.HandleFunc("/person/new", people.New)

}

**# command-line-arguments
./main.go:25: cannot use r (type "primary_site/vendor/github.com/gorilla/mux".Router) as type "subdomain_site/vendor/github.com/gorilla/mux".Router in argument to routers.LoadRoutes

看起来我有两个独立项目中的Gorilla Mux实例发生了冲突。我只是将子域站点的包导入到主站点,而不是反过来。只要我将代码放在一个项目中,这段代码就能完美运行,但是当我尝试将项目分开时,它就会出错。

由于我传入了mux.NewRouter()的实例,为什么会发生冲突呢?

英文:

I want to move code used by a specific subdomain its own project, which will be imported by the main code base which is currently resides in. I am able to import code from the subdomain into the main project successfully, until I add the Gorilla Mux code. For example, this works:

// imports and non-relevant routes removed for simplicity
r := mux.NewRouter()
// Primary site routes here...
s := r.Host("subdomain-regex-here").Subrouter()
s.HandleFunc("/", people.Index)
http.ListenAndServe("localhost:8080", r)

But when I move the subdomain to its own project and import it, then call the LoadRoutes() function which passes in the mux.Router object from the primary site, I receive an error. Here's the code:

// Primary Project
r := mux.NewRouter()
// Primary site routes here...

// function located in the subdomain go project, which is imported
func LoadRoutes(host string, r *m.Router) {

	s := r.Host(host).Subrouter()
	s.HandleFunc("/", people.Index)
	s.HandleFunc("/people", people.Index)
	s.HandleFunc("/person/new", people.New)
}

**# command-line-arguments
./main.go:25: cannot use r (type "primary_site/vendor/github.com/gorilla/mux".Router) as type "subdomain_site/vendor/github.com/gorilla/mux".Router in argument to routers.LoadRoutes

It looks like I have two instances of the Gorilla Mux, from two separate projects, that are conflicting. I only import packages from the subdomain site to the primary site, not the other way around. This exact code works perfectly as long as I have it in a single project, but when I try to separate the projects, it breaks.

Since I pass in the instance of mux.NewRouter(), why am I having a conflict?

答案1

得分: 2

你的项目中有两个供应商目录。为了在包之间共享供应商类型,你需要将它们合并为一个顶级供应商目录。删除 subdomain_site/vendor,只使用主包中的供应商目录。

英文:

You have 2 vendor directories in your project. You need to flatten them into a single vendor directory at the top level in order to share vendored types between packages. Remove the subdomain_site/vendor and only use the vendor directory in your main pacakage.

huangapple
  • 本文由 发表于 2016年12月10日 08:52:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/41071033.html
匿名

发表评论

匿名网友

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

确定