This is a creation in Article, where the information may have evolved or changed.
Go-mysql has supported the Golang Database/sql interface and passed the Https://github.com/bradfitz/go-sql-test test case.
Now Go-mysql can be used directly through the Golang SQL interface, as follows:
import _ "github.com/siddontang/go-mysql/mysql"import "database/sql"
For subsequent use, you can refer directly to the relevant Golang SQL tutorials, such as this.
The compatibility of the Golang SQL interface is primarily in driver.go files,
The DSN format supported by Go-mysql is:
<username>:<password>@
Because in the process of realizing go-mysql, I have consciously designed some interfaces to fit with Database/sql. In addition to the fit of the rows interface, I always feel that it is inconvenient to use, but it is also convenient to fit rows with resultset. Because ResultSet stores all the data after query, we can simulate the rows interface with an int type iterator:
func (r *mysqlRows) Close() error { r.iter = -1 return nil}func (r *mysqlRows) Next(dest []driver.Value) error { if r.iter >= r.r.RowNumber() { return io.EOF } data := r.r.Data[r.iter] for i := range data { //set data[i] to dest[i] } r.iter++ return nil}