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
                Geographical Operations System, mapping and database software for telecommunications companies.
Tx, which represents a transaction. In addition to Commit and Rollback methods representing transaction-specific semantics, sql. Tx has all of the methods you use to perform common database operations.
the return datatype should be *sql.DB 
changingfunc opendb() (*DB) {
to
func opendb() (*sql.DB) {
should work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With