简体   繁体   中英

how do I use my import package's struct as a type in go

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.

您必须对导入的实体使用限定符 - 来自'name'的包名称:

func read(db *sql.DB, table string)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM