我的目标是使用结构来包装 sql.DB 来做一些比 sql.DB 更重要的事情。我创建的实例无法工作并在我运行时出现此错误。DbConn.db.prepare undefined (cannot refer to unexported field or method sql.(*DB)."".prepare)我的代码是: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}我正在尝试创建一个实例并调用 sql.DB 方法。var dbConn DatabaseConndbConn.Open()defer dbConn.Close()dbQuery := fmt.Sprintf("SELECT *, FROM ms_node WHERE node_id = ?")getNodeRecord, err := dbConn.db.prepare(dbQuery)错误信息指向此处。./server.go:343: dbConn.db.prepare undefined (cannot refer to unexported field or method sql.(*DB)."".prepare)(个人背景:我是新手2周)
- 1 回答
- 0 关注
- 180 浏览
添加回答
举报
0/150
提交
取消