From 30deaf17175eae5f98e3c1889b60a7fa18132e72 Mon Sep 17 00:00:00 2001 From: abhi9560 <54478454+abhi9560@users.noreply.github.com> Date: Wed, 17 Jan 2024 15:21:50 +0530 Subject: [PATCH] Update utils.go Add proper error handling for database connections and queries to ensure any potential issues are identified. --- utils.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/utils.go b/utils.go index e65cd87..6fc860b 100644 --- a/utils.go +++ b/utils.go @@ -8,6 +8,14 @@ import ( // createConnection creates a connection to mysql database func createConnection() *sql.DB { db, err := sql.Open("mysql", "root:password@tcp(127.0.0.1:3306)/test") - fmt.Println("sql open " + err.Error()) - return db + if err != nil { + return nil, err + } + + err = db.Ping() + if err != nil { + return nil, err + } + + return db, nil }