dow98764 2016-03-16 00:02
浏览 150
已采纳

通过结构包装的sql.DB无法调用实例中的方法

My goal is using a struct to wrap sql.DB to do something more than sql.DB. The instance I create can't work and give me this error when I run it.

DbConn.db.prepare undefined (cannot refer to unexported field or method sql.(*DB)."".prepare)

My code is:

type DatabaseConn struct {
  driverName string
  databaseName string
  userName string
  password string
  dataSourceName string
  db *sql.DB
}

func (d DatabaseConn)Open() error {
    d.driverName    = DB_DRNAME
    d.userName      = DB_UNAME
    d.password      = DB_PWD
    d.databaseName  = DB_DBNAME
    d.dataSourceName = fmt.Sprintf("%s:%s@/%s?charset=utf8",d.userName, d.password, d.databaseName)
    db, err := sql.Open(d.driverName, d.dataSourceName)
    return err
}


func (d *DatabaseConn)Close() error {
    defer func() {
        if err := recover(); err != nil {
            fmt.Println("Trying to handle error in DatabaseConn.Close(): ", err)
        }
    }()

    err := d.db.Close()
    return err
}

I am trying to create an instance and invoke sql.DB method.

var dbConn DatabaseConn
dbConn.Open()
defer dbConn.Close()
dbQuery := fmt.Sprintf("SELECT *, FROM ms_node WHERE node_id = ?")
getNodeRecord, err := dbConn.db.prepare(dbQuery)

The error message is pointed here.

./server.go:343: dbConn.db.prepare undefined (cannot refer to unexported field or method sql.(*DB)."".prepare)

(Personal background: I am newbie for 2 weeks)

  • 写回答

1条回答 默认 最新

  • donglu3184 2016-03-16 01:51
    关注

    The function Prepare starts with a capital letter ;)

    So you should have:

    getNodeRecord, err := dbConn.db.Prepare(dbQuery)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页