Can any one suggest any another approach to deal with initializing a database outside init function in my server.go program ?
I am using a MySQL in my program and it is my requirement to initialize and connect and send the handler to the controllers.
You cannot return a value with the init()
function but what you can do is initialize global(package) variables with it so you can try something like that:
package mysql
var Conn Connection
func init(){
Conn = ...
}
And now the controllers can access your connection importing your package and accessing your already initialized connection.
package controllers
import(
"mysql"
)
func abc(){
mysql.Conn ...
}