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
Closed

Comments

@hiromaily
Copy link

@hiromaily hiromaily commented May 2, 2016

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 dominikh commented May 2, 2016

Duplicate of #15502.

@hiromaily
Copy link
Author

@hiromaily hiromaily commented May 2, 2016

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

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

@hiromaily 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 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

@hiromaily hiromaily commented May 2, 2016

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
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.