From b0f859cc91abc17b8a0873a433705910bbaf722d Mon Sep 17 00:00:00 2001 From: John Date: Tue, 24 Sep 2019 23:41:18 +0800 Subject: [PATCH] improve example codes --- .example/container/garray/array_unique.go | 16 ---- .example/container/garray/basic_array.go | 46 ++++++++++ .example/container/garray/garray1.go | 59 ------------ .../container/garray/garray_slice_safe.go | 17 ---- .../{garray2.go => sorted_array_basic.go} | 0 ...tring_array1.go => sorted_string_array.go} | 25 +++--- .../container/garray/sorted_string_array2.go | 27 ------ .example/container/gmap/{gmap.go => basic.go} | 0 .example/container/gmap/gmap_json.go | 7 -- .../container/gmap/gmap_map_clone_safe.go | 26 ++++++ .example/container/gmap/gmap_map_safe.go | 17 ---- .../gmap/{gmap_order.go => gmap_maps.go} | 6 +- .example/container/gmap/safe_map_delete.go | 39 -------- .example/container/gvar/g.var.go | 4 - .example/database/gdb/mssql/gdb_sqlserver.go | 1 - .example/database/gdb/mysql/gdb_pool.go | 6 +- .example/database/gdb/oracle/gdb.go | 1 - .example/database/gdb/sqlite/sqlite.go | 90 +++++++++---------- .../database/gredis/migration/migration.go | 60 ------------- .example/encoding/{ => gbase64}/gbase64.go | 2 +- .example/encoding/gbinary/binary.go | 21 ++--- .example/encoding/gcfg/gcfg1.go | 2 +- .example/encoding/gcharset/gcharset.go | 2 + .../{ghash.go => ghash/ghash_repeat_check.go} | 0 .example/encoding/gparser/gparser.go | 16 ++-- .example/frame/main/model/config.toml | 4 - .example/frame/main/model/model1.go | 13 --- .example/os/gflock/flock.go | 16 ---- .example/os/gflock/gflock.go | 18 ---- database/gdb/gdb.go | 4 +- database/gdb/gdb_config.go | 33 +++---- frame/gins/gins.go | 4 +- 32 files changed, 177 insertions(+), 405 deletions(-) delete mode 100644 .example/container/garray/array_unique.go create mode 100644 .example/container/garray/basic_array.go delete mode 100644 .example/container/garray/garray1.go delete mode 100644 .example/container/garray/garray_slice_safe.go rename .example/container/garray/{garray2.go => sorted_array_basic.go} (100%) rename .example/container/garray/{sorted_string_array1.go => sorted_string_array.go} (67%) delete mode 100644 .example/container/garray/sorted_string_array2.go rename .example/container/gmap/{gmap.go => basic.go} (100%) create mode 100644 .example/container/gmap/gmap_map_clone_safe.go delete mode 100644 .example/container/gmap/gmap_map_safe.go rename .example/container/gmap/{gmap_order.go => gmap_maps.go} (84%) delete mode 100644 .example/container/gmap/safe_map_delete.go delete mode 100644 .example/database/gredis/migration/migration.go rename .example/encoding/{ => gbase64}/gbase64.go (92%) rename .example/encoding/{ghash.go => ghash/ghash_repeat_check.go} (100%) delete mode 100644 .example/frame/main/model/config.toml delete mode 100644 .example/frame/main/model/model1.go delete mode 100644 .example/os/gflock/flock.go delete mode 100644 .example/os/gflock/gflock.go diff --git a/.example/container/garray/array_unique.go b/.example/container/garray/array_unique.go deleted file mode 100644 index 4fbffb6ba..000000000 --- a/.example/container/garray/array_unique.go +++ /dev/null @@ -1,16 +0,0 @@ -package main - -import "fmt" - -func main() { - array := []uint{1, 1, 2, 3, 3, 4, 4, 5, 5, 6, 6} - for i := 0; i < len(array)-1; i++ { - for j := i + 1; j < len(array); j++ { - if array[i] == array[j] { - array = append(array[:j], array[j+1:]...) - } - } - } - fmt.Println(array) - -} diff --git a/.example/container/garray/basic_array.go b/.example/container/garray/basic_array.go new file mode 100644 index 000000000..7a74ec957 --- /dev/null +++ b/.example/container/garray/basic_array.go @@ -0,0 +1,46 @@ +package main + +import ( + "fmt" + + "github.com/gogf/gf/container/garray" +) + +func main() { + // Create a int array, which is concurrent-unsafe in default. + a := garray.NewIntArray() + + // Appending items. + for i := 0; i < 10; i++ { + a.Append(i) + } + + // Get the length of the array. + fmt.Println(a.Len()) + + // Get the slice of the array. + fmt.Println(a.Slice()) + + // Get the item of specified index. + fmt.Println(a.Get(6)) + + // Insert after/before specified index. + a.InsertAfter(9, 11) + a.InsertBefore(10, 10) + fmt.Println(a.Slice()) + + a.Set(0, 100) + fmt.Println(a.Slice()) + + // Searching the item and returning the index. + fmt.Println(a.Search(5)) + + // Remove item of specified index. + a.Remove(0) + fmt.Println(a.Slice()) + + // Clearing the array. + fmt.Println(a.Slice()) + a.Clear() + fmt.Println(a.Slice()) +} diff --git a/.example/container/garray/garray1.go b/.example/container/garray/garray1.go deleted file mode 100644 index 86eaf6ba3..000000000 --- a/.example/container/garray/garray1.go +++ /dev/null @@ -1,59 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/gogf/gf/container/garray" -) - -func main() { - // 创建普通的int类型数组,并关闭默认的并发安全特性 - a := garray.NewIntArray(true) - - // 添加数据项 - for i := 0; i < 10; i++ { - a.Append(i) - } - - // 获取当前数组长度 - fmt.Println(a.Len()) - - // 获取当前数据项列表 - fmt.Println(a.Slice()) - - // 获取指定索引项 - fmt.Println(a.Get(6)) - - // 在指定索引前插入数据项 - a.InsertAfter(9, 11) - // 在指定索引后插入数据项 - a.InsertBefore(10, 10) - fmt.Println(a.Slice()) - - // 修改指定索引的数据项 - a.Set(0, 100) - fmt.Println(a.Slice()) - - // 搜索数据项,返回搜索到的索引位置 - fmt.Println(a.Search(5)) - - // 删除指定索引的数据项 - a.Remove(0) - fmt.Println(a.Slice()) - - // 并发安全,写锁操作 - a.LockFunc(func(array []int) { - // 将末尾项改为100 - array[len(array)-1] = 100 - }) - - // 并发安全,读锁操作 - a.RLockFunc(func(array []int) { - fmt.Println(array[len(array)-1]) - }) - - // 清空数组 - fmt.Println(a.Slice()) - a.Clear() - fmt.Println(a.Slice()) -} diff --git a/.example/container/garray/garray_slice_safe.go b/.example/container/garray/garray_slice_safe.go deleted file mode 100644 index f9783c331..000000000 --- a/.example/container/garray/garray_slice_safe.go +++ /dev/null @@ -1,17 +0,0 @@ -package main - -import ( - "github.com/gogf/gf/container/garray" - "github.com/gogf/gf/frame/g" -) - -func main() { - a := garray.NewIntArray() - a.Append(1, 2, 3) - - v := a.Slice() - v[0] = 4 - - g.Dump(a.Slice()) - g.Dump(v) -} diff --git a/.example/container/garray/garray2.go b/.example/container/garray/sorted_array_basic.go similarity index 100% rename from .example/container/garray/garray2.go rename to .example/container/garray/sorted_array_basic.go diff --git a/.example/container/garray/sorted_string_array1.go b/.example/container/garray/sorted_string_array.go similarity index 67% rename from .example/container/garray/sorted_string_array1.go rename to .example/container/garray/sorted_string_array.go index 0320922a2..109fe31c5 100644 --- a/.example/container/garray/sorted_string_array1.go +++ b/.example/container/garray/sorted_string_array.go @@ -1,20 +1,23 @@ package main import ( + "fmt" + "github.com/gogf/gf/container/garray" - "github.com/gogf/gf/frame/g" ) func main() { - array := garray.NewSortedStringArray() - array.Add("1") - array.Add("2") - array.Add("3") - array.Add("4") - array.Add("5") - array.Add("6") - array.Add("7") - array.Add("8") + array := garray.NewSortedStrArray() array.Add("9") - g.Dump(array.Slice()) + array.Add("8") + array.Add("7") + array.Add("6") + array.Add("5") + array.Add("4") + array.Add("3") + array.Add("2") + array.Add("1") + fmt.Println(array.Slice()) + // output: + // [1 2 3 4 5 6 7 8 9] } diff --git a/.example/container/garray/sorted_string_array2.go b/.example/container/garray/sorted_string_array2.go deleted file mode 100644 index de1f0ab33..000000000 --- a/.example/container/garray/sorted_string_array2.go +++ /dev/null @@ -1,27 +0,0 @@ -package main - -import ( - "fmt" - "strings" - - "github.com/gogf/gf/container/garray" - "github.com/gogf/gf/frame/g" -) - -func main() { - array := garray.NewSortedStringArray() - array.Add("/api/ctl/show") - array.Add("/api/ctl/post") - array.Add("/api/obj/rest") - array.Add("/api/handler") - array.Add("/api/obj/delete") - array.Add("/api/obj/show") - array.Add("/api/obj/my-show") - array.Add("/api/*") - array.Add("/api/ctl/rest") - array.Add("/api/ctl/my-show") - g.Dump(array.Slice()) - - fmt.Println(strings.Compare("/api/ctl/post", "/api/*")) - fmt.Println(strings.Compare("/api/*", "/api/ctl/my-show")) -} diff --git a/.example/container/gmap/gmap.go b/.example/container/gmap/basic.go similarity index 100% rename from .example/container/gmap/gmap.go rename to .example/container/gmap/basic.go diff --git a/.example/container/gmap/gmap_json.go b/.example/container/gmap/gmap_json.go index 51d147a5a..f1fc4a448 100644 --- a/.example/container/gmap/gmap_json.go +++ b/.example/container/gmap/gmap_json.go @@ -8,13 +8,6 @@ import ( ) func main() { - //m := gmap.New() - //m.Set("k", "v") - //m.Set("1", "2") - //b, err := json.Marshal(m) - //fmt.Println(err) - //fmt.Println(string(b)) - m := gmap.NewIntIntMap() m.Set(1, 2) m.Set(3, 4) diff --git a/.example/container/gmap/gmap_map_clone_safe.go b/.example/container/gmap/gmap_map_clone_safe.go new file mode 100644 index 000000000..46bd1051f --- /dev/null +++ b/.example/container/gmap/gmap_map_clone_safe.go @@ -0,0 +1,26 @@ +package main + +import ( + "github.com/gogf/gf/container/gmap" + "github.com/gogf/gf/frame/g" +) + +func main() { + m1 := gmap.New() + m1.Set("1", "1") + + m2 := m1.Map() + m2["2"] = "2" + + g.Dump(m1.Clone()) + g.Dump(m2) + //output: + //{ + // "1": "1" + //} + // + //{ + // "1": "1", + // "2": "2" + //} +} diff --git a/.example/container/gmap/gmap_map_safe.go b/.example/container/gmap/gmap_map_safe.go deleted file mode 100644 index 735416a43..000000000 --- a/.example/container/gmap/gmap_map_safe.go +++ /dev/null @@ -1,17 +0,0 @@ -package main - -import ( - "github.com/gogf/gf/container/gmap" - "github.com/gogf/gf/frame/g" -) - -func main() { - m := gmap.New() - m.Set("1", "1") - - m1 := m.Map() - m1["2"] = "2" - - g.Dump(m.Clone()) - g.Dump(m1) -} diff --git a/.example/container/gmap/gmap_order.go b/.example/container/gmap/gmap_maps.go similarity index 84% rename from .example/container/gmap/gmap_order.go rename to .example/container/gmap/gmap_maps.go index a3bf7e958..9054ac70c 100644 --- a/.example/container/gmap/gmap_order.go +++ b/.example/container/gmap/gmap_maps.go @@ -10,9 +10,9 @@ import ( func main() { array := g.Slice{2, 3, 1, 5, 4, 6, 8, 7, 9} - hashMap := gmap.New(true) - linkMap := gmap.NewLinkMap(true) - treeMap := gmap.NewTreeMap(gutil.ComparatorInt, true) + hashMap := gmap.New() + linkMap := gmap.NewListMap() + treeMap := gmap.NewTreeMap(gutil.ComparatorInt) for _, v := range array { hashMap.Set(v, v) } diff --git a/.example/container/gmap/safe_map_delete.go b/.example/container/gmap/safe_map_delete.go deleted file mode 100644 index 5d63df6b9..000000000 --- a/.example/container/gmap/safe_map_delete.go +++ /dev/null @@ -1,39 +0,0 @@ -package main - -import ( - "fmt" - "sync" - "time" -) - -// 验证 map 的delete方法是否并发安全 -func main() { - // 创建一个初始化的map - m := make(map[int]int) - for i := 0; i < 10000; i++ { - m[i] = i - } - - fmt.Println("map size:", len(m)) - - wg := sync.WaitGroup{} - ev := make(chan struct{}, 0) - - // 创建10个并发的goroutine,使用ev控制并发开始事件,更容易模拟data race - for i := 0; i < 10; i++ { - wg.Add(1) - go func() { - <-ev - fmt.Println("start") - for i := 0; i < 10000; i++ { - delete(m, i) - } - wg.Done() - }() - } - - time.Sleep(time.Second) - - close(ev) - wg.Wait() -} diff --git a/.example/container/gvar/g.var.go b/.example/container/gvar/g.var.go index d9d40029b..fe688e674 100644 --- a/.example/container/gvar/g.var.go +++ b/.example/container/gvar/g.var.go @@ -30,8 +30,4 @@ func main() { s := new(Score) v.Struct(s) fmt.Println(s) - - // 只读接口 - r := v.ReadOnly() - fmt.Println(r.String()) } diff --git a/.example/database/gdb/mssql/gdb_sqlserver.go b/.example/database/gdb/mssql/gdb_sqlserver.go index ba73d7990..fb3ffdcf3 100644 --- a/.example/database/gdb/mssql/gdb_sqlserver.go +++ b/.example/database/gdb/mssql/gdb_sqlserver.go @@ -519,7 +519,6 @@ func getQueriedSqls() { fmt.Println("Sql :", v.Sql) fmt.Println("Args :", v.Args) fmt.Println("Error:", v.Error) - fmt.Println("Func :", v.Func) } } diff --git a/.example/database/gdb/mysql/gdb_pool.go b/.example/database/gdb/mysql/gdb_pool.go index aadc56960..2ccbe4b97 100644 --- a/.example/database/gdb/mysql/gdb_pool.go +++ b/.example/database/gdb/mysql/gdb_pool.go @@ -8,9 +8,9 @@ import ( func main() { db := g.DB() - db.SetMaxIdleConns(10) - db.SetMaxOpenConns(10) - db.SetConnMaxLifetime(10) + db.SetMaxIdleConnCount(10) + db.SetMaxOpenConnCount(10) + db.SetMaxConnLifetime(time.Minute) // 开启调试模式,以便于记录所有执行的SQL db.SetDebug(true) diff --git a/.example/database/gdb/oracle/gdb.go b/.example/database/gdb/oracle/gdb.go index c574ef55b..bf2020868 100644 --- a/.example/database/gdb/oracle/gdb.go +++ b/.example/database/gdb/oracle/gdb.go @@ -520,7 +520,6 @@ func getQueriedSqls() { fmt.Println("Sql :", v.Sql) fmt.Println("Args :", v.Args) fmt.Println("Error:", v.Error) - fmt.Println("Func :", v.Func) } } diff --git a/.example/database/gdb/sqlite/sqlite.go b/.example/database/gdb/sqlite/sqlite.go index 5d3f2c0d1..6224914ee 100644 --- a/.example/database/gdb/sqlite/sqlite.go +++ b/.example/database/gdb/sqlite/sqlite.go @@ -1,47 +1,47 @@ package main -//import ( -// _ "github.com/mattn/go-sqlite3" -// "github.com/gogf/gf/database/gdb" -// "github.com/gogf/gf/frame/g" -// "fmt" -//) -// -//func main() { -// gdb.SetConfig(gdb.Config{ -// "default": gdb.ConfigGroup{ -// gdb.ConfigNode{ -// Name: "/tmp/my.db", -// Type: "sqlite", -// }, -// }, -// }) -// db := g.Database() -// if db == nil { -// panic("db create failed") -// } -// defer db.Close() -// -// // 创建表 -// sql := `CREATE TABLE user ( -// uid INT PRIMARY KEY NOT NULL, -// name VARCHAR(30) NOT NULL -// );` -// if _, err := db.Exec(sql); err != nil { -// fmt.Println(err) -// } -// -// // 写入数据 -// result, err := db.Table("user").Data(g.Map{"uid" : 1, "name" : "john"}).Save() -// if err == nil { -// fmt.Println(result.RowsAffected()) -// } else { -// fmt.Println(err) -// } -// -// // 删除表 -// sql = `DROP TABLE user;` -// if _, err := db.Exec(sql); err != nil { -// fmt.Println(err) -// } -//} +import ( + "fmt" + + "github.com/gogf/gf/database/gdb" + "github.com/gogf/gf/frame/g" + _ "github.com/mattn/go-sqlite3" +) + +func main() { + gdb.SetConfig(gdb.Config{ + "default": gdb.ConfigGroup{ + gdb.ConfigNode{ + Name: "/tmp/my.db", + Type: "sqlite", + }, + }, + }) + db := g.DB() + if db == nil { + panic("db create failed") + } + + // 创建表 + sql := `CREATE TABLE user ( + uid INT PRIMARY KEY NOT NULL, + name VARCHAR(30) NOT NULL + );` + if _, err := db.Exec(sql); err != nil { + fmt.Println(err) + } + + // 写入数据 + result, err := db.Table("user").Data(g.Map{"uid": 1, "name": "john"}).Save() + if err == nil { + fmt.Println(result.RowsAffected()) + } else { + fmt.Println(err) + } + + // 删除表 + sql = `DROP TABLE user;` + if _, err := db.Exec(sql); err != nil { + fmt.Println(err) + } +} diff --git a/.example/database/gredis/migration/migration.go b/.example/database/gredis/migration/migration.go deleted file mode 100644 index ac9c0ab17..000000000 --- a/.example/database/gredis/migration/migration.go +++ /dev/null @@ -1,60 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/gogf/gf/util/gconv" - - "github.com/gogf/gf/os/gcmd" - - "github.com/gogf/gf/container/garray" - "github.com/gogf/gf/database/gredis" - "github.com/gogf/gf/frame/g" -) - -func main() { - batchNumber := 1000 - redis1Config, err := gredis.ConfigFromStr("im-redis-slave:6379,9") - if err != nil { - panic(err) - } - redis2Config, err := gredis.ConfigFromStr("r-bp1f0a5d4efd8744.redis.rds.aliyuncs.com:6379,9") - if err != nil { - panic(err) - } - gredis.SetConfig(redis1Config) - - v, err := g.Redis().DoVar("keys", "*") - if err != nil { - panic(err) - } - array := garray.NewStrArrayFrom(v.Strings()) - for { - slice := array.PopLefts(batchNumber) - if len(slice) > 0 { - // `migrate %s %d "" 0 2000 copy replace auth %s keys %s`, - params := g.Slice{ - redis2Config.Host, - redis2Config.Port, - "", - redis2Config.Db, - 2000, - "copy", - "replace", - "keys", - } - params = append(params, gconv.Interfaces(slice)...) - fmt.Println(params) - if gcmd.GetOpt("dryrun") == "0" { - if v, err := g.Redis().DoVar("migrate", params...); err != nil { - panic(err) - } else { - fmt.Println(v.String()) - } - } - } else { - break - } - } - fmt.Println("done") -} diff --git a/.example/encoding/gbase64.go b/.example/encoding/gbase64/gbase64.go similarity index 92% rename from .example/encoding/gbase64.go rename to .example/encoding/gbase64/gbase64.go index f9ae0c13c..ecf665407 100644 --- a/.example/encoding/gbase64.go +++ b/.example/encoding/gbase64/gbase64.go @@ -1,4 +1,4 @@ -package main +package gbase64 import ( "fmt" diff --git a/.example/encoding/gbinary/binary.go b/.example/encoding/gbinary/binary.go index cb275d401..de2f5e1ec 100644 --- a/.example/encoding/gbinary/binary.go +++ b/.example/encoding/gbinary/binary.go @@ -9,26 +9,19 @@ import ( func main() { // 使用gbinary.Encoded对基本数据类型进行二进制打包 - if buffer, err := gbinary.Encode(18, 300, 1.01); err != nil { - glog.Error(err) - } else { - fmt.Println(buffer) - } + fmt.Println(gbinary.Encode(18, 300, 1.01)) // 使用gbinary.Decode对整形二进制解包,注意第二个及其后参数为字长确定的整形变量的指针地址,字长确定的类型, // 例如:int8/16/32/64、uint8/16/32/64、float32/64 // 这里的1.01默认为float64类型(64位系统下) - if buffer, err := gbinary.Encode(18, 300, 1.01); err != nil { + buffer := gbinary.Encode(18, 300, 1.01) + var i1 int8 + var i2 int16 + var f3 float64 + if err := gbinary.Decode(buffer, &i1, &i2, &f3); err != nil { glog.Error(err) } else { - var i1 int8 - var i2 int16 - var f3 float64 - if err := gbinary.Decode(buffer, &i1, &i2, &f3); err != nil { - glog.Error(err) - } else { - fmt.Println(i1, i2, f3) - } + fmt.Println(i1, i2, f3) } // 编码/解析 int,自动识别变量长度 diff --git a/.example/encoding/gcfg/gcfg1.go b/.example/encoding/gcfg/gcfg1.go index bbd2e67a5..a9414223e 100644 --- a/.example/encoding/gcfg/gcfg1.go +++ b/.example/encoding/gcfg/gcfg1.go @@ -15,6 +15,6 @@ func main() { } redisCfg := new(RedisConfig) - fmt.Println(g.Config().GetToStruct("redis", redisCfg)) + fmt.Println(g.Config().GetStruct("redis", redisCfg)) fmt.Println(redisCfg) } diff --git a/.example/encoding/gcharset/gcharset.go b/.example/encoding/gcharset/gcharset.go index 06dcbedf8..07d2d37ec 100644 --- a/.example/encoding/gcharset/gcharset.go +++ b/.example/encoding/gcharset/gcharset.go @@ -15,4 +15,6 @@ func main() { panic(err) } fmt.Println(str) + // output: + // 花间一壶酒,独酌无相亲。 } diff --git a/.example/encoding/ghash.go b/.example/encoding/ghash/ghash_repeat_check.go similarity index 100% rename from .example/encoding/ghash.go rename to .example/encoding/ghash/ghash_repeat_check.go diff --git a/.example/encoding/gparser/gparser.go b/.example/encoding/gparser/gparser.go index f58039fd8..df42f1ffd 100644 --- a/.example/encoding/gparser/gparser.go +++ b/.example/encoding/gparser/gparser.go @@ -19,7 +19,7 @@ func getWithPattern1() { } }` - if p, e := gparser.LoadContent([]byte(data), "json"); e != nil { + if p, e := gparser.LoadContent([]byte(data)); e != nil { glog.Error(e) } else { fmt.Println("John Score:", p.GetFloat32("users.list.1.score")) @@ -36,7 +36,7 @@ func getWithPattern2() { Don't forget me this weekend! ` - if p, e := gparser.LoadContent([]byte(data), "xml"); e != nil { + if p, e := gparser.LoadContent([]byte(data)); e != nil { glog.Error(e) } else { fmt.Println("Heading:", p.GetString("note.heading")) @@ -52,7 +52,7 @@ func multiDots1() { }, "users.count" : 101 }` - if p, e := gparser.LoadContent([]byte(data), "json"); e != nil { + if p, e := gparser.LoadContent([]byte(data)); e != nil { glog.Error(e) } else { fmt.Println("Users Count:", p.Get("users.count")) @@ -70,7 +70,7 @@ func multiDots2() { "count.type1" : 100 } }` - if p, e := gparser.LoadContent([]byte(data), "json"); e != nil { + if p, e := gparser.LoadContent([]byte(data)); e != nil { glog.Error(e) } else { fmt.Println("Users Count:", p.Get("users.count.type1")) @@ -88,7 +88,7 @@ func set1() { gf article2gf content2 gf article3gf content3 ` - if p, e := gparser.LoadContent([]byte(data), "xml"); e != nil { + if p, e := gparser.LoadContent([]byte(data)); e != nil { glog.Error(e) } else { p.Set("article.list.0", nil) @@ -105,7 +105,7 @@ func set2() { "count" : 100 } }` - if p, e := gparser.LoadContent([]byte(data), "json"); e != nil { + if p, e := gparser.LoadContent([]byte(data)); e != nil { glog.Error(e) } else { p.Set("users.count", 1) @@ -116,7 +116,7 @@ func set2() { } func makeXml1() { - p := gparser.New() + p := gparser.New(nil) p.Set("name", "john") p.Set("age", 18) p.Set("scores", map[string]int{ @@ -133,7 +133,7 @@ func makeJson1() { Id int `json:"id"` Price float32 `json:"price"` } - p := gparser.New() + p := gparser.New(nil) p.Set("orders.list.0", Order{1, 100}) p.Set("orders.list.1", Order{2, 666}) p.Set("orders.list.2", Order{3, 999.99}) diff --git a/.example/frame/main/model/config.toml b/.example/frame/main/model/config.toml deleted file mode 100644 index 931c6287b..000000000 --- a/.example/frame/main/model/config.toml +++ /dev/null @@ -1,4 +0,0 @@ - -# MySQL数据库配置 -[database] - link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test" diff --git a/.example/frame/main/model/model1.go b/.example/frame/main/model/model1.go deleted file mode 100644 index 8b7bf2264..000000000 --- a/.example/frame/main/model/model1.go +++ /dev/null @@ -1,13 +0,0 @@ -package main - -import ( - "github.com/gogf/gf/.example/frame/mvc/model/test" - "github.com/gogf/gf/frame/g" -) - -func main() { - g.DB().SetDebug(true) - user, err := test.ModelUser().One() - g.Dump(err) - g.Dump(user) -} diff --git a/.example/os/gflock/flock.go b/.example/os/gflock/flock.go deleted file mode 100644 index bac993cb9..000000000 --- a/.example/os/gflock/flock.go +++ /dev/null @@ -1,16 +0,0 @@ -package main - -import ( - "fmt" - "time" -) - -func main() { - l := flock.NewFlock("/tmp/go-lock.lock") - l.Lock() - fmt.Printf("lock 1") - l.Lock() - fmt.Printf("lock 1") - - time.Sleep(time.Hour) -} diff --git a/.example/os/gflock/gflock.go b/.example/os/gflock/gflock.go deleted file mode 100644 index 518a59f3b..000000000 --- a/.example/os/gflock/gflock.go +++ /dev/null @@ -1,18 +0,0 @@ -package main - -import ( - "time" - - "github.com/gogf/gf/os/gflock" - "github.com/gogf/gf/os/glog" - "github.com/gogf/gf/os/gproc" -) - -func main() { - l := gflock.New("demo.lock") - l.Lock() - glog.Printf("locked by pid: %d", gproc.Pid()) - time.Sleep(10 * time.Second) - l.UnLock() - glog.Printf("unlocked by pid: %d", gproc.Pid()) -} diff --git a/database/gdb/gdb.go b/database/gdb/gdb.go index 3b8c0f041..74098874f 100644 --- a/database/gdb/gdb.go +++ b/database/gdb/gdb.go @@ -90,7 +90,7 @@ type DB interface { SetLogger(logger *glog.Logger) SetMaxIdleConnCount(n int) SetMaxOpenConnCount(n int) - SetMaxConnLifetime(n int) + SetMaxConnLifetime(d time.Duration) Tables() (tables []string, err error) TableFields(table string) (map[string]*TableField, error) @@ -126,7 +126,7 @@ type dbBase struct { logger *glog.Logger // 日志管理对象 maxIdleConnCount int // 连接池最大限制的连接数 maxOpenConnCount int // 连接池最大打开的连接数 - maxConnLifetime int // (单位秒)连接对象可重复使用的时间长度 + maxConnLifetime time.Duration // 连接对象可重复使用的时间长度 } // 执行的SQL对象 diff --git a/database/gdb/gdb_config.go b/database/gdb/gdb_config.go index 669b0542a..56806f353 100644 --- a/database/gdb/gdb_config.go +++ b/database/gdb/gdb_config.go @@ -9,6 +9,7 @@ package gdb import ( "fmt" "sync" + "time" "github.com/gogf/gf/os/glog" @@ -27,20 +28,20 @@ type ConfigGroup []ConfigNode // 数据库单项配置 type ConfigNode struct { - Host string // 地址 - Port string // 端口 - User string // 账号 - Pass string // 密码 - Name string // 数据库名称 - Type string // 数据库类型:mysql, sqlite, mssql, pgsql, oracle - Role string // (可选,默认为master)数据库的角色,用于主从操作分离,至少需要有一个master,参数值:master, slave - Debug bool // (可选)开启调试模式 - Weight int // (可选)用于负载均衡的权重计算,当集群中只有一个节点时,权重没有任何意义 - Charset string // (可选,默认为 utf8)编码,默认为 utf8 - LinkInfo string // (可选)自定义链接信息,当该字段被设置值时,以上链接字段(Host,Port,User,Pass,Name)将失效(该字段是一个扩展功能) - MaxIdleConnCount int // (可选)连接池最大限制的连接数 - MaxOpenConnCount int // (可选)连接池最大打开的连接数 - MaxConnLifetime int // (可选,单位秒)连接对象可重复使用的时间长度 + Host string // 地址 + Port string // 端口 + User string // 账号 + Pass string // 密码 + Name string // 数据库名称 + Type string // 数据库类型:mysql, sqlite, mssql, pgsql, oracle + Role string // (可选,默认为master)数据库的角色,用于主从操作分离,至少需要有一个master,参数值:master, slave + Debug bool // (可选)开启调试模式 + Weight int // (可选)用于负载均衡的权重计算,当集群中只有一个节点时,权重没有任何意义 + Charset string // (可选,默认为 utf8)编码,默认为 utf8 + LinkInfo string // (可选)自定义链接信息,当该字段被设置值时,以上链接字段(Host,Port,User,Pass,Name)将失效(该字段是一个扩展功能) + MaxIdleConnCount int // (可选)连接池最大限制的连接数 + MaxOpenConnCount int // (可选)连接池最大打开的连接数 + MaxConnLifetime time.Duration // (可选)连接对象可重复使用的时间长度 } // 数据库配置包内对象 @@ -130,8 +131,8 @@ func (bs *dbBase) SetMaxOpenConnCount(n int) { // 设置数据库连接可重复利用的时间,超过该时间则被关闭废弃 // 如果 d <= 0 表示该链接会一直重复利用 -func (bs *dbBase) SetMaxConnLifetime(n int) { - bs.maxConnLifetime = n +func (bs *dbBase) SetMaxConnLifetime(d time.Duration) { + bs.maxConnLifetime = d } // 节点配置转换为字符串 diff --git a/frame/gins/gins.go b/frame/gins/gins.go index 46730411b..a35f4aa61 100644 --- a/frame/gins/gins.go +++ b/frame/gins/gins.go @@ -215,10 +215,10 @@ func parseDBConfigNode(value interface{}) *gdb.ConfigNode { node.MaxOpenConnCount = gconv.Int(value) } if value, ok := nodeMap["max-lifetime"]; ok { - node.MaxConnLifetime = gconv.Int(value) + node.MaxConnLifetime = gconv.Duration(value) } if value, ok := nodeMap["maxLifetime"]; ok { - node.MaxConnLifetime = gconv.Int(value) + node.MaxConnLifetime = gconv.Duration(value) } // Parse link syntax. if node.LinkInfo != "" && node.Type == "" {