duanqing2209 2016-12-10 00:52
浏览 20

项目之间的类型冲突

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条回答 默认 最新

  • doucan8049 2016-12-10 01:40
    关注

    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.

    评论

报告相同问题?