'golang webapp package structure [duplicate]
I've a simple golang web application, everything in main
package, where http handlers are methods of an App
struct embedding a DB pointer
type App struct {
DB *sqlx.DB
}
func (app *App) Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// i can use app.DB here
}
router.GET("/api/v1/index", app.Index)
Now i need to add other methods, and i was thinking to use packages to keep the code clean.
in main package
type App struct {
DB *sqlx.DB
V2 NewPackage.App
}
in Newpackage
package
type App struct{}
func (app *App) Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// how can i use DB here?
}
router.GET("/api/v2/index", app.V2.Index)
Question: how i can use App.DB in Newpackage?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|