Home > Article > Backend Development > golang sqlx catch errors
sqlxThis third-party library is indeed much more fun to use. Here are the learning and usage experiences
Installation:
Use commands (Recommended learning: Go )
go get github.com/jmoiron/sqlx
## Introduction:
The general idea is that sqlx is an extension of golang's standard database/sql. The interface using sqlx is no different from the original interface method, but has the following extensions: 1. Row records can be mapped such as struct (embedded struct Also supported), map and slices <--This is exactly the effect I wanted before 2. Supports the use of named parameters in prepared statements, and adds many extensions to the built-in database/sql package. Simplify the writing of database operation code. 3. Get and Select query results to struct/slice are faster sqlx has also added many interfaces to facilitate developers to use, which will be discussed later.package main import ( "database/sql" _"github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" "log" "fmt" ) type Student struct { Id int `db:"id"` Name string `db:"name"` Nick string `db:"nick"` Country string `db:"country"` Province string `db:"province"` City string `db:"city"` ImgUrl string `db:"img_url"` Status int `db:"status"` CreateTime string `db:"create_time"` } func main() { dns := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8", dbuser, dbpwd, dbhost, dbname) db, err := sqlx.Connect("mysql", dns) if err != nil { log.Fatalln(err) } defer db.Close() tx := db.MustBegin() tx.MustExec(`INSERT INTO student VALUES ('1', 'Jack', 'Jack', 'England', '', '', 'http://img2.imgtn.bdimg.com/it/u=3588772980,2454248748&fm=27&gp=0.jpg', '1', '2018-06-26 17:08:35');`) tx.MustExec(`INSERT INTO student VALUES ('2', 'Emily', 'Emily', 'England', '', '', 'http://img2.imgtn.bdimg.com/it/u=3588772980,2454248748&fm=27&gp=0.jpg', '2', null);`) err = tx.Commit() if err != nil { log.Fatalln(err) } }
The above is the detailed content of golang sqlx catch errors. For more information, please follow other related articles on the PHP Chinese website!