Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 835
xsuii Avatar asked Aug 15 '13 15:08

xsuii


1 Answers

You must use a qualifier for imported entities - the package name from where the 'name' comes from:

func read(db *sql.DB, table string)
like image 159
zzzz Avatar answered Sep 20 '22 20:09

zzzz