【发布时间】:2017-04-25 13:24:36
【问题描述】:
我想将特定子域使用的代码移动到它自己的项目中,这些代码将由当前所在的主代码库导入。我能够成功地将子域中的代码导入到主项目中,直到我添加Gorilla Mux 代码。例如,这有效:
// 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)
但是当我将子域移动到它自己的项目并导入它,然后调用从主站点传入 mux.Router 对象的 LoadRoutes() 函数时,我收到一个错误。代码如下:
// 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)
}
# 命令行参数 ./main.go:25: 不能使用 r (type *"primary_site/vendor/github.com/gorilla/mux".Router) 作为类型 *"subdomain_site/vendor/github.com/gorilla/mux".Router 在参数到 routers.LoadRoutes
看起来我有两个 Gorilla Mux 实例,来自两个不同的项目,它们相互冲突。我只将包从子域站点导入到主站点,而不是相反。只要我在一个项目中拥有这个确切的代码,它就可以完美地工作,但是当我尝试分离项目时,它会中断。
既然我传入了mux.NewRouter()的实例,为什么会发生冲突?
【问题讨论】:
-
出于这个原因,您的项目中应该只有一个顶级供应商目录
-
这是两组独立的代码——主站点是一个项目,子域站点是一个包。
-
如果它们都被导入到同一个程序中,就构建工具而言,它们不是分开的。
-
只有子域项目被导入到主网站项目中。两个主文件夹都位于我的 gopath 的 src/ 下,在同一级别。
标签: go