I'm using golang to writer a web server application.
Need to use a very common database query function findUserByEmail(DB gorm.DB, email string )
in several controllers in the same package.
not sure where should I declare this function so that I don't need to copy this code in every controller when I need it.
I can make it as FindUserByEmail(DB gorm.DB, email string )
in one of the controller. so that I could use it in any other contollers. but that seems not a good practice?
You have to create a seperate package for your database functions called db for example. create a folder in your project named db and put all codes there.
package db
FindUserByEmail(DB gorm.DB, email string ) {
...
}
When you need it you can access the package like this :
import "myproject/db"
db.FindUserByEmail(gorm,"test@gmail.com")