Golang build error: cannot assign *sqlx.DB to *sql.DB

When I try to build my project, it says:

./main.go:140: cannot assign *sqlx.DB to db.Conn (type *sql.DB) in multiple assignment

My imports in main.go:

import (
    "html/template"
    "io/ioutil"
    "net/http"
    "regexp"
    "./network"
    "log"
    "./config"
    "./db"
    "fmt"
    "github.com/jmoiron/sqlx"
)

My db.go is:

package db
import "database/sql"
import _ "github.com/go-sql-driver/mysql"

var Conn *sql.DB
var Err error

And 140th line from main.go:

db.Conn, db.Err = sqlx.Open(config.Db.Type, config.Db.User+":"+config.Db.Pass+"@/"+config.Db.Name)

Thanks for your help.

Upvotes: 1

Views: 2757

Answers (1)

Tinwor
Tinwor

Reputation: 7973

sqlx.Open return a sqlx.DB struct (here the definition) that is different from the DB struct definition inside the package databqase/sql.
Change the Conn type to sqlx.DB and it should work.

Upvotes: 2

Related Questions