package driverrepo import ( "context" "go-udemy-web-1/internal/repository" "github.com/jackc/pgx/v5" ) func (s *sqlcDriverRepo) ConnectSQL(dsn string) (*repository.DB, error) { ctx := context.Background() c, err := s.newConn(ctx, dsn) if err != nil { panic(err) } dbConn.PG = c err = s.testDB(ctx, c) if err != nil { return nil, err } return dbConn, nil } // testDB tries to ping the database func (s *sqlcDriverRepo) testDB(ctx context.Context, c *pgx.Conn) error { err := c.Ping(ctx) if err != nil { return err } return nil } // newDatabase creates a new database for the application func (s *sqlcDriverRepo) newConn(ctx context.Context, dsn string) (*pgx.Conn, error) { conn, err := pgx.Connect(ctx, dsn) if err != nil { return nil, err } // Ping test the conn if err = conn.Ping(ctx); err != nil { return nil, err } return conn, err }