Golang/gin: How to Pass db to router functions

Viewed 1367

I'm using gin framework. And I'm opening the sqlite database in the main function like this

func main() {
...
db, err := sql.Open("sqlite3", "./libreread.db")
CheckError(err)
defer db.Close()
...
}

And I have these router handlers in the main function.

...
r.GET("/", GetHomePage)
r.GET("/signin", GetSignIn)
r.POST("/signin", PostSignIn)
...

How to pass that db value through the router handler func PostSignin(c *gin.Context) ?

So that I could avoid opening and closing the database each time in the functions.

UPDATE: I'm using go-sqlite3 package.

Thanks!

1 Answers
Related