Reputation: 309
I use Go with PostgreSQL using github.com/lib/pq.
I want to call this opendb()
function in other functions but I'm having problem with the return value.
package database
import (
"fmt"
"database/sql"
_ "github.com/lib/pq"
)
const (
host = "localhost"
port = 5432
user = "postgres"
password = "pgpassword"
dbname = "postgres"
)
func opendb() (*DB) {
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s " +
"sslmode=disable", host, port, user, password, dbname)
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
defer db.Close()
return db
}
If you run this:
$ go run main.go
It will show this error:
error:
undefined: DB
Upvotes: 7
Views: 3553
Reputation: 1701
the return datatype should be *sql.DB
changing
func opendb() (*DB) {
to
func opendb() (*sql.DB) {
should work
Upvotes: 12