initialize 初始化
(dbpath string)
| 14 | |
| 15 | // initialize 初始化 |
| 16 | func initialize(dbpath string) (db *hitokotodb, err error) { |
| 17 | if _, err = os.Stat(dbpath); err != nil || os.IsNotExist(err) { |
| 18 | // 生成文件 |
| 19 | f, err := os.Create(dbpath) |
| 20 | if err != nil { |
| 21 | return nil, err |
| 22 | } |
| 23 | _ = f.Close() |
| 24 | } |
| 25 | gdb, err := gorm.Open("sqlite3", dbpath) |
| 26 | if err != nil { |
| 27 | return |
| 28 | } |
| 29 | gdb.AutoMigrate(&hitokoto{}) |
| 30 | return (*hitokotodb)(gdb), nil |
| 31 | } |
| 32 | |
| 33 | type hitokoto struct { |
| 34 | ID int `json:"id" gorm:"column:id;primary_key"` |