Here is a small package for initializing queries to the postgresql database:

package postgresql // Работа с postgresql import ( "database/sql" _ "errors" "fmt" "log" "sync" _ "github.com/lib/pq" ) var db *sql.DB var Requests dbRequests type dbRequests struct { Rlock *sync.RWMutex RequestsList map[string]*sql.Stmt } func (dbr *dbRequests) initRequests() error { var err error dbr.Rlock = &sync.RWMutex{} dbr.Rlock.Lock() defer dbr.Rlock.Unlock() fff := make(map[string]*sql.Stmt) // dbr.RequestsList = make(map[string]*sql.Stmt) fff["asd"], err = db.Prepare("INSERT INTO \"AvtorizationR\" (\"HashUser\", \"HashRole\", \"HashOrg\") VALUES ($1, $2, $3)") if err != nil { fmt.Println("::::", err) } if err != nil { fmt.Println("sssssss") return err } return nil } func init() { // Подключение к postgresql fmt.Println("----------------------------------------") db, err := sql.Open("postgres", "host=localhost port=5433 user=admin password=Oma524744 dbname=Role sslmode=disable") if err != nil { log.Panic("Postgresql not found!:", err) } if err = db.Ping(); err != nil { log.Panic("Postgresql not reply!:", err) } log.Println("\nPostgresql running!") fmt.Println("::::::::::::::::::1111111111111111111111111111::::::::::::::::::::::::::::::::::::") ss, err := db.Prepare("SELECT * FROM \"AvtorizationR\"") if err != nil { fmt.Println(err) } rows, err := ss.Query() for rows.Next() { fmt.Println("ЕСТЬ МАЗАФАКА") } fff := make(map[string]*sql.Stmt) fff["asd"], err = db.Prepare("INSERT INTO \"AvtorizationR\" (\"HashUser\", \"HashRole\", \"HashOrg\") VALUES ($1, $2, $3)") if err != nil { fmt.Println("asdasdasdadsasdasdasdasddddddddddddddddd::::", err) } //Запуск инициализации запроса if err = Requests.initRequests(); err != nil { fmt.Println("::::::::::::::::::", err, "::::::::::::::::::::::::::::::::::::") } } 

So here the program crashes on prepare exactly if I write it in the function initRequests. A message is issued that I am contacting a non-existent address. What am I doing wrong?

  • Where do you call the init() function, which should create a connection and populate the db variable? Or in golang, calls like something hidden are happening ... - Mike
  • The package itself calls the init function when the package is initialized in the application, in other languages ​​there are also analogues of this function ... - Oma

1 answer 1

After a few hours of thinking, I realized what the problem was, with this call to the Open () function

 db, err := sql.Open("postgres", "host=localhost port=5433 user=admin password=Oma524744 dbname=Role sslmode=disable") 

Only one new err variable is clearly visible. Since db global thought it would just assign a value to it. But as it turned out, he created two new variables in the init () function and destroyed it on exit. In order to solve this problem, it was necessary to slightly change the code:

 var err error db, err = sql.Open("postgres", "host=localhost port=5433 user=admin password=Oma524744 dbname=Role sslmode=disable") 

Thus, no new variables were detected and an assignment operation was applied. All successful future work with golang!