Go-mysql:database/sql Interface Adaptation

Source: Internet
Author: User
Tags dsn format
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}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.