This is a design question, not a bug. I am building a web project in golang and trying to follow best practice.
It seems logical, and is often recommended, to separate, and possibly abstract from, routing. In my case, which seems to be common, I created a file called routes.go
which contains the following
var Router *mux.Router
func AuthRoutes() {
// Export router
Router = mux.NewRouter()
Router.HandleFunc("/auth/login", controllers.LoginHandler)
...
}
Until now, I have put this function, and the variable Router
in a package called router
which lives in a folder caled routes
.
But why not just put the file in the same folder as the main.go
file and make it part of package main
?
I don’t expect that Authroutes
will be used anywhere else but in main
though I guess it’s just possible Router
will be.
So I am thinking of storing routes.go
in the same folder as main.go
, but for readability, in a separate file. That is, its contents will not be in a separate module and will not be part of a separate package but will be in a separate file.
I would like to ask if this decision conforms to good practice.