duanmianxue2687
2017-08-17 14:20
浏览 185
已采纳

Golang构建错误:无法将* sqlx.DB分配给* 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.

图片转代码服务由CSDN问答提供 功能建议

当我尝试构建自己的项目时,它说:

  ./ main.go:140:无法通过多次分配将* sqlx.DB分配给db.Conn(类型* sql.DB)
   
 
 

我在main中的导入 .go:

  import(
“ html / template” 
“ io / ioutil” 
“ net / http” 
“ regexp” 
“ ./ 网络“ 
”日志“ 
” ./config"
“ ./db"
” fmt“ 
” github.com/jmoiron/sqlx"
)
   
 
 

我的db.go是:

 程序包db 
import“ database / sql” 
import _“ github.com/go-sql-driver/  mysql“ 
 
var Conn * sql.DB 
var Err错误
   
 
 

和main.go的第140行:

  db.Conn,db.Err = sqlx.Open(config.Db.Type,config.Db.User +“:” + config.Db.Pass +“ @ /” + config.Db.Name)
 <  / code>  
 
 

感谢您的帮助。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doujiang2641 2017-08-17 14:30
    已采纳

    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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题