I have a function which open db connection and return it. Or error, if something happened:
OpenDbConnection(connectionString string, logSql bool) (*gorm.DB,
You can name your returning error variable and initialize anywhere inside the function.
check this test code here
OpenDbConnection(connectionString string, logSql bool) (db *gorm.DB, err error) {
logger := zap.NewExample().Sugar()
defer func() {
err = logger.Sync()
}()
// some logic here
return db, err
}