Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "database/sql"
- "fmt"
- "log"
- _ "github.com/lib/pq"
- )
- // Set a new db connection
- func (s *Switch) newDBConn() error {
- // open pgsql database
- if s.PGhost == "" {
- s.PGhost = "localhost"
- }
- if s.PGport == "" {
- s.PGport = "5432"
- }
- openQuery := "user=" + s.PGuser +
- " password=" + s.PGpasswd +
- " dbname=" + s.PGdbname +
- " host=" + s.PGhost +
- " port=" + s.PGport
- log.Printf("%v", openQuery)
- db, err := sql.Open("postgres", openQuery)
- // test and open pgsql connection
- err = db.Ping()
- log.Printf("%v", err)
- if err == nil {
- s.DB = db
- } else {
- err = fmt.Errorf("cannot connect to the database err=%v\n", err)
- }
- return err
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement