Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

database/sql: func Open(driverName, dataSourceName string) doesn't return error. #15504

Closed
hiromaily opened this issue May 2, 2016 · 5 comments

Comments

@hiromaily
Copy link

Why Open func doesn't return error?

func Open(driverName, dataSourceName string) (*DB, error) {
    driversMu.RLock()
    driveri, ok := drivers[driverName]
    driversMu.RUnlock()
    if !ok {
        return nil, fmt.Errorf("sql: unknown driver %q (forgotten import?)", driverName)
    }
    db := &DB{
        driver:   driveri,
        dsn:      dataSourceName,
        openerCh: make(chan struct{}, connectionRequestQueueSize),
        lastPut:  make(map[*driverConn]string),
    }
    go db.connectionOpener()
    return db, nil
}


Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?
    1.6.2
  2. What operating system and processor architecture are you using (go env)?
    amd64/darwin
  3. What did you do?
    Even though mysql server doesn't start, nothing error occur when I connect server using Open func of sql package.
func connection() (*sql.DB, error) {
    return sql.Open("mysql", getDsn())
}

db, err := connection()
if err != nil {
    panic(err.Error())
}
  1. What did you expect to see?
    I expected connection error.
  2. What did you see instead?
    I changed code to handle error as below.
func connection() (*sql.DB, error) {
    //return sql.Open("mysql", getDsn())
    db, _ := sql.Open("mysql", getDsn())
    return db, db.Ping()
}

@dominikh
Copy link
Member

dominikh commented May 2, 2016

Duplicate of #15502.

@hiromaily
Copy link
Author

I'm sorry. I didn't notice that issue.

@hiromaily hiromaily reopened this May 2, 2016
@bradfitz bradfitz closed this as completed May 2, 2016
@hiromaily
Copy link
Author

hiromaily commented May 2, 2016

I red previous issue #15502.
Though I understood why Open() func doesn't return error but I think interface of Open() func don't have to have return of error. it will mislead people who use this func.

If this func has interface of error, people would expect error. It makes people misleaded.

@dominikh
Copy link
Member

dominikh commented May 2, 2016

  1. The API cannot be changed. It needs to stay backwards compatible.
  2. The function can return an error: when you provide an unknown driver name.

@hiromaily
Copy link
Author

I got it. Thanks.

@golang golang locked and limited conversation to collaborators May 2, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants