I'm working in a project and using "database/sql" package in go. And I want to use struct "DB" that declare in package "database/sql" as an argument to my func, so I can use the return value by sql.Open() and as my func's argument. Was it possible? Codes are below:
package main
import (
"database/sql"
"fmt"
_ "github.com/Go-SQL-Driver/MySQL"
)
func main() {
var table string = "tablename"
db, err := sql.Open("mysql", "user:password@/dbname")
// read data from database
read(db, table)
}
func read(db *DB, table string) {
// read
}
This code throws a "undefined: DB" error.
You must use a qualifier for imported entities - the package name from where the 'name' comes from:
func read(db *sql.DB, table string)
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