V
V
v- death2015-08-17 13:44:29
go
v- death, 2015-08-17 13:44:29

How to connect to the database correctly?

Hello. I can not figure out how to connect to the database 1 time and then pass the connection identifier to the function.
Here is the connection

db, err := gorm.Open("mysql", "root:[email protected]/generais?charset=utf8&parseTime=True&loc=Local")
  if err != nil {
    log.Fatal(err)
  }
  db.DB()
  db.DB().Ping()
  db.DB().SetMaxIdleConns(10)
  db.DB().SetMaxOpenConns(100)
  db.SingularTable(true)

An elementary example if possible. Thanks in advance.
(now I make a new connection in each function)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dev2expert, 2015-08-17
@vGrabko99

An elementary example if possible.

Declare a global variable for the database.
It is described here in general terms.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question