This is a creation in Article, where the information may have evolved or changed.
Build a simple test table
CREATE TABLE ' Test ' (' a ' int (one) not null auto_increment, ' b ' int (one) DEFAULT NULL, PRIMARY KEY (' a ')) Engine=innodb a uto_increment=157
Package Mainimport ("Database/sql" "Fmt" _ "Github.com/go-sql-driver/mysql") func main () {db, err: = SQL. Open ("MySQL", "Root: @tcp (127.0.0.1:3306)/test?charset=utf8") if err! = Nil {panic (err. Error ())}defer db. Close () rows, _: = Fetchrows (db, "SELECT * from Test") for _, V: = Range *rows {fmt. Println (v["A"], v["B"])}fmt. PRINTLN (Insert (DB, "INSERT into Test (b) VALUES (?)", 1)) row, _: = Fetchrow (db, "select * from Test where a =?", 1) fmt. Println (*row)}//inserts a func insert (db *sql. DB, sqlstr string, args ... interface{}) (Int64, error) {stmtins, err: = db. Prepare (SQLSTR) if err! = Nil {panic (err. Error ())}defer stmtins.close () result, err: = Stmtins.exec (args ...) If err! = Nil {panic (err. Error ())}return result. Lastinsertid ()}//modifies and removes the Func exec (db *sql. DB, sqlstr string, args ... interface{}) (Int64, error) {stmtins, err: = db. Prepare (SQLSTR) if err! = Nil {panic (err. Error ())}defer stmtins.close () result, err: = Stmtins.exec (args ...) If err! = Nil {panic (err. Error ())}return result. Rowsaffected ()}//take a row of data, note that suchThe result of the extraction is the string func fetchrow (db *sql. DB, sqlstr string, args ... interface{}) (*map[string]string, error) {stmtout, err: = db. Prepare (SQLSTR) if err! = Nil {panic (err. Error ())}defer stmtout.close () rows, err: = Stmtout.query (args ...) If err! = Nil {panic (err. Error ())}columns, err: = rows. Columns () if err! = Nil {panic (err. Error ())}values: = Make ([]sql. Rawbytes, Len (columns)) Scanargs: = Make ([]interface{}, Len (values)) RET: = Do (map[string]string, Len (Scanargs)) for I: = Range values {scanargs[i] = &values[i]}for rows. Next () {err = rows. Scan (Scanargs ...) If err! = Nil {panic (err. Error ())}var value stringfor I, col: = range values {if Col = = Nil {value = "NULL"} else {value = string (col)}ret[columns[ I]] = Value}break//get the first row Only}return &ret, nil}//take multiple lines, <span style= "font-family:arial, Helvetica, sans- serif; " > Note that this type of result is a string </span>func fetchrows (db *sql. DB, sqlstr string, args ... interface{}) (*[]map[string]string, error) {stmtout, err: = db. Prepare (SQLSTR) if Err! = Nil {panic (err. Error ())}defer stmtout.close () rows, err: = Stmtout.query (args ...) If err! = Nil {panic (err. Error ())}columns, err: = rows. Columns () if err! = Nil {panic (err. Error ())}values: = Make ([]sql. Rawbytes, Len (columns)) Scanargs: = Make ([]interface{}, Len (values)) RET: = Do ([]map[string]string, 0) for I: = Range Valu es {Scanargs[i] = &values[i]}for rows. Next () {err = rows. Scan (Scanargs ...) If err! = Nil {panic (err. Error ())}var value Stringvmap: = Make (Map[string]string, Len (Scanargs)) for I, col: = range values {if Col = = Nil {value = "NULL"} else {value = string (col)}vmap[columns[i]] = Value}ret = Append (ret, vmap)}return &ret, nil}