企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
~~~ package demo4 //doc : https://github.com/jmoiron/sqlx import ( "github.com/jmoiron/sqlx" "fmt" _ "github.com/go-sql-driver/mysql" "runtime" ) type Article struct { Id int `db:"id"` Title string `db:"title"` Content string `db:"content"` ViewNum int `db:"viewnum"` Time int `db:"time2"` } func Demo4() { host := "localhost" dbname := "messagebox" username := "root" password := "123456" db, err := sqlx.Open("mysql", username+":"+password+"@tcp("+host+")/"+dbname+"?charset=utf8") SimplePanic(err) defer db.Close() article := Article{} err = db.Get(&article, "select id, title, content, viewnum, time as time2 from msg_article order by time desc limit 1") SimplePanic(err) fmt.Println(article) articles := make([]Article, 0) err = db.Select(&articles, "select id, title, content, viewnum, time as time2 from msg_article limit 10") SimplePanic(err) fmt.Println(articles) } func SimplePanic(err error) { if err != nil { _, file, line, _ := runtime.Caller(1) fmt.Println(file, line, err) runtime.Goexit() } } ~~~