Update 05.1.md

Typographical errors and improved readability
This commit is contained in:
Jimmy99
2016-04-06 07:48:41 +02:00
parent d3ba612e4a
commit 1f83f48547

View File

@@ -21,13 +21,13 @@ Let's take a look at the corresponding mymysql and sqlite3 driver code:
sql.Register("mymysql", &d)
}
We see that all third-party database drivers have implemented this function to register themselves, and Go uses a map to save user drivers inside of `database/sql`.
We see that all third-party database drivers implement this function to register themselves, and Go uses a map to save user drivers inside of `database/sql`.
var drivers = make(map[string]driver.Driver)
drivers[name] = driver
Therefore, this register function can register drivers as many as you want with different names.
Therefore, this registration function can register as many drivers as you may require, each with different names.
We always see the following code when we use third-party drivers:
@@ -36,7 +36,7 @@ We always see the following code when we use third-party drivers:
_ "github.com/mattn/go-sqlite3"
)
Here the underscore (also known as a 'blank') `_` can be quite confusing for many beginners, but this is a great feature in Go. We already know that this identifier is for discarding values from function returns, and also that you must use all packages that you've imported in your code in Go. So when the blank is used with import, it means that you need to execute the init() function of that package without directly using it, which exactly fits the use-case for registering database drivers.
Here, the underscore (also known as a 'blank') `_` can be quite confusing for many beginners, but this is a great feature in Go. We already know that this underscore identifier is used for discarding values from function returns, and also that you must use all packages that you've imported in your code in Go. So when the blank is used with import, it means that you need to execute the init() function of that package without directly using it, which is a perfect fit for the use-case of registering database drivers.
## driver.Driver
@@ -46,20 +46,20 @@ Here the underscore (also known as a 'blank') `_` can be quite confusing for man
Open(name string) (Conn, error)
}
This is a one-time Conn, which means it can only be used once in one goroutine. The following code will cause errors to occur:
This is a one-time Conn, which means it can only be used once per goroutine. The following code will cause errors to occur:
...
go goroutineA (Conn) // query
go goroutineB (Conn) // insert
...
Because Go has no idea which goroutine does what operation, the query operation may get the result of the insert operation, and vice-versa.
Because Go has no idea which goroutine does which operation, the query operation may get the result of the insert operation, and vice-versa.
All third-party drivers should have this function to parse the name of Conn and return the correct results.
## driver.Conn
This is a database connection interface with some methods, and as i've said above, the same Conn can only be used in one goroutine.
This is a database connection interface with some methods, and as i've said above, the same Conn can only be used once per goroutine.
type Conn interface {
Prepare(query string) (Stmt, error)
@@ -68,12 +68,12 @@ This is a database connection interface with some methods, and as i've said abov
}
- `Prepare` returns the prepare status of corresponding SQL commands for querying and deleting, etc.
- `Close` closes the current connection and cleans resources. Most third-party drivers implement some kind of connection pool, so you don't need to cache connections unless you want to have unexpected errors.
- `Close` closes the current connection and cleans resources. Most third-party drivers implement some kind of connection pool, so you don't need to cache connections which can cause unexpected errors.
- `Begin` returns a Tx that represents a transaction handle. You can use it for querying, updating, rolling back transactions, etc.
## driver.Stmt
This is a ready status that corresponds with Conn, so it can only be used in one goroutine like Conn.
This is a ready status that corresponds with Conn, so it can only be used once per goroutine (as is the case with Conn).
type Stmt interface {
Close() error
@@ -144,11 +144,11 @@ This is an alias of int64, but it implements the Result interface.
## driver.Value
This is an empty interface that can contains any kind of data.
This is an empty interface that can contain any kind of data.
type Value interface{}
The Value must be something that drivers can operate on or nil, so it should be one of following types:
The Value must be something that drivers can operate on or nil, so it should be one of the following types:
int64
float64
@@ -181,7 +181,7 @@ This defines an interface for returning driver.Value.
Many types implement this interface for conversion between driver.Value and itself.
At this point, you should know a bit about developping database drivers in Go. Once you can implement interfaces for operations like add, delete, update, etc., there are only a few problems left related to communicating with specific databases.
At this point, you should know a bit about developing database drivers in Go. Once you can implement interfaces for operations like add, delete, update, etc., there are only a few problems left related to communicating with specific databases.
## database/sql
@@ -195,7 +195,7 @@ database/sql defines even more high-level methods on top of database/sql/driver
closed bool
}
As you can see, the `Open` function returns a DB that has a freeConn, and this is a simple connection pool. Its implementation is very simple and ugly. It uses `defer db.putConn(ci, err)` in the Db.prepare function to put a connection into the connection pool. Everytime you call the Conn function, it checks the length of freeCoon. If it's greater than 0, that means there is a reusable connection and it directly returns to you. Otherwise it creates a new connection and returns.
As you can see, the `Open` function returns a DB that has a freeConn, and this is a simple connection pool. Its implementation is very simple and ugly. It uses `defer db.putConn(ci, err)` in the Db.prepare function to put a connection into the connection pool. Everytime you call the Conn function, it checks the length of freeConn. If it's greater than 0, that means there is a reusable connection and it directly returns to you. Otherwise it creates a new connection and returns.
## Links