Golang中的db事务

Go uses sql.DB for autocommit, and sql.Tx for manual commit. It's not a problem for a one off use. The problem is that I have a framework which uses sql.DB to do the DB work, now I want some of them to join my new transaction, and it seems to be not that easy without modifying the existing framework to accept sql.Tx. I am wondering if there is really not an easy way to do the auto/manual switch in Go?

Without knowing more about which framework you are using, I don't think there is a way to do it without modifying the framework. You should really try to get the modifications you do included in the framework because the main problem here is that the framework you are using is designed poorly. When writing for a new language (specially a library or framework) you should get to know the conventions and design your software accordingly.

In go, it is not to hard to accomplish this functionality you just have to declared the Queryer (or however you want to call it) interface like this:

type Queryer interface {
    Query(string, ...interface{}) (*sql.Rows, error)
    QueryRow(string, ...interface{}) *sql.Row
    Prepare(string) (*sql.Stmt, error)
    Exec(string, ...interface{}) (sql.Result, error)
}

This interface is implemented implicitly by the sql.DB and the sql.Tx so after declaring it, you just have to modify the functions/methods to accept the Queryer type instead of a sql.DB.