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