This is a creation in Article, where the information may have evolved or changed.
The GO src comes with the SQL package and the package that connects to the database. Here is an example of connecting MySQL.
Sql. Open opens up a database connection. When you execute EXEC or query, you make the connection yourself. So the entire application only needs to initialize the sql.db pointer once. Then use it everywhere and it's OK.
DbTest Project Main.gopackage mainimport ("Database/sql" "Fmt" _ "Github.com/go-sql-driver/mysql" "Time") Func Iserr ( Err error) {if err! = Nil {fmt. Printf ("Cheng Error:%s", err)}}func main () {//Initialize database information, this is a database connection pool db, err: = SQL. Open ("MySQL", "test:123456@tcp (192.168.33.9:3306)/jxy2_s001") Iserr (ERR)//Package SQL instruction stmt, err: = db. Prepare ("update ch_playerequip set level = 1") Iserr (ERR)//execute impact instruction result, err: = stmt. Exec () Iserr (err) fmt. Print (result. Rowsaffected ())//Execute query rows, err: = db. Query ("Select Playerid,id,configid,isdress,level from Ch_playerequip") Iserr (ERR) defer rows. Close () var playerID, Configid, id, leave, isdress intfor rows. Next () {rows. Scan (&playerid, &configid, &id, &leave, &isdress) fmt. Print (playerID, configid, id, leave, isdress)}}