官網https://studyiris.com/example/orm/xorm.html例子,稍作修改html
一、我是win64,但沒有遇到mingw問題,應該是以前安裝過gcc環境,參考:測試一下robotgo自動化操做,順便解決了原來的mingw版本中只有gcc,沒有g++的問題git
二、將其中的字段名、字段內容改成中文,並按id訪問數據表中的行,沒遇到亂碼問題,很好。github
代碼以下:sql
//包主顯示如何在您的Web應用程序中使用orm //它只是插入一列並選擇第一列。 package main import ( "time" "github.com/go-xorm/xorm" "github.com/kataras/iris" _ "github.com/mattn/go-sqlite3" ) /* go get -u github.com/mattn/go-sqlite3 go get -u github.com/go-xorm/xorm 若是您使用的是win64而且沒法安裝go-sqlite3: 1.下載:https://sourceforge.net/projects/mingw-w64/files/latest/download 2.選擇「x86_x64」和「posix」 3.添加C:\Program Files\mingw-w64\x86_64-7.1.0-posix-seh-rt_v5-rev1\mingw64\bin 到你的PATH env變量。 手冊: http://xorm.io/docs/ */ //User是咱們的用戶表結構。 type User struct { ID int64 // xorm默認自動遞增 Version string `xorm:"varchar(200)"` Salt string A用戶名 string Password string `xorm:"varchar(200)"` Languages string `xorm:"varchar(200)"` CreatedAt time.Time `xorm:"created"` UpdatedAt time.Time `xorm:"updated"` } func main() { app := iris.New() orm, err := xorm.NewEngine("sqlite3", "./test.db") if err != nil { app.Logger().Fatalf("orm failed to initialized: %v", err) } iris.RegisterOnInterrupt(func() { orm.Close() }) err = orm.Sync2(new(User)) if err != nil { app.Logger().Fatalf("orm failed to initialized User table: %v", err) } app.Get("/insert", func(ctx iris.Context) { user := &User{A用戶名: "大大", Salt: "hash---", Password: "hashed", CreatedAt: time.Now(), UpdatedAt: time.Now()} orm.Insert(user) ctx.Writef("user inserted: %#v", user) }) app.Get("/get/{id:int}", func(ctx iris.Context) { id, _ := ctx.Params().GetInt("id") //int到int64 id64 := int64(id) ctx.Writef("id is %#v", id64) user := User{ID: id64} if ok, _ := orm.Get(&user); ok { ctx.Writef("user found: %#v", user) } }) app.Get("/delete", func(ctx iris.Context) { user := User{ID: 1} orm.Delete(user) ctx.Writef("user delete: %#v", user) }) app.Get("/update", func(ctx iris.Context) { user := User{ID: 2, A用戶名: "小小"} orm.Update(user) ctx.Writef("user update: %#v", user) }) // http://localhost:8080/insert // http://localhost:8080/get/數字 app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed)) }
增:先訪問2次:http://localhost:8080/insert數據庫
查:http://localhost:8080/get/1 和 http://localhost:8080/get/2app
刪:http://localhost:8080/delete工具
改:http://localhost:8080/updatepost
補充:推薦使用SQLiteStudio.exe(https://sqlitestudio.pl/)查看數據庫結構。測試
另外,可參考 Golang xorm工具,根據數據庫自動生成 go 代碼url
http://www.javashuo.com/article/p-kztpovbg-bg.html
http://gobook.io/read/github.com/go-xorm/manual-zh-CN/
https://www.kancloud.cn/xormplus/xorm/167077