package pgDB import ( "fmt" "github.com/jmoiron/sqlx" _ "github.com/lib/pq" ) func Conn( host, user, password, dbname string, port int, sslmode bool, ) (*sqlx.DB, error) { var ssl string if sslmode { ssl = "enable" } else { ssl = "disable" } dataSourceName := fmt.Sprintf( "host=%s port=%d user=%s password=%s dbname=%s sslmode=%s", host, port, user, password, dbname, ssl, ) conn, errOpen := sqlx.Connect("postgres", dataSourceName) if errOpen != nil { return nil, errOpen } if err := conn.Ping(); err != nil { conn.Close() return nil, err } return conn, nil }