Go connection database, third-party package installation

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

In MySQL, for example, go does not implement a database-connected drive, but provides an interface for third-party implementations

Install third-party packages first

CMD Execute go get command

Before executing the go get command, make sure that your computer is configured with the environment variable gopath and that Git is installed

Perform:

Go get  Github.com/go-sql-driver/mysql

It will then be installed automatically, and after the installation is successful, you can use the

Package Mainimport (       _ "Github.com/go-sql-driver/mysql"        "Database/sql" "       fmt") type Users struct{       Username String       pwd string}func main () {       db,err: = sql. Open ("MySQL", "yld:1226@tcp (127.0.0.1:3306)/operation")       if err! = Nil {              FMT. PRINTLN (Err)              return       }       var users []users = make ([]users,0)       Rows,err: = db. Query ("Select username,pwd from User")       defer rows. Close ()       if err! = Nil {              FMT. PRINTLN (Err)              return       } for       rows. Next () {              var u Users              rows. Scan (&u.username,&u.pwd)              users = append (users,u)       }       fmt. Println (Users)}

I looked at the next row again. Scan () This method, as the official explains.

Copy the columns in the current row to the value pointed to by dest, the number of values in dest must be the same as the number of columns in rows, and scan converts the value type of the column read from the database to the following common go types and the special types provided by the SQL package and any types that implement scanner

    *string//    *[]byte//    *int, *int8, *int16, *int32, *int64//    *uint, *uint8, *uint16, *uint32, *uint64/ /    *bool//    *float32, *float64//    *interface{}//    *rawbytes

In the simplest case, if the type of the value in the source column is an integer, bool, or string type, then scan simply assigns the value through the pointer

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.