mirror of
https://gitee.com/johng/gf
synced 2026-06-07 10:22:11 +08:00
Compare commits
422 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f8ab71e7f0 | |||
| 9cca3a3ec1 | |||
| 97bcf2a438 | |||
| 85dd2e9f4f | |||
| 23d62da87f | |||
| b4d947fecd | |||
| 650c95af31 | |||
| 943116d495 | |||
| 644df7c16e | |||
| 638773b216 | |||
| 889e7914e2 | |||
| 68cc85f2b2 | |||
| c273ce576b | |||
| f8d57096a8 | |||
| d7542e87ae | |||
| b178210a31 | |||
| 3e6a23b0e1 | |||
| ee8d2afe58 | |||
| 11e102e137 | |||
| e06b62ecf2 | |||
| d178102f82 | |||
| e1dd5cce7d | |||
| 1edc1f35fb | |||
| 4df47be521 | |||
| 9cb88bca5a | |||
| fa8cc1d3f4 | |||
| 9ae8a7ca33 | |||
| f4da179140 | |||
| 13330658cb | |||
| 9f04e46166 | |||
| 1072ea3fb0 | |||
| ea9e8055a4 | |||
| 784abf2a30 | |||
| ed4a70deff | |||
| fef20d10a2 | |||
| 176dcdc7cc | |||
| 12ed05f846 | |||
| 5999f22f76 | |||
| c056fd2a06 | |||
| cb422f043e | |||
| 4ae89dc9f6 | |||
| 4f6f07db1d | |||
| cd981c7294 | |||
| 557d2967fa | |||
| a7a70636dd | |||
| 2215661f89 | |||
| a22b590b43 | |||
| 1b0b209662 | |||
| 2a2761c54f | |||
| 1c83d72f39 | |||
| fcea774b59 | |||
| da2ec21571 | |||
| ebb8d8a2f7 | |||
| 9706a9c768 | |||
| cee67a8d4e | |||
| 87650557fd | |||
| d3bf52f12f | |||
| 6b13a4849b | |||
| 8e380c0d9d | |||
| 0caf4bfcec | |||
| 9c3b978b50 | |||
| ab689a7792 | |||
| 846646d92d | |||
| 2eb2b89432 | |||
| 43441a8218 | |||
| 561a541fa1 | |||
| ffe9ecc141 | |||
| 77f7884604 | |||
| 0a203d1e22 | |||
| f4f4550483 | |||
| e87226a092 | |||
| 391a3ec9bd | |||
| dd5cd31ef5 | |||
| de92e804fe | |||
| 7725d9aaaf | |||
| bd3e25adea | |||
| 0b1d49af4b | |||
| 7efa9e351e | |||
| 0b0141954b | |||
| 82b531fbfb | |||
| 67fb626339 | |||
| 9044d5f05d | |||
| 47663aa1f1 | |||
| 63c0aab19c | |||
| 261216f5e4 | |||
| f88b799d67 | |||
| 9c48dd172c | |||
| 09ce105eee | |||
| 651aa895f8 | |||
| 0509e41198 | |||
| 1b40d6a53a | |||
| f9189d48d1 | |||
| 849874a247 | |||
| 3f6510bae7 | |||
| a585a26c39 | |||
| 9943966a86 | |||
| 3617e51c01 | |||
| c931032f08 | |||
| 37b286eaa4 | |||
| 619287c273 | |||
| aeb9b68298 | |||
| bae8f6315b | |||
| a6f70f8935 | |||
| acca6f4009 | |||
| 727fdd2391 | |||
| 1d174e00c0 | |||
| a5e3e2f5ba | |||
| da43c2d52f | |||
| 262f27748c | |||
| a29aef7e1c | |||
| 1671391195 | |||
| 28b0d59c61 | |||
| 86433cef25 | |||
| e96ccd5f71 | |||
| 50a087bb3d | |||
| 1ec049c52f | |||
| ec38805542 | |||
| 4c8517d075 | |||
| edf06da6ea | |||
| eb43a2040e | |||
| 9d0ecc7d3e | |||
| ad943c5e02 | |||
| bdb4fd0d25 | |||
| 2440e05457 | |||
| 1337c6c0d1 | |||
| 957689e07c | |||
| 3952d74f87 | |||
| 6dc4b81693 | |||
| 9cd953b7be | |||
| 631810dda2 | |||
| 8c12bc5506 | |||
| d4091a4826 | |||
| a7c269886b | |||
| f54593037b | |||
| 0415cf6a08 | |||
| 87c22d32b0 | |||
| 27dd15b403 | |||
| dd452c19ce | |||
| acc0846cf3 | |||
| 5a6738841f | |||
| bea451c9d6 | |||
| 676e904ec6 | |||
| 49aa5c61bc | |||
| a2272b852c | |||
| 1fa77630f9 | |||
| a841c4cc05 | |||
| 1874808e3b | |||
| 149b67916b | |||
| 7fb6f58162 | |||
| a309114a18 | |||
| 35cbde9530 | |||
| b9211b182a | |||
| 81ec499ae9 | |||
| f2e276eabd | |||
| f6dbaba1f8 | |||
| 65dcff052a | |||
| e3861567c7 | |||
| e89a20c725 | |||
| a8acc6bd28 | |||
| eb5efc735e | |||
| 737af527cd | |||
| 875d2b7e63 | |||
| bf1cb0e1bd | |||
| 2bfeb1b06c | |||
| 820e4302b7 | |||
| 84d761b418 | |||
| e5e27f2ac4 | |||
| efca9b18a8 | |||
| 607821ecbc | |||
| 7cc1b239d4 | |||
| efa8de34da | |||
| bcf45e3c5a | |||
| 0a3cd1d2ab | |||
| 3e621856c8 | |||
| 32e4d64ddb | |||
| 46e45ca84b | |||
| fcb13bd8ee | |||
| eacad9b453 | |||
| ed479e2a13 | |||
| 19937cb75d | |||
| a95093222c | |||
| 7b599d1882 | |||
| 4d501fd2f4 | |||
| c3d3053ded | |||
| 14d5fd3e11 | |||
| 9f79453334 | |||
| c7f1c881c0 | |||
| c39c032b4e | |||
| fa96c18308 | |||
| d90145a47f | |||
| 4871f86346 | |||
| 919eaf1e9a | |||
| 8a84ca16d1 | |||
| 12b4fdd692 | |||
| 3d8451d5d0 | |||
| cf88f28519 | |||
| 15d99eee46 | |||
| 6d68277db8 | |||
| 3e3b5557f7 | |||
| ba1a9d9f8e | |||
| 3e1a7953ec | |||
| 073fb2d717 | |||
| 7f33021184 | |||
| b396096721 | |||
| 0a5c6d832f | |||
| d279566114 | |||
| 2693cbb136 | |||
| f7a9be4292 | |||
| a926033b66 | |||
| dbcdd06b19 | |||
| ff5dab5c70 | |||
| 84b576418f | |||
| 253b124903 | |||
| 33dc5ddf79 | |||
| a456fa537c | |||
| 9e1fb93e08 | |||
| 5d24f702be | |||
| ca8e2c2986 | |||
| 2f0e5a45c5 | |||
| c4b28b0bc4 | |||
| 6cc4747965 | |||
| 91cd34b26a | |||
| e35a2f028c | |||
| 9cff2bd10f | |||
| 6d406498db | |||
| 939ae37baf | |||
| 4d6c8744e5 | |||
| e252d8b740 | |||
| 9b8d63e21b | |||
| 437fc04620 | |||
| 04dee090a3 | |||
| 245c6d24a1 | |||
| e92fd05f9f | |||
| 937f8e6919 | |||
| f489e6273e | |||
| 4f99bdbc87 | |||
| 1250b33220 | |||
| b57aee4595 | |||
| d9da51933d | |||
| 9fca93e7d8 | |||
| 854b2ed185 | |||
| cfac03bc40 | |||
| a3cb4a6ae8 | |||
| 2798fa4444 | |||
| 8bac0614f5 | |||
| 646280a6a9 | |||
| 208bdffdf7 | |||
| 9e7291903f | |||
| d56835fc00 | |||
| 1d5e717a80 | |||
| 2f44d9ae18 | |||
| 0627ab81d6 | |||
| 8167a398fc | |||
| 6291751014 | |||
| ee5ddaab52 | |||
| 207476be1f | |||
| b9b470c2ae | |||
| 52b6e8ef9d | |||
| 48c84bf74a | |||
| 5be30b3684 | |||
| 54a2b13825 | |||
| d44ddae3dc | |||
| 7bbc2459ba | |||
| 0cb77caa2a | |||
| 3f5f76458d | |||
| 835e07e8de | |||
| 3fc5e43abe | |||
| 9c8cb26bd6 | |||
| 540f4d2d0c | |||
| 51be255821 | |||
| 7d278fea25 | |||
| ca72d3b23a | |||
| 534cd3be1c | |||
| 78536de1b5 | |||
| edc67d9ec3 | |||
| 4dd12434b7 | |||
| f654bb2eda | |||
| 205f98cfeb | |||
| 69fa5bf464 | |||
| e7dc58ac6c | |||
| 2033299632 | |||
| 639d34d5d9 | |||
| 605181da32 | |||
| 183f631190 | |||
| 64c99b9871 | |||
| d29b0a27ff | |||
| aaa726e6dc | |||
| 05cc0c4644 | |||
| e1c0a92e60 | |||
| c770e4779a | |||
| c135122ca1 | |||
| 1d87df2afe | |||
| 8efc0ca0ea | |||
| 001d524ff7 | |||
| 5c5dce9dc3 | |||
| 293256c2ca | |||
| 4e027c1de3 | |||
| 6712a33164 | |||
| 98531143a6 | |||
| 71127ba308 | |||
| 0f6f571ccb | |||
| 9b6936a4fb | |||
| 8b78609412 | |||
| 4a31082f8c | |||
| 3643a69d8d | |||
| adbaa4aa89 | |||
| 0e025eda1b | |||
| 7c44bf8e94 | |||
| e6718f1113 | |||
| f3f6adb03a | |||
| 977827e453 | |||
| bcc9153991 | |||
| c04be14cd9 | |||
| de8f29751d | |||
| 12d58e4d08 | |||
| 3ae44185f4 | |||
| 1290f42f75 | |||
| 17b91dcad7 | |||
| 72da1642ee | |||
| 76d93b3a61 | |||
| 7be0ee9566 | |||
| 326e1f8da5 | |||
| e4e44ddd19 | |||
| 46bdde9265 | |||
| 09eba58927 | |||
| 0e884c78f5 | |||
| 2f44721086 | |||
| 6e08eebcbe | |||
| d9422d00ac | |||
| efc0501548 | |||
| bb07c60838 | |||
| 330ea05ca3 | |||
| 1f534b48a3 | |||
| aceae5eee3 | |||
| d4d66fd529 | |||
| b7686d6d37 | |||
| b97e397354 | |||
| ab8fbf171e | |||
| 2c0cfa24b0 | |||
| f28a76d470 | |||
| f3525c84a3 | |||
| abe9b54112 | |||
| b2aa59d893 | |||
| 9e9865afa7 | |||
| ecc439d4e8 | |||
| 452f0fc99e | |||
| 54f47845f6 | |||
| cb238cd6d9 | |||
| 897a9f4584 | |||
| 55f9b121de | |||
| 386f38af5e | |||
| 65cea430c2 | |||
| 4d38b508a3 | |||
| 5c774fd391 | |||
| f6d760e90f | |||
| c3ffa40bad | |||
| 5ce5d0e593 | |||
| b83f1efde8 | |||
| ca5f14c366 | |||
| 95a8b51fb4 | |||
| 2b64979730 | |||
| 508cb7db88 | |||
| e8d6d96883 | |||
| 9378a6e7bf | |||
| 1d609fc5c7 | |||
| 49d1e3dbaf | |||
| b0e9334852 | |||
| 95bd369959 | |||
| a433890097 | |||
| c9c3be517c | |||
| 89e122cd31 | |||
| 27c2a03ea8 | |||
| b56eb3a948 | |||
| fb1b0bfd88 | |||
| dd10167ec2 | |||
| 3138ad10ec | |||
| 080ca82605 | |||
| 0290de2360 | |||
| 9b330adc1d | |||
| f3ff1ae08b | |||
| 4f699af051 | |||
| 7ebc7259cb | |||
| 9d22edbdb8 | |||
| 7e95058cb5 | |||
| f33753e0cd | |||
| ac71e1d753 | |||
| 8151b6efd6 | |||
| 94de306c93 | |||
| 51f8ea26de | |||
| 29d9bb17cd | |||
| 30501a882d | |||
| f2f98e1d16 | |||
| cbf465eeea | |||
| 79c400e912 | |||
| cc4c49b5b0 | |||
| 96ea2c911d | |||
| 131b11680a | |||
| 3c6ab96283 | |||
| d2c4fa921a | |||
| dbb51a9328 | |||
| 55bfc3fa73 | |||
| d9c5182d1a | |||
| 899fcbf2da | |||
| 6e2c0d8706 | |||
| c4e599ce63 | |||
| 9cad9e4467 | |||
| 48019444ea | |||
| 76d31a7fbb | |||
| facb9d93c0 | |||
| 1d6bd46c5e | |||
| 8646dc1825 | |||
| 2992bdeed7 | |||
| c5601e6c88 | |||
| ef1d9a561c | |||
| 2d3b32c94a | |||
| 269378aa0d | |||
| 6889542801 | |||
| 8e6f1f1740 | |||
| b6ab1a992c | |||
| a5a267567c | |||
| f05f855c07 | |||
| bc5f773ba6 |
@ -512,16 +512,6 @@ func mapToStruct() {
|
||||
}
|
||||
}
|
||||
|
||||
// getQueriedSqls
|
||||
func getQueriedSqls() {
|
||||
for k, v := range db.GetQueriedSqls() {
|
||||
fmt.Println(k, ":")
|
||||
fmt.Println("Sql :", v.Sql)
|
||||
fmt.Println("Args :", v.Args)
|
||||
fmt.Println("Error:", v.Error)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
db.PingMaster()
|
||||
|
||||
@ -1,8 +1,14 @@
|
||||
|
||||
# MySQL数据库配置
|
||||
# MySQL.
|
||||
[database]
|
||||
# debug = true
|
||||
link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test?parseTime=true&loc=Local"
|
||||
debug = true
|
||||
link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test?parseTime=true&loc=Local"
|
||||
MaxOpen = 100
|
||||
|
||||
# Redis.
|
||||
[redis]
|
||||
default = "127.0.0.1:6379,0"
|
||||
cache = "127.0.0.1:6379,1"
|
||||
|
||||
#[database]
|
||||
# [[database.default]]
|
||||
|
||||
@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
@ -11,11 +10,19 @@ func main() {
|
||||
// 开启调试模式,以便于记录所有执行的SQL
|
||||
db.SetDebug(true)
|
||||
|
||||
r, e := db.Table("test").Order("id asc").All()
|
||||
r, e := db.GetAll("SELECT * from `user` where id in(?)", g.Slice{})
|
||||
if e != nil {
|
||||
fmt.Println(e)
|
||||
}
|
||||
if r != nil {
|
||||
fmt.Println(r.List())
|
||||
fmt.Println(r)
|
||||
}
|
||||
return
|
||||
//r, e := db.Table("user").Where("id in(?)", g.Slice{}).All()
|
||||
//if e != nil {
|
||||
// fmt.Println(e)
|
||||
//}
|
||||
//if r != nil {
|
||||
// fmt.Println(r.List())
|
||||
//}
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/util/gutil"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@ -10,7 +11,7 @@ func main() {
|
||||
Host: "127.0.0.1",
|
||||
Port: "3306",
|
||||
User: "root",
|
||||
Pass: "123456",
|
||||
Pass: "12345678",
|
||||
Name: "test",
|
||||
Type: "mysql",
|
||||
Role: "master",
|
||||
@ -20,19 +21,20 @@ func main() {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
//db.GetCache().SetAdapter(adapter.NewRedis(g.Redis()))
|
||||
// 开启调试模式,以便于记录所有执行的SQL
|
||||
db.SetDebug(true)
|
||||
|
||||
// 执行2次查询并将查询结果缓存3秒,并可执行缓存名称(可选)
|
||||
for i := 0; i < 2; i++ {
|
||||
r, _ := db.Table("user").Cache(3, "vip-user").Where("uid=?", 1).One()
|
||||
gutil.Dump(r.ToMap())
|
||||
for i := 0; i < 3; i++ {
|
||||
r, _ := db.Table("user").Cache(3000*time.Second).Where("id=?", 1).One()
|
||||
gutil.Dump(r.Map())
|
||||
}
|
||||
|
||||
// 执行更新操作,并清理指定名称的查询缓存
|
||||
db.Table("user").Cache(-1, "vip-user").Data(gdb.Map{"name": "smith"}).Where("uid=?", 1).Update()
|
||||
//db.Table("user").Cache(-1, "vip-user").Data(gdb.Map{"name": "smith"}).Where("id=?", 1).Update()
|
||||
|
||||
// 再次执行查询,启用查询缓存特性
|
||||
r, _ := db.Table("user").Cache(3, "vip-user").Where("uid=?", 1).One()
|
||||
gutil.Dump(r.ToMap())
|
||||
//r, _ := db.Table("user").Cache(300000*time.Second, "vip-user").Where("id=?", 1).One()
|
||||
//gutil.Dump(r.Map())
|
||||
}
|
||||
|
||||
@ -8,9 +8,6 @@ import (
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
db.SetMaxIdleConnCount(10)
|
||||
db.SetMaxOpenConnCount(10)
|
||||
db.SetMaxConnLifetime(time.Minute)
|
||||
|
||||
// 开启调试模式,以便于记录所有执行的SQL
|
||||
db.SetDebug(true)
|
||||
@ -19,7 +16,7 @@ func main() {
|
||||
for i := 0; i < 10; i++ {
|
||||
go db.Table("user").All()
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -8,5 +8,5 @@ func main() {
|
||||
db := g.DB()
|
||||
db.SetDebug(true)
|
||||
|
||||
db.Table("user").Fields("DISTINCT id,nickname").Filter().All()
|
||||
db.Table("user").Data("num=num+1").Where("id", 8).Update()
|
||||
}
|
||||
|
||||
@ -513,16 +513,6 @@ func mapToStruct() {
|
||||
}
|
||||
}
|
||||
|
||||
// getQueriedSqls
|
||||
func getQueriedSqls() {
|
||||
for k, v := range db.GetQueriedSqls() {
|
||||
fmt.Println(k, ":")
|
||||
fmt.Println("Sql :", v.Sql)
|
||||
fmt.Println("Args :", v.Args)
|
||||
fmt.Println("Error:", v.Error)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
db.PingMaster()
|
||||
|
||||
@ -2,25 +2,14 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/i18n/gi18n"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
t := gi18n.New()
|
||||
t.SetPath("/Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/i18n/gi18n/i18n")
|
||||
t.SetLanguage("en")
|
||||
fmt.Println(t.Translate(`hello`))
|
||||
fmt.Println(t.Translate(`{#hello}{#world}!`))
|
||||
|
||||
t.SetLanguage("ja")
|
||||
fmt.Println(t.Translate(`hello`))
|
||||
fmt.Println(t.Translate(`{#hello}{#world}!`))
|
||||
|
||||
t.SetLanguage("ru")
|
||||
fmt.Println(t.Translate(`hello`))
|
||||
fmt.Println(t.Translate(`{#hello}{#world}!`))
|
||||
|
||||
fmt.Println(t.Translate(`hello`, "zh-CN"))
|
||||
fmt.Println(t.Translate(`{#hello}{#world}!`, "zh-CN"))
|
||||
var (
|
||||
orderId = 865271654
|
||||
orderAmount = 99.8
|
||||
)
|
||||
fmt.Println(g.I18n().Tfl(`en`, `{#OrderPaid}`, orderId, orderAmount))
|
||||
fmt.Println(g.I18n().Tfl(`zh-CN`, `{#OrderPaid}`, orderId, orderAmount))
|
||||
}
|
||||
|
||||
@ -1,3 +1 @@
|
||||
|
||||
hello = "Hello"
|
||||
world = "World"
|
||||
OrderPaid = "You have successfully complete order #%d payment, paid amount: ¥%0.2f."
|
||||
@ -1,2 +1 @@
|
||||
hello = "你好"
|
||||
world = "世界"
|
||||
OrderPaid = "您已成功完成订单号 #%d 支付,支付金额¥%.2f。"
|
||||
@ -2,12 +2,34 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/glog"
|
||||
)
|
||||
|
||||
func SendXmlFile(gameId int, areaName string, filePath string) error {
|
||||
path := filepath.FromSlash(filePath)
|
||||
fmt.Println(path)
|
||||
data := g.Map{
|
||||
"gameName": gameId,
|
||||
"area": areaName,
|
||||
"file": "@file:" + path,
|
||||
"contentType": "json",
|
||||
}
|
||||
if r, err := ghttp.Post("http://127.0.0.1:8199/upload", data); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
defer r.Close()
|
||||
fmt.Println("ok")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
SendXmlFile(1, "xxx", "/Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/net/ghttp/server/session.go")
|
||||
return
|
||||
path := "/home/john/Workspace/Go/github.com/gogf/gf/version.go"
|
||||
r, e := ghttp.Post("http://127.0.0.1:8199/upload", "upload-file=@file:"+path)
|
||||
if e != nil {
|
||||
|
||||
@ -8,7 +8,7 @@ import (
|
||||
// Upload uploads files to /tmp .
|
||||
func Upload(r *ghttp.Request) {
|
||||
saveDirPath := "/tmp/"
|
||||
files := r.GetUploadFiles("upload-file")
|
||||
files := r.GetUploadFiles("file")
|
||||
if _, err := files.Save(saveDirPath); err != nil {
|
||||
r.Response.WriteExit(err)
|
||||
}
|
||||
|
||||
@ -3,15 +3,21 @@ package main
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/glog"
|
||||
)
|
||||
|
||||
type GetById struct {
|
||||
Id *g.Var `p:"id" v:"required|integer#id不能为空|id必须为整数"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.SetIndexFolder(true)
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
glog.Println(r.Header)
|
||||
r.Response.Write("hello world")
|
||||
var idInfo *GetById
|
||||
if err := r.Parse(&idInfo); err != nil {
|
||||
r.Response.Write(err)
|
||||
}
|
||||
r.Response.Write("ok")
|
||||
})
|
||||
s.SetPort(8999)
|
||||
s.Run()
|
||||
|
||||
@ -5,21 +5,18 @@ import (
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
}
|
||||
type User struct{}
|
||||
|
||||
func (c *User) Index(r *ghttp.Request) {
|
||||
r.Response.Write("Index")
|
||||
}
|
||||
|
||||
// 不符合规范,不会被注册
|
||||
func (c *User) Test(r *ghttp.Request, value interface{}) {
|
||||
func (c *User) Test(r *ghttp.Request) {
|
||||
r.Response.Write("Test")
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindObject("/user", new(User))
|
||||
u := new(User)
|
||||
s.Group("/", func(group *ghttp.RouterGroup) {
|
||||
group.GET("/db-{table}/{id}", u, "Test")
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -1,12 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := ghttp.GetServer()
|
||||
s.EnablePProf()
|
||||
s := g.Server()
|
||||
s.Domain("localhost").EnablePProf()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Writeln("哈喽世界!")
|
||||
})
|
||||
|
||||
@ -6,12 +6,8 @@ import (
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.SetConfigWithMap(g.Map{"Graceful": true})
|
||||
s.EnableAdmin()
|
||||
//s.BindHookHandler("/admin/*any", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
// if !r.BasicAuth("admin", "123", "") {
|
||||
// r.Exit()
|
||||
// }
|
||||
//})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -1,29 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/os/gfcache"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := 0
|
||||
r := ""
|
||||
path := gfile.TempDir() + gfile.Separator + "temp"
|
||||
gfile.PutContents(path, "hello")
|
||||
|
||||
s = gfcache.GetSize()
|
||||
r = gfcache.GetContents(path)
|
||||
fmt.Println(s, r)
|
||||
|
||||
gfile.PutContentsAppend(path, " john")
|
||||
|
||||
// 等待1秒以便gfsnotify回调能处理完成
|
||||
time.Sleep(time.Second)
|
||||
|
||||
s = gfcache.GetSize()
|
||||
r = gfcache.GetContents(path)
|
||||
fmt.Println(s, r)
|
||||
}
|
||||
@ -2,19 +2,18 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/frame/gmvc"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
type Controller struct {
|
||||
gmvc.Controller
|
||||
}
|
||||
|
||||
func (c *Controller) Test() {
|
||||
c.View.Display("layout.html")
|
||||
}
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindControllerMethod("/", new(Controller), "Test")
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.WriteTpl("layout.html", g.Map{
|
||||
"header": "This is header",
|
||||
"container": "This is container",
|
||||
"footer": "This is footer",
|
||||
})
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
{{define "container"}}
|
||||
<h1>CONTAINER</h1>
|
||||
<h1>{{.container}}</h1>
|
||||
{{end}}
|
||||
@ -1,3 +1,3 @@
|
||||
{{define "footer"}}
|
||||
<h1>FOOTER</h1>
|
||||
<h1>{{.footer}}</h1>
|
||||
{{end}}
|
||||
@ -1,3 +1,3 @@
|
||||
{{define "header"}}
|
||||
<h1>HEADER</h1>
|
||||
<h1>{{.header}}</h1>
|
||||
{{end}}
|
||||
@ -2,14 +2,14 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>GoFrame Layout</title>
|
||||
{{template "header"}}
|
||||
{{template "header" .}}
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
{{template "container"}}
|
||||
{{template "container" .}}
|
||||
</div>
|
||||
<div class="footer">
|
||||
{{template "footer"}}
|
||||
{{template "footer" .}}
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@ -9,11 +9,13 @@ func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/main1", func(r *ghttp.Request) {
|
||||
r.Response.WriteTpl("layout.html", g.Map{
|
||||
"name": "smith",
|
||||
"mainTpl": "main/main1.html",
|
||||
})
|
||||
})
|
||||
s.BindHandler("/main2", func(r *ghttp.Request) {
|
||||
r.Response.WriteTpl("layout.html", g.Map{
|
||||
"name": "john",
|
||||
"mainTpl": "main/main2.html",
|
||||
})
|
||||
})
|
||||
|
||||
@ -1,25 +0,0 @@
|
||||
|
||||
[database]
|
||||
debug = true
|
||||
link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
|
||||
|
||||
[redis]
|
||||
default = "127.0.0.1:6379,0"
|
||||
cache = "127.0.0.1:6379,1"
|
||||
|
||||
# Logger.
|
||||
[logger]
|
||||
Path = "/tmp/log/gf-app"
|
||||
Level = "all"
|
||||
Stdout = true
|
||||
|
||||
[viewer]
|
||||
delimiters = ["${", "}"]
|
||||
autoencode = true
|
||||
|
||||
|
||||
[server]
|
||||
Address = ":8800"
|
||||
ServerRoot = "/Users/john/Downloads"
|
||||
ServerAgent = "gf-app"
|
||||
# LogPath = "./log/gf-app/server"
|
||||
@ -1,13 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/encoding/gjson"
|
||||
)
|
||||
|
||||
func main() {
|
||||
body := "{\"id\": 413231383385427875}"
|
||||
if dat, err := gjson.DecodeToJson(body); err == nil {
|
||||
fmt.Println(dat.MustToJsonString())
|
||||
}
|
||||
}
|
||||
@ -1,11 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.SetIndexFolder(true)
|
||||
s.Run()
|
||||
}
|
||||
@ -10,4 +10,7 @@ func main() {
|
||||
for i := 0; i < 100; i++ {
|
||||
fmt.Println(grand.S(16))
|
||||
}
|
||||
for i := 0; i < 100; i++ {
|
||||
fmt.Println(grand.N(0, 99999999))
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
for i := 0; i < 1000; i++ {
|
||||
for i := 0; i < 100; i++ {
|
||||
s := guid.S()
|
||||
fmt.Println(s, len(s))
|
||||
}
|
||||
|
||||
@ -7,9 +7,14 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(strconv.FormatUint(4589634556, 36))
|
||||
fmt.Println(strconv.FormatUint(math.MaxUint64-2, 36))
|
||||
fmt.Println(strconv.FormatUint(math.MaxUint32-1, 36))
|
||||
fmt.Println(strconv.FormatUint(math.MaxUint32, 36))
|
||||
fmt.Println(strconv.FormatUint(2000000, 36))
|
||||
// 36*36^2+36*36+36
|
||||
var s string
|
||||
fmt.Println(strconv.ParseUint("zzz", 36, 3))
|
||||
fmt.Println(1 << 1)
|
||||
// MaxInt64
|
||||
s = strconv.FormatUint(math.MaxUint64, 16)
|
||||
fmt.Println(s, len(s))
|
||||
// PID
|
||||
s = strconv.FormatInt(1000000, 36)
|
||||
fmt.Println(s, len(s))
|
||||
}
|
||||
|
||||
@ -6,17 +6,17 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
for i := 0; i < 10; i++ {
|
||||
for i := 0; i < 100; i++ {
|
||||
s := guid.S([]byte("123"))
|
||||
fmt.Println(s, len(s))
|
||||
}
|
||||
fmt.Println()
|
||||
for i := 0; i < 10; i++ {
|
||||
for i := 0; i < 100; i++ {
|
||||
s := guid.S([]byte("123"), []byte("456"))
|
||||
fmt.Println(s, len(s))
|
||||
}
|
||||
fmt.Println()
|
||||
for i := 0; i < 10; i++ {
|
||||
for i := 0; i < 100; i++ {
|
||||
s := guid.S([]byte("123"), []byte("456"), []byte("789"))
|
||||
fmt.Println(s, len(s))
|
||||
}
|
||||
|
||||
@ -11,7 +11,7 @@ func main() {
|
||||
fmt.Println(1)
|
||||
gutil.Throw("error")
|
||||
fmt.Println(2)
|
||||
}, func(err interface{}) {
|
||||
}, func(err error) {
|
||||
fmt.Println(err)
|
||||
})
|
||||
}
|
||||
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
@ -6,7 +6,7 @@
|
||||
.idea/
|
||||
.settings/
|
||||
.vscode/
|
||||
vender/
|
||||
vendor/
|
||||
composer.lock
|
||||
gitpush.sh
|
||||
pkg/
|
||||
@ -15,3 +15,4 @@ cbuild
|
||||
**/.DS_Store
|
||||
.vscode/
|
||||
go.sum
|
||||
.example/other/
|
||||
|
||||
@ -5,6 +5,7 @@ go:
|
||||
- "1.12.x"
|
||||
- "1.13.x"
|
||||
- "1.14.x"
|
||||
- "1.15.x"
|
||||
|
||||
branches:
|
||||
only:
|
||||
|
||||
78
DONATOR.MD
78
DONATOR.MD
@ -1,13 +1,17 @@
|
||||
# Donators
|
||||
|
||||
We currently accept donation by Alipay/WechatPay, please note your github/gitee account in your payment bill.
|
||||
We currently accept donation by [Wechat](https://goframe.org/images/donate.png) / [Alipay](https://goframe.org/images/donate.png) / [Gitee](https://gitee.com/johng/gf),
|
||||
please note your github/gitee account in your payment bill. All the donations will be used only for `GoFrame` project development and its community construction.
|
||||
|
||||
> If you cannot see the donation image, please click [here](https://goframe.org/images/donate.png).
|
||||
|
||||
<img src="https://goframe.org/images/donate.png?20200718"/>
|
||||
|
||||
> If you cannot view the donation image, please click [here](https://goframe.org/images/donate.png).
|
||||
|
||||
| Name | Channel | Amount | Comment
|
||||
|---|---|--- | ---
|
||||
|[hailaz](https://gitee.com/hailaz)|gitee|¥20.00 |
|
||||
|[ireadx](https://github.com/ireadx)|alipay|¥301.00 |
|
||||
|[ireadx](https://github.com/ireadx)|alipay|¥501.00 |
|
||||
|[mg91](https://gitee.com/mg91)|gitee|¥10.00 |
|
||||
|[pibigstar](https://github.com/pibigstar)|alipay|¥10.00 |
|
||||
|[tiangenglan](https://gitee.com/tiangenglan)|gitee|¥30.00 |
|
||||
@ -46,7 +50,7 @@ We currently accept donation by Alipay/WechatPay, please note your github/gitee
|
||||
|struggler|alipay|¥18.80|
|
||||
|*铁|wechat|¥0.01|
|
||||
|C*e|wechat|¥66.66| GF越来越好,棒👍!
|
||||
|(佚名)|wechat|¥6.66| (名字打不出来,也没备注,捐赠时间2020-02-21 14:24:34)
|
||||
|---P คิดถึง|wechat|¥6.66|
|
||||
|[王飞](https://gitee.com/wang_2018)|gitee|¥20.00| 感谢您的开源项目!
|
||||
|[Zeroing-ZY](https://gitee.com/yunjieg)|gitee|¥20.00| 感谢您的开源项目!
|
||||
|[katydid酱](https://gitee.com/katydid2005)|gitee|¥50.00| 感谢您的开源项目!框架给予了很大的帮助!谢谢大佬!
|
||||
@ -56,7 +60,7 @@ We currently accept donation by Alipay/WechatPay, please note your github/gitee
|
||||
|🚶|wechat|¥6.88| 喝杯冰阔落
|
||||
|a*l|wechat|¥10.00| gf
|
||||
|[wxkj](https://gitee.com/wxkj)|wechat|¥10.00|
|
||||
|*包|wechat|¥9.99|
|
||||
|[米司特包](https://github.com/misitebao)|wechat|¥9.99|
|
||||
|重庆宝尔威科技|wechat|¥6.66|
|
||||
|琦玉-QPT|wechat|¥6.66|
|
||||
|sailsea|wechat|¥11.00|
|
||||
@ -64,7 +68,7 @@ We currently accept donation by Alipay/WechatPay, please note your github/gitee
|
||||
|*华|wechat|¥6.66| 感谢郭强的热心
|
||||
|[Playhi](https://github.com/Playhi)|alipay|¥10.00|
|
||||
|北京京纬互动科技|alipay|¥200.00|
|
||||
|米司特包|wechat|¥99.99|
|
||||
|[米司特包](https://github.com/misitebao)|wechat|¥99.99|
|
||||
|金毛|alipay|¥100.00|
|
||||
|1*1x|wechat|¥100.00|
|
||||
|[ywanbing](https://github.com/ywanbing)|wechat|¥66.66|
|
||||
@ -73,13 +77,71 @@ We currently accept donation by Alipay/WechatPay, please note your github/gitee
|
||||
|蔡蔡|wechat|¥666.00| gf真强大,让项目省心
|
||||
|jack|wechat|¥100.00|
|
||||
|sbilly|wechat|¥100.00| 祝好!
|
||||
|米司|wechat|¥166.66| 大佬加油!
|
||||
|[米司特包](https://github.com/misitebao)|wechat|¥166.66| 大佬加油!
|
||||
|*秦|wechat|¥20.00| 给群主献上一杯咖啡...
|
||||
|[zhuhuan12](https://gitee.com/zhuhuan12)|wechat|¥50.00|
|
||||
|faddei|qq|¥9.99|
|
||||
|*天|wechat|¥10.00|
|
||||
|*.|wechat|¥20.00| 学生一个微薄之力,冲
|
||||
|李勇|wechat|¥50.00|
|
||||
|[米司特包](https://github.com/misitebao)|wechat|¥66.66|
|
||||
|千年|wechat|¥1.08|
|
||||
|[szzxing](https://github.com/szzxing)|wechat|¥50.00|
|
||||
|伟客互联|wechat|¥66.66|
|
||||
|[sbilly](https://github.com/sbilly)|wechat|¥99.00|
|
||||
|**阳|alipay|¥100.01|
|
||||
|**亮|alipay|¥10.00|
|
||||
|[mingzaily](https://github.com/mingzaily)|alipay|¥30.00|
|
||||
|[seny0929](https://gitee.com/seny0929)|alipay|¥9.90|
|
||||
|Fly的狐狸|alipay|¥100.00|
|
||||
|六七 ·|wechat|¥88.88| gf越来越好
|
||||
|Tom|wechat|¥500.00| 一点心意 希望GF越来越好
|
||||
|Chao|wechat|¥166.00|
|
||||
|汤sir|wechat|¥10.00| 强哥,喝杯咖啡☕️
|
||||
|秋叶、|wechat|¥66.66| GF带我一起玩
|
||||
||wechat|¥20.00|
|
||||
|程凤明|wechat|¥18.80|
|
||||
|Glowworm|wechat|¥8.88| 感谢大佬
|
||||
|徒行|wechat|¥10.00| 目前项目在转用这个框架,鼓励一下大佬吧👍👍👍
|
||||
|产品设计.软件开发|wechat|¥10.00| 祝愿走向更成功!
|
||||
|[charlieccGuo](https://github.com/charlieccGuo)|wechat|¥10.00|
|
||||
|yiran|wechat|¥20.00| 赏给大佬喝茶🍵
|
||||
|长夏朔酒|wechat|¥20.00| 请大佬喝茶
|
||||
|💥聪จุ๊บ 🇨🇳|wechat|¥66.66| 请大佬喝茶
|
||||
|Even_|wechat|¥10.00| 日照市民发来贺电!
|
||||
|智慧人生|wechat|¥50.00| 智慧人生
|
||||
|一滴水|wechat|¥10.00| goframe 越来越强大
|
||||
|[wenzi1](https://github.com/wenzi1)|alipay|¥100.00|
|
||||
|[hyuant](https://github.com/hyuant)|alipay|¥66.66|
|
||||
|*庆|alipay|¥9.99| 支持一下,gf越来越好
|
||||
|**君|alipay|¥10.00| 加油
|
||||
|向回走的闹钟|wechat|¥20.00| 越来越好
|
||||
|金毛|wechat|¥100.00|
|
||||
|莫失莫忘|wechat|¥100.00|
|
||||
|**航|alipay|¥20.00|
|
||||
|阿康|wechat|¥100.00|
|
||||
|Tzp|wechat|¥10.00|
|
||||
|[hkxiaoyu118](https://github.com/hkxiaoyu118)|wechat|¥10.00|
|
||||
|辰|wechat|¥50.00|
|
||||
|LSJ|wechat|¥66.66|我想我是海:祝gf越来越好,统治后端
|
||||
|yu|wechat|¥100.00|感谢开源,加油!我是QQ群里的lah
|
||||
|雁字回时月满楼|wechat|¥20.00|感谢gf
|
||||
|Panda|wechat|¥20.00|支持一下!gf很棒👍
|
||||
|[Thunur](https://gitee.com/thunur)|wechat|¥100.00|
|
||||
|[Mr.奇淼](https://www.gin-vue-admin.com/)|wechat|¥18.88|强哥无敌,奇淼爱你
|
||||
|[SliverHorn](hhttps://github.com/sliverhorn)|wechat|¥17.77|强哥无敌,SliverHorn爱你
|
||||
|[fly的狐狸](https://github.com/zcool321)|wechat|¥50.00|
|
||||
|北漂生活|wechat|¥66.66|gf大展鸿图
|
||||
|YJ|wechat|¥10.00|YangJ-Eric祝愿越来越好
|
||||
|秋葵|wechat|¥20.00|之前强哥
|
||||
|陈诚|wechat|¥100.00|Loocor恭喜郭总发版🎉
|
||||
|**栋|alipay|¥100.00|
|
||||
|**浩|alipay|¥100.00|
|
||||
|RAGGA-TIME|alipay|¥50.00|
|
||||
|[ChArmy](https://gitee.com/charmy)|alipay|¥50.00|
|
||||
|[sanfenzui](https://gitee.com/sanfenzui)|alipay|¥88.00|
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<img src="https://goframe.org/images/donate.png"/>
|
||||
|
||||
39
README.MD
39
README.MD
@ -31,19 +31,19 @@ require github.com/gogf/gf latest
|
||||
golang version >= 1.11
|
||||
```
|
||||
|
||||
# Architecture
|
||||
<div align=center>
|
||||
<img src="https://goframe.org/images/arch.png?v=12"/>
|
||||
</div>
|
||||
|
||||
# Packages
|
||||
1. **Primary**
|
||||
1. **Primary Package**
|
||||
|
||||
The `gf` repository maintains some basic and most commonly used packages, keeping it as lightweight and simple as possible.
|
||||
|
||||
1. **Community**
|
||||
1. **Community Package**
|
||||
|
||||
The community packages are contrinuted and maintained by community members, which are reposited in `gogf` organization. Some of the community packages are seperated from th `gf` repository, which are not of common usage or are with heavy dependecies.
|
||||
|
||||
# Architecture
|
||||
<div align=center>
|
||||
<img src="https://goframe.org/images/arch.png?v=11"/>
|
||||
</div>
|
||||
The community packages are contributed and maintained by community members, which are hosted in `gogf` organization. Some of the community packages are separated from the `gf` repository, which are not of common usage or are with heavy dependencies.
|
||||
|
||||
# Performance
|
||||
|
||||
@ -115,18 +115,33 @@ The concurrency starts from `100` to `10000`.
|
||||
|
||||
`GF` is licensed under the [MIT License](LICENSE), 100% free and open-source, forever.
|
||||
|
||||
# Part Of Users
|
||||
|
||||
- [Tencent](https://www.tencent.com/)
|
||||
- [ZTE](https://www.zte.com.cn/china/)
|
||||
- [Ant Financial Services](https://www.antfin.com/)
|
||||
- [MedLinker](https://www.medlinker.com/)
|
||||
- [KuCoin](https://www.kucoin.io/)
|
||||
- [LeYouJia](https://www.leyoujia.com/)
|
||||
- [IGG](https://igg.com)
|
||||
- [XiMaLaYa](https://www.ximalaya.com)
|
||||
|
||||
> We list part of the users here, if your company or products are using `GoFrame`, please let us know [here](https://github.com/gogf/gf/issues/168).
|
||||
|
||||
|
||||
# Contributors
|
||||
This project exists thanks to all the people who contribute. [[Contributors](https://github.com/gogf/gf/graphs/contributors)].
|
||||
<a href="https://github.com/gogf/gf/graphs/contributors"><img src="https://opencollective.com/goframe/contributors.svg?width=890&button=false" /></a>
|
||||
|
||||
<!--
|
||||
|
||||
# Donators
|
||||
|
||||
We currently accept donation by Alipay/WechatPay, please note your github/gitee account in your payment bill. If you like `GF`, why not [buy developer a cup of coffee](DONATOR.MD)?
|
||||
|
||||
# Sponsors
|
||||
We appreciate any kind of sponsorship for `GF` development. If you've got some interesting, please contact WeChat `389961817` / Email `john@goframe.org`.
|
||||
-->
|
||||
|
||||
|
||||
|
||||
# Thanks
|
||||
<a href="https://www.jetbrains.com/?from=GoFrame"><img src="https://goframe.org/images/jetbrains.png" width="100" alt="JetBrains"/></a>
|
||||
@ -138,3 +153,7 @@ We appreciate any kind of sponsorship for `GF` development. If you've got some i
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
32
README_ZH.MD
32
README_ZH.MD
@ -45,6 +45,13 @@ require github.com/gogf/gf latest
|
||||
golang版本 >= 1.11
|
||||
```
|
||||
|
||||
|
||||
|
||||
# 架构
|
||||
<div align=center>
|
||||
<img src="https://goframe.org/images/arch.png?v=12"/>
|
||||
</div>
|
||||
|
||||
# 模块
|
||||
|
||||
1. **核心模块**
|
||||
@ -53,14 +60,9 @@ golang版本 >= 1.11
|
||||
|
||||
1. **社区模块**
|
||||
|
||||
社区模块主要由社区贡献并维护,大部分也是由`gf`主仓库的贡献者提供及维护,存放于`gogf`组织下,与`gf`主仓库处于同一级别。有的社区模块是从`gf`主仓库中剥离出来单独维护的模块,这些模块并不是特别常用,或者对外部依赖较重。
|
||||
社区模块主要由社区贡献并维护,大部分也是由`gf`主仓库的贡献者提供及维护,存放于`gogf`空间下,与`gf`主仓库处于同一级别。有的社区模块是从`gf`主仓库中剥离出来单独维护的模块,这些模块并不是特别常用,或者对外部依赖较重。
|
||||
|
||||
|
||||
# 架构
|
||||
<div align=center>
|
||||
<img src="https://goframe.org/images/arch.png?v=11"/>
|
||||
</div>
|
||||
|
||||
# 性能
|
||||
|
||||
以下是目前最流行的`WEB Server` Golang框架/类库性能测试结果。
|
||||
@ -127,12 +129,25 @@ ab -t 10 -c 100 http://127.0.0.1:3000/json
|
||||
- WX交流群:微信添加`389961817`备注`GF`
|
||||
- 主库ISSUE:https://github.com/gogf/gf/issues
|
||||
|
||||
> 建议通过阅读`Gorame`的源码以及API文档深度学习`GoFrame`,了解更多的精妙设计。
|
||||
> 建议通过阅读`GoFrame`的源码以及API文档深度学习`GoFrame`,了解更多的精妙设计。
|
||||
|
||||
# 协议
|
||||
|
||||
`GF` 使用非常友好的 [MIT](LICENSE) 开源协议进行发布,永久`100%`开源免费。
|
||||
|
||||
# 用户
|
||||
|
||||
- [腾讯科技](https://www.tencent.com/)
|
||||
- [中兴科技](https://www.zte.com.cn/china/)
|
||||
- [蚂蚁金服](https://www.antfin.com/)
|
||||
- [医联科技](https://www.medlinker.com/)
|
||||
- [库币科技](https://www.kucoin.io/)
|
||||
- [乐有家](https://www.leyoujia.com/)
|
||||
- [IGG](https://igg.com)
|
||||
- [喜马拉雅](https://www.ximalaya.com)
|
||||
|
||||
> 在这里只列举了部分知名的用户,如果您的企业或者产品正在使用`GoFrame`,欢迎到 [这里](https://github.com/gogf/gf/issues/168) 留言。
|
||||
|
||||
# 贡献
|
||||
|
||||
感谢所有参与`GoFrame`开发的贡献者。 [[贡献者列表](https://github.com/gogf/gf/graphs/contributors)].
|
||||
@ -149,4 +164,5 @@ ab -t 10 -c 100 http://127.0.0.1:3000/json
|
||||
赞助支持`GF`框架的快速研发,如果您感兴趣,请联系 微信 `389961817` / 邮件 `john@goframe.org`。
|
||||
|
||||
# 感谢
|
||||
<a href="https://www.jetbrains.com/?from=GoFrame"><img src="https://goframe.org/images/jetbrains.png" width="100" alt="JetBrains"/></a>
|
||||
<a href="https://www.jetbrains.com/?from=GoFrame"><img src="https://goframe.org/images/jetbrains.png" width="100" alt="JetBrains"/></a>
|
||||
|
||||
|
||||
384
RELEASE.2.MD
384
RELEASE.2.MD
@ -1,3 +1,387 @@
|
||||
# `v1.14.2` (2020-10-27)
|
||||
|
||||
# GoFrame
|
||||
|
||||
`GF(Go Frame)`是一款模块化、高性能、生产级的Go基础开发框架。实现了比较完善的基础设施建设以及开发工具链,提供了常用的基础开发模块,如:缓存、日志、队列、数组、集合、容器、定时器、命令行、内存锁、对象池、配置管理、资源管理、数据校验、数据编码、定时任务、数据库ORM、TCP/UDP组件、进程管理/通信等等。并提供了Web服务开发的系列核心组件,如:`Router`、`Cookie`、`Session`、`Middleware`、服务注册、模板引擎等等,支持热重启、热更新、域名绑定、`TLS/HTTPS`、`Rewrite`等特性。
|
||||
|
||||
## 特点
|
||||
|
||||
* 模块化、松耦合设计;
|
||||
* 模块丰富、开箱即用;
|
||||
* 简便易用、易于维护;
|
||||
* 高代码质量、高单元测试覆盖率;
|
||||
* 社区活跃,大牛谦逊低调脾气好;
|
||||
* 详尽的开发文档及示例;
|
||||
* 完善的本地中文化支持;
|
||||
* 设计为团队及企业使用;
|
||||
|
||||
## 支持我们
|
||||
|
||||
OSC最佳开源项目评选开始了,如果您喜欢`GoFrame`,欢迎为`GoFrame`投上您宝贵的一票🙏 https://www.oschina.net/p/goframe
|
||||
|
||||
|
||||
# Change Log
|
||||
|
||||
由于`GoFrame`是模块化设计,因此每个版本的更新记录都会以模块的形式进行介绍。
|
||||
|
||||
重要更新:
|
||||
1. 将框架内所有的`json`操作从标准库替换为`json-iterator/go`,提高操作效率。
|
||||
1. 缓存模块重构底层设计,增加适配器设计模式,并增加内存及`Redis`适配器支持。其中内存适配器默认核心模块提供,`Redis`适配器由社区模块提供:https://goframe.org/os/gcache/adapter
|
||||
1. 增加可自定义的校验规则注册特性:https://goframe.org/util/gvalid/customrule
|
||||
1. `Web Server`增加所有配置项示例:https://goframe.org/net/ghttp/config/example
|
||||
1. `ORM`新增基于`Redis`的`SQL`缓存适配器:https://goframe.org/database/gdb/model/cache
|
||||
1. `ORM`新增模型关联实验特性:https://goframe.org/database/gdb/model/association
|
||||
1. `ORM`改进时间自动更新特性增加自定义时间字段:https://goframe.org/database/gdb/model/auto-time
|
||||
1. 错误处理模块新增`Current`及`Next`方法:https://goframe.org/errors/gerror/index
|
||||
|
||||
|
||||
|
||||
## `net`
|
||||
1. `ghttp`
|
||||
- `Client`
|
||||
- 增加`GetVar/PutVar/PostVar`等`*Var`请求方法,用于发起`HTTP`请求获取内容之后直接返回泛型对象,方便类型转换,特别是针对于返回`XML/JSON`的结果处理将会更加简便:https://goframe.org/net/ghttp/client/demo/index
|
||||
- 增加`SetProxy/Proxy`方法,用于设置客户端代理,支持`HTTP/Socket5`代理类型:https://goframe.org/net/ghttp/client/demo/proxy
|
||||
- 增加`SetRedirectLimit/RedirectLimit`方法,用于设置页面跳转数量限制。
|
||||
- `Request`
|
||||
- 增加`ParseQuery`, `ParseForm`方法,用于解析指定类型的参数,并绑定到给定的对象。
|
||||
- 增加`GetHeader`方法,用于获取指定`Header`参数。
|
||||
- 增加`GetRemoteIp`方法,用于获取请求客户端IP。在IP白名单限制时应当使用`GetRemoteIp`而不是`GetClientIp`进行判断,后者可以通过`Header`伪造。
|
||||
- 增加`ReloadParam`方法,往往用在中间件处理中,当中间件修改了请求参数,需要通过调用该方法重新解析一下请求参数。
|
||||
- 增加`GetRouterMap`方法,用于获得所有的路由参数返回为`map`。
|
||||
- `Response`
|
||||
- 将`Output`方法名称改为`Flush`,用于将缓冲区的数据写入到客户端数据流中。
|
||||
- `Server`
|
||||
- `Server`增加所有配置项示例:https://goframe.org/net/ghttp/config/example
|
||||
- 增加`SessionCookieOutput`配置,用于控制是否输出`SessionId`到`Cookie`中,默认为开启。
|
||||
- 改进路由解析,增加对`URI`带有重复的`/`符号的支持。
|
||||
- `Pprof`功能路由支持`Domain`绑定。
|
||||
- 其他一些细节改进。
|
||||
- `Cookie`
|
||||
- 增加`SetHttpCookie`方法,用于根据标准库`http.Cookie`对象设置`Cookie`。
|
||||
- 其他一些功能改进
|
||||
|
||||
|
||||
## `database`
|
||||
1. `gdb`
|
||||
- 新增模型关联实验特性:https://goframe.org/database/gdb/model/association
|
||||
- 改进时间自动更新特性增加自定义时间字段:https://goframe.org/database/gdb/model/auto-time
|
||||
- 新增基于`Redis`的`SQL`缓存适配器:https://goframe.org/database/gdb/model/cache
|
||||
- 新增对输入参数的键名-字段名自动识别映射特性:https://goframe.org/database/gdb/senior
|
||||
- 新增`DB.HasTable`方法,用于判断是否当前数据库存在指定数据表。
|
||||
- 新增`Model.HasField`方法,用于判断是否当前数据表存在指定字段。
|
||||
- 新增`Model.ScanList`方法,用于智能地将当前`struct`/`slice`绑定到指定的`list`对应属性上。
|
||||
- 新增`Result.MapKeyValue`方法,用于将当前`Result`转换为`map[string]Value`类型。
|
||||
- 新增`Result.IsEmpty/Len/Size/ScanList`方法。
|
||||
- 增加`ListItemValues`及`ListItemValuesUnique`方法,用于自动获取`list`中指定名称的键值或属性值,构成`slice`返回。
|
||||
- `SQL`日志内容增加分组名称打印。
|
||||
- 改进`DataToMapDeep`方法。
|
||||
- 其他一些细节改进工作。
|
||||
|
||||
1. `gredis`
|
||||
- 新增`TLS`特性支持,并支持配置文件配置,https://goframe.org/database/gredis/config
|
||||
|
||||
## `container`
|
||||
1. `gvar`
|
||||
- 增加`Scan`及`ScanDeep`方法,用于`struct`/`slice`自动识别转换。
|
||||
- 增加`ListItemValues`及`ListItemValuesUnique`方法,用于自动获取`list`中指定名称的键值或属性值,构成`slice`返回。
|
||||
- 增加`MapStrAny`接口实现方法。
|
||||
|
||||
## `os`
|
||||
1. `gcache`
|
||||
- 增加`GetVar`方法,用于获取缓存数据并返回为泛型对象。
|
||||
- 增加`Update`方法,用于仅修改缓存数值,不修改缓存过期时间。
|
||||
- 增加`UpdateExpire`方法,用于仅修改缓存过期时间,不修改缓存数值。
|
||||
- 重构底层设计,增加适配器设计模式,并增加内存及`Redis`适配器支持。其中内存适配器默认核心模块提供,`Redis`适配器由社区模块提供:https://goframe.org/os/gcache/adapter
|
||||
- 注意,本次模块的修改会有部分方法不兼容,部分方法增加了`error`参数返回,升级时请注意查看。编译时将不会通过。
|
||||
- 其他一些功能改进。
|
||||
1. `gfile`
|
||||
- 增加`ScanDirFileFunc`方法,用于自定义函数处理的递归目录文件遍历。
|
||||
- 改进`Scan*`方法,增加递归层级限制,默认层级限制为`100000`.
|
||||
|
||||
1. `gfsnotify`
|
||||
- 去掉模块初始化时的`Watcher`对象创建,调整为运行时按需创建,并且增加了并发安全控制。
|
||||
|
||||
1. `grpool`
|
||||
- 增加`AddWithRecover`方法,用于添加异步任务时给定一个`recover`处理方法,当任务`panic`时交由该`recover`方法处理,防止异步任务`panic`引起整个进程崩溃。
|
||||
> 这里解决的痛点是`recover`只能捕获到当前`goroutine`的`panic`,因此只能在创建异步任务的时候指定`recover`处理方法。
|
||||
|
||||
1. `gtime`
|
||||
- 增加`ParseDuration`方法,增加了对时间单位`d`的支持,表示天。
|
||||
- 改进`New`方法,支持通过字符串、时间戳、`time.Time`对象创建`gtime.Time`对象,https://goframe.org/os/gtime/time
|
||||
- 改进`Add/AddStr/ToLocation/ToZone/UTCLocal/AddDate/Truncate/Round`方法,这些方法调用时,不再修改当前对象本身,而是创建并返回一个新的`gtine.Time`对象,以便保证和标准库`time.Time`的逻辑一致,防止混淆。
|
||||
- 其他一些细节改进。
|
||||
1. `gtimer`
|
||||
- 增加`Reset`方法,用于重置定时任务的计时。
|
||||
1. `gfcache`
|
||||
- 去掉了该模块,该模块的功能作用不是特别大。
|
||||
|
||||
## `debug`
|
||||
1. `gdebug`
|
||||
- 新增`GoroutineId`方法,用于获取当前执行的`goroutine id`,仅作调试使用。
|
||||
|
||||
## `encoding`
|
||||
1. `gjson`
|
||||
- 新增`GetScan/GetScanDeep`方法。
|
||||
- 新增`ToScan/ToScanDeep`方法。
|
||||
- 新增`LoadContentType`方法,用于根据指定类型的内容创建`Json`操作对象。
|
||||
- 新增`IsValidDataType`方法,用于判断给定的数据类型是否支持解析。
|
||||
- 其他一些改进。
|
||||
- 单元测试完善。
|
||||
|
||||
1. `gcompress`
|
||||
- 新增`GzipFile/UnGzipFile`基于`gzip`压缩算法的文件压缩/解压。
|
||||
|
||||
## `i18n`
|
||||
1. `gi18n`
|
||||
- 新增`TranslateFormat/TranslateFormatLang/Tf/Tfl`方法: https://goframe.org/i18n/gi18n/index
|
||||
|
||||
## `text`
|
||||
1. `gstr`
|
||||
- 增加`SnakeFirstUpperCase`方法,用于在字母大写前增加连接符,并不会处理数字,例如:`SnakeFirstUpperCase("RGBCodeMd5")`将会返回`rgb_code_md5`。
|
||||
|
||||
## `util`
|
||||
1. `gconv`
|
||||
- 增加对指针基本类型的转换支持。
|
||||
- 增加`Scan/ScanDeep`方法,用于自动识别转换`Struct/[]Struct`。
|
||||
- 改进`MapDeep`方法的层级递归处理。
|
||||
- 其他一些细节改进,性能改进。
|
||||
|
||||
1. `gutil`
|
||||
- 增加`ListItemValues`及`ListItemValuesUnique`方法,用于自动获取`list`中指定名称的键值或属性值,构成`slice`返回。
|
||||
- 增加`ItemValue`方法,用于获取指定`map/*map/struct/*struct`类型的键值/属性值。
|
||||
- 增加`MapOmitEmpty`方法,用于过滤`map`中的空值。
|
||||
- 增加`SliceDelete`方法,用于数组项删除。
|
||||
- 增加`Try`方法,通过闭包执行给定的方法,如果方法产生`panic`则该方法返回`error`,否则返回`nil`。
|
||||
- 改进`TryCatch(try func(), catch ...func(exception interface{}))`为`TryCatch(try func(), catch ...func(exception error))`
|
||||
|
||||
1. `gvalid`
|
||||
- 增加自定义规则特性,开发者可注册自定义的校验规则:https://goframe.org/util/gvalid/customrule
|
||||
- 其他一些功能改进。
|
||||
|
||||
## `error`
|
||||
1. `gerror`
|
||||
- 新增`Current`方法,用于获取当前错误层级的`error`接口对象。
|
||||
- 新增`Next`方法,用于获取层级错误的下一级错误`error`接口对象。当下一层级不存在时,返回`nil`。
|
||||
- 文档更新:https://goframe.org/errors/gerror/index
|
||||
|
||||
## Bug Fix
|
||||
1. 修复`garray`模块的`Unique`方法问题。
|
||||
1. 修复`glog`中定时器懒初始化时的`goroutine`泄露问题。
|
||||
1. 修复`gstr`中名称`Case`转换相关方法在名称中带有数字+特殊字符时的名称转换问题。
|
||||
1. 修复`ghttp`模块中的`CORS`跨域设置`Header`细节问题。
|
||||
1. 其他BUG修复:https://github.com/gogf/gf/issues?q=is%3Aissue+label%3Abug+is%3Aclosed
|
||||
|
||||
|
||||
# `v1.13.1` (2020-06-10)
|
||||
|
||||
# GoFrame
|
||||
|
||||
`GF(Go Frame)`是一款模块化、高性能、生产级的Go基础开发框架。实现了比较完善的基础设施建设以及开发工具链,提供了常用的基础开发模块,如:缓存、日志、队列、数组、集合、容器、定时器、命令行、内存锁、对象池、配置管理、资源管理、数据校验、数据编码、定时任务、数据库ORM、TCP/UDP组件、进程管理/通信等等。并提供了Web服务开发的系列核心组件,如:Router、Cookie、Session、Middleware、服务注册、模板引擎等等,支持热重启、热更新、域名绑定、TLS/HTTPS、Rewrite等特性。
|
||||
|
||||
## 特点
|
||||
|
||||
* 模块化、松耦合设计;
|
||||
* 模块丰富、开箱即用;
|
||||
* 简便易用、易于维护;
|
||||
* 高代码质量、高单元测试覆盖率;
|
||||
* 社区活跃,大牛谦逊低调脾气好;
|
||||
* 详尽的开发文档及示例;
|
||||
* 完善的本地中文化支持;
|
||||
* 设计为团队及企业使用;
|
||||
|
||||
## 发展
|
||||
|
||||
`GoFrame`开始得比较早,`2011`年始于北京一个智能物联网平台项目,那时还没有这么多物联网的现行标准,`Go`的标准库以及生态也未如此丰富。`2017`年的时候`GF`才开始发布测试版,`2018`年`1024`程序员节日的时候才发布`v1.0`正式版,为`Go`生态发展添砖加瓦。开源以来快速迭代、发展成长,广受开发者和企业的青睐,也有许多的开发者加入了贡献行列。`GF`原本是为开发团队设计的,因此她的开发效率和可维护性做得非常好,有着很高的代码质量以及丰富的单元测试和示例,并且`GF`是目前中文化文档做的最好的`Golang`开发框架。
|
||||
|
||||
# Change Log
|
||||
|
||||
1. 应多数开发者的要求,框架要求的最低`Golang`运行版本降级为了`v1.11`。
|
||||
1. 新增`GoFrame`视频教程地址:
|
||||
- bilibili:https://www.bilibili.com/video/av94410029
|
||||
- 西瓜视频: https://www.ixigua.com/pseries/6809291194665796100/
|
||||
1. 将不常用的`guuid`模块迁移到 github.com/gogf/guuid 作为社区模块维护,保持`gf`主仓库的轻量级。
|
||||
1. 新增`guid`模块,用于高效轻量级的唯一字符串生成:https://goframe.org/util/guid/index
|
||||
|
||||
|
||||
## `tool chain`
|
||||
|
||||
1. 工具链更新:https://goframe.org/toolchain/cli
|
||||
1. 新增`gf env`命令,更优雅地查看当前`Golang`环境变量信息。
|
||||
1. 新增`gf mod path`命令,用于将当前`go modules`包拷贝到`GOPATH`中,以便使用原始的`GOPATH`方式开发项目。
|
||||
1. 对现有`cli`命令进行了一些改进,提高使用体验;预编译二进制版本在部分平台下提供了`upx`压缩,使得下载的文件更小。
|
||||
|
||||
## `container`
|
||||
1. `garray`
|
||||
- https://goframe.org/container/garray/index
|
||||
- 简化数组使用方式,支持类似于`var garray.Array`的变量定义使用方式;
|
||||
- 增加`Walk`方法,用于自定义的数组元素处理方法;
|
||||
- 增加`ContainsI`方法,用于大小写忽略匹配的数组元素项存在性查找;
|
||||
- 完善单元测试,代码覆盖率`94%`;
|
||||
- 代码改进,提高性能;
|
||||
- 修复一些问题;
|
||||
1. `gchan`
|
||||
- 由于该封装包实际意义不是很大,因此从主框架中删除;
|
||||
1. `glist`
|
||||
- https://goframe.org/container/glist/index
|
||||
- 简化链表使用方式,支持类似于`var glist.List`的变量定义使用方式;
|
||||
- 完善单元测试,代码覆盖率`99%`;
|
||||
1. `gmap`
|
||||
- https://goframe.org/container/gmap/index
|
||||
- 简化`Map`使用方式,支持类似于`var gmap.Map`的变量定义使用方式;
|
||||
- 完善单元测试,代码覆盖率`81%`;
|
||||
- 代码改进,提高性能;
|
||||
1. `gset`
|
||||
- https://goframe.org/container/gset/index
|
||||
- 简化集合使用方式,支持类似于`var gset.Set`的变量定义使用方式;
|
||||
- 增加`Walk`方法,用于自定义的集合元素处理方法;
|
||||
- 完善单元测试,代码覆盖率`90%`;
|
||||
- 代码改进,提高性能;
|
||||
1. `gtree`
|
||||
- https://goframe.org/container/gtree/index
|
||||
- 简化树型使用方式,支持类似于`var gtree.BTree`的变量定义使用方式;
|
||||
- 完善单元测试,代码覆盖率`90%`;
|
||||
1. `gvar`
|
||||
- https://goframe.org/container/gvar/index
|
||||
- 完善单元测试,代码覆盖率`69%`;
|
||||
- 代码组织结构调整,提高维护性;
|
||||
- 代码改进,提高性能;
|
||||
|
||||
## `database`
|
||||
|
||||
1. `gdb`
|
||||
- 增加`Transaction(f func(tx *TX) error) (err error)`接口方法,用于通过闭包实现事务封装处理:https://goframe.org/database/gdb/transaction
|
||||
- 去掉不常用的`From`接口方法,改进`Table`及`Model`方法的参数为不定参数,并支持通过不定参数传递表别名:https://goframe.org/database/gdb/chaining/select
|
||||
- 增加`DryRun`特性,支持空跑时只执行查询不执行写入/更新/删除操作:https://goframe.org/database/gdb/senior
|
||||
- 增加`create_at`, `update_at`写入时间、更新时间字段自动填充特性:https://goframe.org/database/gdb/chaining/auto-time
|
||||
- 增加`delete_at`软删除特性:https://goframe.org/database/gdb/chaining/auto-time
|
||||
- 增加`Having`链式操作方法,用于`having`条件查询:https://goframe.org/database/gdb/chaining/select
|
||||
- `Result`结果对象增加`Chunk`方法,用于自定义的数据分批处理:https://goframe.org/database/gdb/result
|
||||
- 改进`Schema`数据库运行时切换特性;
|
||||
- 改进对`pgsql`, `mssql`, `sqlite`, `oracle`数据库字段类型的支持;
|
||||
- 进一步完善单元测试;
|
||||
- 代码组织结构调整,提高维护性;
|
||||
- 代码改进,提高性能;
|
||||
1. `gredis`
|
||||
- 增加`MaxActive`连接池参数默认配置为`100`,限制默认的连接数量;
|
||||
- 改进`Conn`连接对象的`Do`方法,支持对`map/slice/struct`类型进行自动的`json.Marshal`处理,注意获取数据时使用`DoVar`方法获取:https://goframe.org/database/gredis/usage
|
||||
- 完善单元测试,代码覆盖率`72%`;
|
||||
|
||||
## `net`
|
||||
1. `ghttp`
|
||||
- 增加`Prefix`及`Retry`客户端链式操作方法;
|
||||
- 增加客户端原始请求打印特性:https://goframe.org/net/ghttp/client/demo/dump
|
||||
- 增加`ClientMaxBodySize`的服务端配置,用于限制客户端提交的`Body`大小,默认为`8MB`;在涉及到上传的Server中需要增加该配置的大小,在配置文件中指定对应的大小即可,如`ClientMaxBodySize="100MB"`:https://goframe.org/net/ghttp/config
|
||||
- 改进`SessionId`生成的随机性,提高`Session`安全性:https://goframe.org/os/gsession/index
|
||||
- 改进`ghttp.Server`实现了标准库的`http.Handler`接口,便于与其他第三方的服务如`Prometheus`进行代码集成;
|
||||
- 其他大量的代码细节改进工作,提高性能及持久维护性;
|
||||
- 完善单元测试,代码覆盖率`61%`;
|
||||
|
||||
1. `gipv4`
|
||||
- 增加`GetIpArray`方法,用于获取当前主机的所有IPv4地址;
|
||||
- 增加`GetMacArray`及`GetMac`方法,用于获取当前主机的`MAC`地址信息;
|
||||
- 修改`IntranetIP`方法名称为`GetIntranetIp`,修改`IntranetIPArray`方法名称为`GetIntranetIpArray`;
|
||||
|
||||
## `encoding`
|
||||
1. `gjson`
|
||||
- 新增`GetMaps`获取`JSON`内部节点变量方法;
|
||||
- 改进`NewWithTag`方法对`map/struct`的处理;
|
||||
- 完善单元测试,代码覆盖率`77%`;
|
||||
1. `gyaml`
|
||||
- 升级依赖的第三方`yaml`解析包,解决了`map[interface{}]interface{}`转换问题;
|
||||
|
||||
## `error`
|
||||
1. `gerror`
|
||||
- 新增`NewfSkip`方法,用于创建`skip`指定堆栈的错误对象;
|
||||
- 放开框架所有的堆栈链路打印,展示错误时真实的链路调用详情;
|
||||
|
||||
## `os`
|
||||
1. `gcache`
|
||||
- 增加`GetVar`方法,用于获得可以便捷转换为其他数据类型的"泛型"变量;
|
||||
- 标记`Removes`方法废弃,改进`Remove`方法参数为不定参数,统一使用`Remove`方法删除单个/多个键值对;
|
||||
- 完善单元测试,代码覆盖率`96%`;
|
||||
|
||||
1. `genv`
|
||||
- 增加`GetVar`方法,用于获得可以便捷转换为其他数据类型的"泛型"变量;
|
||||
|
||||
1. `gfile`
|
||||
- 改进`CopyDir/CopyFile`复制目录/文件方法;
|
||||
- 新增`ScanDirFunc`方法,用于支持自定义处理回调的目录检索;
|
||||
- 完善单元测试,代码覆盖率`64%`;
|
||||
|
||||
1. `glog`
|
||||
- 增加支持`Context`上下文变量的日志打印特性:https://goframe.org/os/glog/context
|
||||
|
||||
1. `gres`
|
||||
- 改进打包特性,增强生成二进制文件及Go文件的压缩比,比旧版本增加`20%`压缩率,使得编译生成的二进制文件体积更小;
|
||||
- 代码结构改进,提高执行效率及可持久维护性;
|
||||
|
||||
1. `gsession`
|
||||
- 改进`SessionId`默认生成方法,采用`guid.S`方法生成;
|
||||
- 增加`SetId`及`SetIdFunc`方法,用于自定义`SessionId`及自定义的`SessionId`生成方法;
|
||||
|
||||
## `frame`
|
||||
1. `g`
|
||||
- 新增`g.Table`方法,用于快速创建数据库模型操作对象;
|
||||
|
||||
## `i18n`
|
||||
1. `gi18n`
|
||||
- 新增`GetContent`方法,用于获取指定`i18n`关键字为转译内容;
|
||||
- 改进代码细节,提高性能和持久可维护性;
|
||||
- 完善单元测试,代码覆盖率`74%`;
|
||||
|
||||
## `test`
|
||||
1. `gtest`
|
||||
- 增加`AssertNQ`断言方法,用于强类型的不相等判断;
|
||||
|
||||
## `text`
|
||||
1. `gstr`
|
||||
- 增加`SubStrRune`方法,用于支持`unicode`的字符串截取;
|
||||
- 增加`StrLimitRune`方法,用于支持`unicode`的字符串截断隐藏;
|
||||
- 增加`LenRune`方法,用于替换`RuneLen`方法,统一方法命名风格;
|
||||
- 增加`PosRune/PosIRune/PosRRune/PosRIRune`方法,用于支持`unicode`的字符串左右位置查找;
|
||||
- 增加`CompareVersionGo`方法,用于`Golang`风格的版本号大小比较;
|
||||
- 完善单元测试,代码覆盖率`75%`;
|
||||
|
||||
## `util`
|
||||
1. `gconv`
|
||||
- 改进`Convert`转换方法,支持常见`map`类型的转换;
|
||||
- 改进类型转换过程中异常错误的捕获,通过`error`返回;
|
||||
- 其他一些细节改进;
|
||||
- 完善单元测试,代码覆盖率`63%`;
|
||||
|
||||
1. `grand`
|
||||
- 增加`B`方法,用于获得随机的二进制数据;
|
||||
- 改进代码底层实现,部分接口性能提高`50%`;
|
||||
- 完善单元测试,代码覆盖率`74%`;
|
||||
|
||||
1. `guid`
|
||||
- 新增`guid`模块,用于高效轻量级的唯一字符串生成:https://goframe.org/util/guid/index
|
||||
|
||||
1. `gutil`
|
||||
- 增加`MapContains`方法,用于判断map中是否包含指定键名;
|
||||
- 增加`MapDelete`方法,用于删除map中指定的键名,可以为多个键名;
|
||||
- 增加`MapMerge`方法,用于合并两个map;
|
||||
- 增加`MapMergeCopy`方法,用于拷贝多个map;
|
||||
- 增加`MapContainsPossibleKey`方法,用于查找指定键名,忽略大小写及字符`'-'/'_'/'.'/' '`;
|
||||
|
||||
1. `gvalid`
|
||||
- 所有默认的错误提示改为了英文;
|
||||
- 错误提示的配置改为了通过`i18n`来配置实现,以便支持国际化:https://goframe.org/util/gvalid/message
|
||||
- 身份证号规则名称从`id-number`改为了`resident-id `;
|
||||
- 银行卡号规则名称从`luhn`改为了`bank-card`;
|
||||
- 完善单元测试,代码覆盖率`96%`;
|
||||
|
||||
## Bug Fix
|
||||
1. 修复`gcompress`的多文件`zip`压缩问题;
|
||||
1. 修复`ghttp.Client`获取返回的过期`Cookie`的问题;
|
||||
1. 修复`gres.File`对于`http.File`接口的实现细节;
|
||||
1. 修复`garray.Pop*`方法的边界问题;
|
||||
1. 修复`gres`中`Readdir`方法参数为`0`时报错的问题;
|
||||
1. 其他一些修复:https://github.com/gogf/gf/issues?q=is%3Aissue+label%3Abug
|
||||
|
||||
|
||||
|
||||
|
||||
# `v1.12.1` (2020-03-31)
|
||||
|
||||
大家好啊!久等啦!
|
||||
|
||||
@ -4,5 +4,5 @@
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
// Package garray provides concurrent-safe/unsafe arrays.
|
||||
// Package garray provides most commonly used array containers which also support concurrent-safe/unsafe switch feature.
|
||||
package garray
|
||||
|
||||
@ -8,10 +8,10 @@ package garray
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/internal/empty"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"math"
|
||||
"sort"
|
||||
@ -22,6 +22,8 @@ import (
|
||||
)
|
||||
|
||||
// Array is a golang array with rich features.
|
||||
// It contains a concurrent-safe/unsafe switch, which should be set
|
||||
// when its initialization and cannot be changed then.
|
||||
type Array struct {
|
||||
mu rwmutex.RWMutex
|
||||
array []interface{}
|
||||
@ -492,9 +494,11 @@ func (a *Array) Search(value interface{}) int {
|
||||
func (a *Array) Unique() *Array {
|
||||
a.mu.Lock()
|
||||
for i := 0; i < len(a.array)-1; i++ {
|
||||
for j := i + 1; j < len(a.array); j++ {
|
||||
for j := i + 1; j < len(a.array); {
|
||||
if a.array[i] == a.array[j] {
|
||||
a.array = append(a.array[:j], a.array[j+1:]...)
|
||||
} else {
|
||||
j++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,9 +8,9 @@ package garray
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"math"
|
||||
"sort"
|
||||
|
||||
@ -20,6 +20,8 @@ import (
|
||||
)
|
||||
|
||||
// IntArray is a golang int array with rich features.
|
||||
// It contains a concurrent-safe/unsafe switch, which should be set
|
||||
// when its initialization and cannot be changed then.
|
||||
type IntArray struct {
|
||||
mu rwmutex.RWMutex
|
||||
array []int
|
||||
@ -508,9 +510,11 @@ func (a *IntArray) Search(value int) int {
|
||||
func (a *IntArray) Unique() *IntArray {
|
||||
a.mu.Lock()
|
||||
for i := 0; i < len(a.array)-1; i++ {
|
||||
for j := i + 1; j < len(a.array); j++ {
|
||||
for j := i + 1; j < len(a.array); {
|
||||
if a.array[i] == a.array[j] {
|
||||
a.array = append(a.array[:j], a.array[j+1:]...)
|
||||
} else {
|
||||
j++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,9 +8,9 @@ package garray
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"math"
|
||||
"sort"
|
||||
@ -22,6 +22,8 @@ import (
|
||||
)
|
||||
|
||||
// StrArray is a golang string array with rich features.
|
||||
// It contains a concurrent-safe/unsafe switch, which should be set
|
||||
// when its initialization and cannot be changed then.
|
||||
type StrArray struct {
|
||||
mu rwmutex.RWMutex
|
||||
array []string
|
||||
@ -512,9 +514,11 @@ func (a *StrArray) Search(value string) int {
|
||||
func (a *StrArray) Unique() *StrArray {
|
||||
a.mu.Lock()
|
||||
for i := 0; i < len(a.array)-1; i++ {
|
||||
for j := i + 1; j < len(a.array); j++ {
|
||||
for j := i + 1; j < len(a.array); {
|
||||
if a.array[i] == a.array[j] {
|
||||
a.array = append(a.array[:j], a.array[j+1:]...)
|
||||
} else {
|
||||
j++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,9 +8,9 @@ package garray
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/internal/empty"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"github.com/gogf/gf/util/gutil"
|
||||
"math"
|
||||
@ -22,7 +22,10 @@ import (
|
||||
)
|
||||
|
||||
// SortedArray is a golang sorted array with rich features.
|
||||
// It's using increasing order in default.
|
||||
// It is using increasing order in default, which can be changed by
|
||||
// setting it a custom comparator.
|
||||
// It contains a concurrent-safe/unsafe switch, which should be set
|
||||
// when its initialization and cannot be changed then.
|
||||
type SortedArray struct {
|
||||
mu rwmutex.RWMutex
|
||||
array []interface{}
|
||||
@ -443,7 +446,7 @@ func (a *SortedArray) binSearch(value interface{}, lock bool) (index int, result
|
||||
mid := 0
|
||||
cmp := -2
|
||||
for min <= max {
|
||||
mid = (min + max) / 2
|
||||
mid = min + int((max-min)/2)
|
||||
cmp = a.getComparator()(value, a.array[mid])
|
||||
switch {
|
||||
case cmp < 0:
|
||||
|
||||
@ -8,8 +8,8 @@ package garray
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"math"
|
||||
"sort"
|
||||
|
||||
@ -19,7 +19,10 @@ import (
|
||||
)
|
||||
|
||||
// SortedIntArray is a golang sorted int array with rich features.
|
||||
// It's using increasing order in default.
|
||||
// It is using increasing order in default, which can be changed by
|
||||
// setting it a custom comparator.
|
||||
// It contains a concurrent-safe/unsafe switch, which should be set
|
||||
// when its initialization and cannot be changed then.
|
||||
type SortedIntArray struct {
|
||||
mu rwmutex.RWMutex
|
||||
array []int
|
||||
@ -440,7 +443,7 @@ func (a *SortedIntArray) binSearch(value int, lock bool) (index int, result int)
|
||||
mid := 0
|
||||
cmp := -2
|
||||
for min <= max {
|
||||
mid = (min + max) / 2
|
||||
mid = min + int((max-min)/2)
|
||||
cmp = a.getComparator()(value, a.array[mid])
|
||||
switch {
|
||||
case cmp < 0:
|
||||
|
||||
@ -8,7 +8,7 @@ package garray
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"math"
|
||||
"sort"
|
||||
@ -20,7 +20,10 @@ import (
|
||||
)
|
||||
|
||||
// SortedStrArray is a golang sorted string array with rich features.
|
||||
// It's using increasing order in default.
|
||||
// It is using increasing order in default, which can be changed by
|
||||
// setting it a custom comparator.
|
||||
// It contains a concurrent-safe/unsafe switch, which should be set
|
||||
// when its initialization and cannot be changed then.
|
||||
type SortedStrArray struct {
|
||||
mu rwmutex.RWMutex
|
||||
array []string
|
||||
@ -442,7 +445,7 @@ func (a *SortedStrArray) binSearch(value string, lock bool) (index int, result i
|
||||
mid := 0
|
||||
cmp := -2
|
||||
for min <= max {
|
||||
mid = (min + max) / 2
|
||||
mid = min + int((max-min)/2)
|
||||
cmp = a.getComparator()(value, a.array[mid])
|
||||
switch {
|
||||
case cmp < 0:
|
||||
|
||||
281
container/garray/garray_z_example_any_test.go
Normal file
281
container/garray/garray_z_example_any_test.go
Normal file
@ -0,0 +1,281 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package garray_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
|
||||
"github.com/gogf/gf/container/garray"
|
||||
)
|
||||
|
||||
func ExampleNew() {
|
||||
// A normal array.
|
||||
a := garray.New()
|
||||
|
||||
// Adding items.
|
||||
for i := 0; i < 10; i++ {
|
||||
a.Append(i)
|
||||
}
|
||||
|
||||
// Print the array length.
|
||||
fmt.Println(a.Len())
|
||||
|
||||
// Print the array items.
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// Retrieve item by index.
|
||||
fmt.Println(a.Get(6))
|
||||
|
||||
// Check item existence.
|
||||
fmt.Println(a.Contains(6))
|
||||
fmt.Println(a.Contains(100))
|
||||
|
||||
// Insert item before specified index.
|
||||
a.InsertAfter(9, 11)
|
||||
// Insert item after specified index.
|
||||
a.InsertBefore(10, 10)
|
||||
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// Modify item by index.
|
||||
a.Set(0, 100)
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// Search item and return its index.
|
||||
fmt.Println(a.Search(5))
|
||||
|
||||
// Remove item by index.
|
||||
a.Remove(0)
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// Empty the array, removes all items of it.
|
||||
fmt.Println(a.Slice())
|
||||
a.Clear()
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// Output:
|
||||
// 10
|
||||
// [0 1 2 3 4 5 6 7 8 9]
|
||||
// 6 true
|
||||
// true
|
||||
// false
|
||||
// [0 1 2 3 4 5 6 7 8 9 10 11]
|
||||
// [100 1 2 3 4 5 6 7 8 9 10 11]
|
||||
// 5
|
||||
// [1 2 3 4 5 6 7 8 9 10 11]
|
||||
// [1 2 3 4 5 6 7 8 9 10 11]
|
||||
// []
|
||||
}
|
||||
|
||||
func ExampleArray_Iterator() {
|
||||
array := garray.NewArrayFrom(g.Slice{"a", "b", "c"})
|
||||
// Iterator is alias of IteratorAsc, which iterates the array readonly in ascending order
|
||||
// with given callback function <f>.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
array.Iterator(func(k int, v interface{}) bool {
|
||||
fmt.Println(k, v)
|
||||
return true
|
||||
})
|
||||
// IteratorDesc iterates the array readonly in descending order with given callback function <f>.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
array.IteratorDesc(func(k int, v interface{}) bool {
|
||||
fmt.Println(k, v)
|
||||
return true
|
||||
})
|
||||
|
||||
// Output:
|
||||
// 0 a
|
||||
// 1 b
|
||||
// 2 c
|
||||
// 2 c
|
||||
// 1 b
|
||||
// 0 a
|
||||
}
|
||||
|
||||
func ExampleArray_Reverse() {
|
||||
array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})
|
||||
|
||||
// Reverse makes array with elements in reverse order.
|
||||
fmt.Println(array.Reverse().Slice())
|
||||
|
||||
// Output:
|
||||
// [9 8 7 6 5 4 3 2 1]
|
||||
}
|
||||
|
||||
func ExampleArray_Shuffle() {
|
||||
array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})
|
||||
|
||||
// Shuffle randomly shuffles the array.
|
||||
fmt.Println(array.Shuffle().Slice())
|
||||
}
|
||||
|
||||
func ExampleArray_Rands() {
|
||||
array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})
|
||||
|
||||
// Randomly retrieve and return 2 items from the array.
|
||||
// It does not delete the items from array.
|
||||
fmt.Println(array.Rands(2))
|
||||
|
||||
// Randomly pick and return one item from the array.
|
||||
// It deletes the picked up item from array.
|
||||
fmt.Println(array.PopRand())
|
||||
}
|
||||
|
||||
func ExampleArray_PopRand() {
|
||||
array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})
|
||||
|
||||
// Randomly retrieve and return 2 items from the array.
|
||||
// It does not delete the items from array.
|
||||
fmt.Println(array.Rands(2))
|
||||
|
||||
// Randomly pick and return one item from the array.
|
||||
// It deletes the picked up item from array.
|
||||
fmt.Println(array.PopRand())
|
||||
}
|
||||
|
||||
func ExampleArray_Join() {
|
||||
array := garray.NewFrom(g.Slice{"a", "b", "c", "d"})
|
||||
fmt.Println(array.Join(","))
|
||||
|
||||
// Output:
|
||||
// a,b,c,d
|
||||
}
|
||||
|
||||
func ExampleArray_Chunk() {
|
||||
array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})
|
||||
|
||||
// Chunk splits an array into multiple arrays,
|
||||
// the size of each array is determined by <size>.
|
||||
// The last chunk may contain less than size elements.
|
||||
fmt.Println(array.Chunk(2))
|
||||
|
||||
// Output:
|
||||
// [[1 2] [3 4] [5 6] [7 8] [9]]
|
||||
}
|
||||
|
||||
func ExampleArray_PopLeft() {
|
||||
array := garray.NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9})
|
||||
|
||||
// Any Pop* functions pick, delete and return the item from array.
|
||||
|
||||
fmt.Println(array.PopLeft())
|
||||
fmt.Println(array.PopLefts(2))
|
||||
fmt.Println(array.PopRight())
|
||||
fmt.Println(array.PopRights(2))
|
||||
|
||||
// Output:
|
||||
// 1 true
|
||||
// [2 3]
|
||||
// 9 true
|
||||
// [7 8]
|
||||
}
|
||||
|
||||
func ExampleArray_PopLefts() {
|
||||
array := garray.NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9})
|
||||
|
||||
// Any Pop* functions pick, delete and return the item from array.
|
||||
|
||||
fmt.Println(array.PopLeft())
|
||||
fmt.Println(array.PopLefts(2))
|
||||
fmt.Println(array.PopRight())
|
||||
fmt.Println(array.PopRights(2))
|
||||
|
||||
// Output:
|
||||
// 1 true
|
||||
// [2 3]
|
||||
// 9 true
|
||||
// [7 8]
|
||||
}
|
||||
|
||||
func ExampleArray_PopRight() {
|
||||
array := garray.NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9})
|
||||
|
||||
// Any Pop* functions pick, delete and return the item from array.
|
||||
|
||||
fmt.Println(array.PopLeft())
|
||||
fmt.Println(array.PopLefts(2))
|
||||
fmt.Println(array.PopRight())
|
||||
fmt.Println(array.PopRights(2))
|
||||
|
||||
// Output:
|
||||
// 1 true
|
||||
// [2 3]
|
||||
// 9 true
|
||||
// [7 8]
|
||||
}
|
||||
|
||||
func ExampleArray_PopRights() {
|
||||
array := garray.NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9})
|
||||
|
||||
// Any Pop* functions pick, delete and return the item from array.
|
||||
|
||||
fmt.Println(array.PopLeft())
|
||||
fmt.Println(array.PopLefts(2))
|
||||
fmt.Println(array.PopRight())
|
||||
fmt.Println(array.PopRights(2))
|
||||
|
||||
// Output:
|
||||
// 1 true
|
||||
// [2 3]
|
||||
// 9 true
|
||||
// [7 8]
|
||||
}
|
||||
|
||||
func ExampleArray_Contains() {
|
||||
var array garray.StrArray
|
||||
array.Append("a")
|
||||
fmt.Println(array.Contains("a"))
|
||||
fmt.Println(array.Contains("A"))
|
||||
fmt.Println(array.ContainsI("A"))
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
// true
|
||||
}
|
||||
|
||||
func ExampleArray_Merge() {
|
||||
array1 := garray.NewFrom(g.Slice{1, 2})
|
||||
array2 := garray.NewFrom(g.Slice{3, 4})
|
||||
slice1 := g.Slice{5, 6}
|
||||
slice2 := []int{7, 8}
|
||||
slice3 := []string{"9", "0"}
|
||||
fmt.Println(array1.Slice())
|
||||
array1.Merge(array1)
|
||||
array1.Merge(array2)
|
||||
array1.Merge(slice1)
|
||||
array1.Merge(slice2)
|
||||
array1.Merge(slice3)
|
||||
fmt.Println(array1.Slice())
|
||||
|
||||
// Output:
|
||||
// [1 2]
|
||||
// [1 2 1 2 3 4 5 6 7 8 9 0]
|
||||
}
|
||||
|
||||
func ExampleArray_FilterEmpty() {
|
||||
array1 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"})
|
||||
array2 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"})
|
||||
fmt.Printf("%#v\n", array1.FilterNil().Slice())
|
||||
fmt.Printf("%#v\n", array2.FilterEmpty().Slice())
|
||||
|
||||
// Output:
|
||||
// []interface {}{0, 1, 2, "", []interface {}{}, "john"}
|
||||
// []interface {}{1, 2, "john"}
|
||||
}
|
||||
|
||||
func ExampleArray_FilterNil() {
|
||||
array1 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"})
|
||||
array2 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"})
|
||||
fmt.Printf("%#v\n", array1.FilterNil().Slice())
|
||||
fmt.Printf("%#v\n", array2.FilterEmpty().Slice())
|
||||
|
||||
// Output:
|
||||
// []interface {}{0, 1, 2, "", []interface {}{}, "john"}
|
||||
// []interface {}{1, 2, "john"}
|
||||
}
|
||||
28
container/garray/garray_z_example_str_test.go
Normal file
28
container/garray/garray_z_example_str_test.go
Normal file
@ -0,0 +1,28 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package garray_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/garray"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func ExampleStrArray_Walk() {
|
||||
var array garray.StrArray
|
||||
tables := g.SliceStr{"user", "user_detail"}
|
||||
prefix := "gf_"
|
||||
array.Append(tables...)
|
||||
// Add prefix for given table names.
|
||||
array.Walk(func(value string) string {
|
||||
return prefix + value
|
||||
})
|
||||
fmt.Println(array.Slice())
|
||||
|
||||
// Output:
|
||||
// [gf_user gf_user_detail]
|
||||
}
|
||||
@ -1,132 +0,0 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package garray_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
|
||||
"github.com/gogf/gf/container/garray"
|
||||
)
|
||||
|
||||
func Example_basic() {
|
||||
// A normal array.
|
||||
a := garray.New()
|
||||
|
||||
// Adding items.
|
||||
for i := 0; i < 10; i++ {
|
||||
a.Append(i)
|
||||
}
|
||||
|
||||
// Print the array length.
|
||||
fmt.Println(a.Len())
|
||||
|
||||
// Print the array items.
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// Retrieve item by index.
|
||||
fmt.Println(a.Get(6))
|
||||
|
||||
// Check item existence.
|
||||
fmt.Println(a.Contains(6))
|
||||
fmt.Println(a.Contains(100))
|
||||
|
||||
// Insert item before specified index.
|
||||
a.InsertAfter(9, 11)
|
||||
// Insert item after specified index.
|
||||
a.InsertBefore(10, 10)
|
||||
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// Modify item by index.
|
||||
a.Set(0, 100)
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// Search item and return its index.
|
||||
fmt.Println(a.Search(5))
|
||||
|
||||
// Remove item by index.
|
||||
a.Remove(0)
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// Empty the array, removes all items of it.
|
||||
fmt.Println(a.Slice())
|
||||
a.Clear()
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// Output:
|
||||
// 10
|
||||
// [0 1 2 3 4 5 6 7 8 9]
|
||||
// 6 true
|
||||
// true
|
||||
// false
|
||||
// [0 1 2 3 4 5 6 7 8 9 10 11]
|
||||
// [100 1 2 3 4 5 6 7 8 9 10 11]
|
||||
// 5
|
||||
// [1 2 3 4 5 6 7 8 9 10 11]
|
||||
// [1 2 3 4 5 6 7 8 9 10 11]
|
||||
// []
|
||||
}
|
||||
|
||||
func Example_rand() {
|
||||
array := garray.NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9})
|
||||
|
||||
// Randomly retrieve and return 2 items from the array.
|
||||
// It does not delete the items from array.
|
||||
fmt.Println(array.Rands(2))
|
||||
|
||||
// Randomly pick and return one item from the array.
|
||||
// It deletes the picked up item from array.
|
||||
fmt.Println(array.PopRand())
|
||||
}
|
||||
|
||||
func Example_popItem() {
|
||||
array := garray.NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9})
|
||||
|
||||
// Any Pop* functions pick, delete and return the item from array.
|
||||
|
||||
fmt.Println(array.PopLeft())
|
||||
fmt.Println(array.PopLefts(2))
|
||||
fmt.Println(array.PopRight())
|
||||
fmt.Println(array.PopRights(2))
|
||||
|
||||
// Output:
|
||||
// 1 true
|
||||
// [2 3]
|
||||
// 9 true
|
||||
// [7 8]
|
||||
}
|
||||
|
||||
func Example_mergeArray() {
|
||||
array1 := garray.NewFrom([]interface{}{1, 2})
|
||||
array2 := garray.NewFrom([]interface{}{3, 4})
|
||||
slice1 := []interface{}{5, 6}
|
||||
slice2 := []int{7, 8}
|
||||
slice3 := []string{"9", "0"}
|
||||
fmt.Println(array1.Slice())
|
||||
array1.Merge(array1)
|
||||
array1.Merge(array2)
|
||||
array1.Merge(slice1)
|
||||
array1.Merge(slice2)
|
||||
array1.Merge(slice3)
|
||||
fmt.Println(array1.Slice())
|
||||
|
||||
// Output:
|
||||
// [1 2]
|
||||
// [1 2 1 2 3 4 5 6 7 8 9 0]
|
||||
}
|
||||
|
||||
func Example_filter() {
|
||||
array1 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"})
|
||||
array2 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"})
|
||||
fmt.Printf("%#v\n", array1.FilterNil().Slice())
|
||||
fmt.Printf("%#v\n", array2.FilterEmpty().Slice())
|
||||
|
||||
// Output:
|
||||
// []interface {}{0, 1, 2, "", []interface {}{}, "john"}
|
||||
// []interface {}{1, 2, "john"}
|
||||
}
|
||||
@ -9,8 +9,8 @@
|
||||
package garray_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -95,9 +95,9 @@ func TestArray_Sort(t *testing.T) {
|
||||
|
||||
func TestArray_Unique(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := []interface{}{1, 1, 2, 3}
|
||||
expect := []interface{}{1, 2, 3, 4, 5, 3, 2, 2, 3, 5, 5}
|
||||
array := garray.NewArrayFrom(expect)
|
||||
t.Assert(array.Unique().Slice(), []interface{}{1, 2, 3})
|
||||
t.Assert(array.Unique().Slice(), []interface{}{1, 2, 3, 4, 5})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -9,8 +9,8 @@
|
||||
package garray_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -87,9 +87,9 @@ func TestIntArray_Sort(t *testing.T) {
|
||||
|
||||
func TestIntArray_Unique(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := []int{1, 1, 2, 3}
|
||||
expect := []int{1, 2, 3, 4, 5, 3, 2, 2, 3, 5, 5}
|
||||
array := garray.NewIntArrayFrom(expect)
|
||||
t.Assert(array.Unique().Slice(), []int{1, 2, 3})
|
||||
t.Assert(array.Unique().Slice(), []int{1, 2, 3, 4, 5})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -9,8 +9,8 @@
|
||||
package garray_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@ -91,7 +91,7 @@ func TestStrArray_Sort(t *testing.T) {
|
||||
|
||||
func TestStrArray_Unique(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := []string{"1", "1", "2", "3"}
|
||||
expect := []string{"1", "1", "2", "2", "3", "3", "2", "2"}
|
||||
array := garray.NewStrArrayFrom(expect)
|
||||
t.Assert(array.Unique().Slice(), []string{"1", "2", "3"})
|
||||
})
|
||||
|
||||
@ -9,8 +9,8 @@
|
||||
package garray_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/util/gutil"
|
||||
"strings"
|
||||
"testing"
|
||||
@ -526,15 +526,21 @@ func TestSortedArray_CountValues(t *testing.T) {
|
||||
|
||||
func TestSortedArray_SetUnique(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{"a", "d", "c", "c"}
|
||||
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
array1 := garray.NewSortedArrayFrom(a1, func1)
|
||||
a1 := []interface{}{1, 2, 3, 4, 5, 3, 2, 2, 3, 5, 5}
|
||||
array1 := garray.NewSortedArrayFrom(a1, gutil.ComparatorInt)
|
||||
array1.SetUnique(true)
|
||||
t.Assert(array1.Len(), 3)
|
||||
t.Assert(array1, []interface{}{"a", "c", "d"})
|
||||
t.Assert(array1.Len(), 5)
|
||||
t.Assert(array1, []interface{}{1, 2, 3, 4, 5})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_Unique(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []interface{}{1, 2, 3, 4, 5, 3, 2, 2, 3, 5, 5}
|
||||
array1 := garray.NewSortedArrayFrom(a1, gutil.ComparatorInt)
|
||||
array1.Unique()
|
||||
t.Assert(array1.Len(), 5)
|
||||
t.Assert(array1, []interface{}{1, 2, 3, 4, 5})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -9,8 +9,8 @@
|
||||
package garray_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -436,7 +436,7 @@ func TestSortedIntArray_CountValues(t *testing.T) {
|
||||
|
||||
func TestSortedIntArray_SetUnique(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 2, 3, 4, 5, 3}
|
||||
a1 := []int{1, 2, 3, 4, 5, 3, 2, 2, 3, 5, 5}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
array1.SetUnique(true)
|
||||
t.Assert(array1.Len(), 5)
|
||||
@ -444,6 +444,16 @@ func TestSortedIntArray_SetUnique(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_Unique(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []int{1, 2, 3, 4, 5, 3, 2, 2, 3, 5, 5}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
array1.Unique()
|
||||
t.Assert(array1.Len(), 5)
|
||||
t.Assert(array1, []int{1, 2, 3, 4, 5})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_LockFunc(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s1 := []int{1, 2, 3, 4}
|
||||
|
||||
@ -9,8 +9,8 @@
|
||||
package garray_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -454,11 +454,21 @@ func TestSortedStrArray_Chunk(t *testing.T) {
|
||||
|
||||
func TestSortedStrArray_SetUnique(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"e", "a", "d", "a", "c"}
|
||||
a1 := []string{"1", "1", "2", "2", "3", "3", "2", "2"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
array2 := array1.SetUnique(true)
|
||||
t.Assert(array2.Len(), 4)
|
||||
t.Assert(array2, []string{"a", "c", "d", "e"})
|
||||
t.Assert(array2.Len(), 3)
|
||||
t.Assert(array2, []string{"1", "2", "3"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_Unique(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a1 := []string{"1", "1", "2", "2", "3", "3", "2", "2"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
array1.Unique()
|
||||
t.Assert(array1.Len(), 3)
|
||||
t.Assert(array1, []string{"1", "2", "3"})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -5,25 +5,26 @@
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
//
|
||||
|
||||
// Package glist provides a concurrent-safe/unsafe doubly linked list.
|
||||
// Package glist provides most commonly used doubly linked list container which also supports concurrent-safe/unsafe switch feature.
|
||||
package glist
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"container/list"
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
|
||||
"github.com/gogf/gf/internal/rwmutex"
|
||||
)
|
||||
|
||||
type (
|
||||
// List is a doubly linked list containing a concurrent-safe/unsafe switch.
|
||||
// The switch should be set when its initialization and cannot be changed then.
|
||||
List struct {
|
||||
mu rwmutex.RWMutex
|
||||
list list.List
|
||||
list *list.List
|
||||
}
|
||||
|
||||
// Element the item type of the list.
|
||||
Element = list.Element
|
||||
)
|
||||
|
||||
@ -31,7 +32,7 @@ type (
|
||||
func New(safe ...bool) *List {
|
||||
return &List{
|
||||
mu: rwmutex.Create(safe...),
|
||||
list: list.List{},
|
||||
list: list.New(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -39,7 +40,7 @@ func New(safe ...bool) *List {
|
||||
// The parameter <safe> is used to specify whether using list in concurrent-safety,
|
||||
// which is false in default.
|
||||
func NewFrom(array []interface{}, safe ...bool) *List {
|
||||
l := list.List{}
|
||||
l := list.New()
|
||||
for _, v := range array {
|
||||
l.PushBack(v)
|
||||
}
|
||||
@ -52,6 +53,9 @@ func NewFrom(array []interface{}, safe ...bool) *List {
|
||||
// PushFront inserts a new element <e> with value <v> at the front of list <l> and returns <e>.
|
||||
func (l *List) PushFront(v interface{}) (e *Element) {
|
||||
l.mu.Lock()
|
||||
if l.list == nil {
|
||||
l.list = list.New()
|
||||
}
|
||||
e = l.list.PushFront(v)
|
||||
l.mu.Unlock()
|
||||
return
|
||||
@ -60,6 +64,9 @@ func (l *List) PushFront(v interface{}) (e *Element) {
|
||||
// PushBack inserts a new element <e> with value <v> at the back of list <l> and returns <e>.
|
||||
func (l *List) PushBack(v interface{}) (e *Element) {
|
||||
l.mu.Lock()
|
||||
if l.list == nil {
|
||||
l.list = list.New()
|
||||
}
|
||||
e = l.list.PushBack(v)
|
||||
l.mu.Unlock()
|
||||
return
|
||||
@ -68,6 +75,9 @@ func (l *List) PushBack(v interface{}) (e *Element) {
|
||||
// PushFronts inserts multiple new elements with values <values> at the front of list <l>.
|
||||
func (l *List) PushFronts(values []interface{}) {
|
||||
l.mu.Lock()
|
||||
if l.list == nil {
|
||||
l.list = list.New()
|
||||
}
|
||||
for _, v := range values {
|
||||
l.list.PushFront(v)
|
||||
}
|
||||
@ -77,6 +87,9 @@ func (l *List) PushFronts(values []interface{}) {
|
||||
// PushBacks inserts multiple new elements with values <values> at the back of list <l>.
|
||||
func (l *List) PushBacks(values []interface{}) {
|
||||
l.mu.Lock()
|
||||
if l.list == nil {
|
||||
l.list = list.New()
|
||||
}
|
||||
for _, v := range values {
|
||||
l.list.PushBack(v)
|
||||
}
|
||||
@ -86,20 +99,28 @@ func (l *List) PushBacks(values []interface{}) {
|
||||
// PopBack removes the element from back of <l> and returns the value of the element.
|
||||
func (l *List) PopBack() (value interface{}) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
if l.list == nil {
|
||||
l.list = list.New()
|
||||
return
|
||||
}
|
||||
if e := l.list.Back(); e != nil {
|
||||
value = l.list.Remove(e)
|
||||
}
|
||||
l.mu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
// PopFront removes the element from front of <l> and returns the value of the element.
|
||||
func (l *List) PopFront() (value interface{}) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
if l.list == nil {
|
||||
l.list = list.New()
|
||||
return
|
||||
}
|
||||
if e := l.list.Front(); e != nil {
|
||||
value = l.list.Remove(e)
|
||||
}
|
||||
l.mu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
@ -107,6 +128,11 @@ func (l *List) PopFront() (value interface{}) {
|
||||
// and returns values of the removed elements as slice.
|
||||
func (l *List) PopBacks(max int) (values []interface{}) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
if l.list == nil {
|
||||
l.list = list.New()
|
||||
return
|
||||
}
|
||||
length := l.list.Len()
|
||||
if length > 0 {
|
||||
if max > 0 && max < length {
|
||||
@ -117,7 +143,6 @@ func (l *List) PopBacks(max int) (values []interface{}) {
|
||||
values[i] = l.list.Remove(l.list.Back())
|
||||
}
|
||||
}
|
||||
l.mu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
@ -125,6 +150,11 @@ func (l *List) PopBacks(max int) (values []interface{}) {
|
||||
// and returns values of the removed elements as slice.
|
||||
func (l *List) PopFronts(max int) (values []interface{}) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
if l.list == nil {
|
||||
l.list = list.New()
|
||||
return
|
||||
}
|
||||
length := l.list.Len()
|
||||
if length > 0 {
|
||||
if max > 0 && max < length {
|
||||
@ -135,7 +165,6 @@ func (l *List) PopFronts(max int) (values []interface{}) {
|
||||
values[i] = l.list.Remove(l.list.Front())
|
||||
}
|
||||
}
|
||||
l.mu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
@ -154,6 +183,10 @@ func (l *List) PopFrontAll() []interface{} {
|
||||
// FrontAll copies and returns values of all elements from front of <l> as slice.
|
||||
func (l *List) FrontAll() (values []interface{}) {
|
||||
l.mu.RLock()
|
||||
defer l.mu.RUnlock()
|
||||
if l.list == nil {
|
||||
return
|
||||
}
|
||||
length := l.list.Len()
|
||||
if length > 0 {
|
||||
values = make([]interface{}, length)
|
||||
@ -161,13 +194,16 @@ func (l *List) FrontAll() (values []interface{}) {
|
||||
values[i] = e.Value
|
||||
}
|
||||
}
|
||||
l.mu.RUnlock()
|
||||
return
|
||||
}
|
||||
|
||||
// BackAll copies and returns values of all elements from back of <l> as slice.
|
||||
func (l *List) BackAll() (values []interface{}) {
|
||||
l.mu.RLock()
|
||||
defer l.mu.RUnlock()
|
||||
if l.list == nil {
|
||||
return
|
||||
}
|
||||
length := l.list.Len()
|
||||
if length > 0 {
|
||||
values = make([]interface{}, length)
|
||||
@ -175,43 +211,54 @@ func (l *List) BackAll() (values []interface{}) {
|
||||
values[i] = e.Value
|
||||
}
|
||||
}
|
||||
l.mu.RUnlock()
|
||||
return
|
||||
}
|
||||
|
||||
// FrontValue returns value of the first element of <l> or nil if the list is empty.
|
||||
func (l *List) FrontValue() (value interface{}) {
|
||||
l.mu.RLock()
|
||||
defer l.mu.RUnlock()
|
||||
if l.list == nil {
|
||||
return
|
||||
}
|
||||
if e := l.list.Front(); e != nil {
|
||||
value = e.Value
|
||||
}
|
||||
l.mu.RUnlock()
|
||||
return
|
||||
}
|
||||
|
||||
// BackValue returns value of the last element of <l> or nil if the list is empty.
|
||||
func (l *List) BackValue() (value interface{}) {
|
||||
l.mu.RLock()
|
||||
defer l.mu.RUnlock()
|
||||
if l.list == nil {
|
||||
return
|
||||
}
|
||||
if e := l.list.Back(); e != nil {
|
||||
value = e.Value
|
||||
}
|
||||
l.mu.RUnlock()
|
||||
return
|
||||
}
|
||||
|
||||
// Front returns the first element of list <l> or nil if the list is empty.
|
||||
func (l *List) Front() (e *Element) {
|
||||
l.mu.RLock()
|
||||
defer l.mu.RUnlock()
|
||||
if l.list == nil {
|
||||
return
|
||||
}
|
||||
e = l.list.Front()
|
||||
l.mu.RUnlock()
|
||||
return
|
||||
}
|
||||
|
||||
// Back returns the last element of list <l> or nil if the list is empty.
|
||||
func (l *List) Back() (e *Element) {
|
||||
l.mu.RLock()
|
||||
defer l.mu.RUnlock()
|
||||
if l.list == nil {
|
||||
return
|
||||
}
|
||||
e = l.list.Back()
|
||||
l.mu.RUnlock()
|
||||
return
|
||||
}
|
||||
|
||||
@ -219,8 +266,11 @@ func (l *List) Back() (e *Element) {
|
||||
// The complexity is O(1).
|
||||
func (l *List) Len() (length int) {
|
||||
l.mu.RLock()
|
||||
defer l.mu.RUnlock()
|
||||
if l.list == nil {
|
||||
return
|
||||
}
|
||||
length = l.list.Len()
|
||||
l.mu.RUnlock()
|
||||
return
|
||||
}
|
||||
|
||||
@ -234,8 +284,11 @@ func (l *List) Size() int {
|
||||
// The element and <p> must not be nil.
|
||||
func (l *List) MoveBefore(e, p *Element) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
if l.list == nil {
|
||||
l.list = list.New()
|
||||
}
|
||||
l.list.MoveBefore(e, p)
|
||||
l.mu.Unlock()
|
||||
}
|
||||
|
||||
// MoveAfter moves element <e> to its new position after <p>.
|
||||
@ -243,8 +296,11 @@ func (l *List) MoveBefore(e, p *Element) {
|
||||
// The element and <p> must not be nil.
|
||||
func (l *List) MoveAfter(e, p *Element) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
if l.list == nil {
|
||||
l.list = list.New()
|
||||
}
|
||||
l.list.MoveAfter(e, p)
|
||||
l.mu.Unlock()
|
||||
}
|
||||
|
||||
// MoveToFront moves element <e> to the front of list <l>.
|
||||
@ -252,8 +308,11 @@ func (l *List) MoveAfter(e, p *Element) {
|
||||
// The element must not be nil.
|
||||
func (l *List) MoveToFront(e *Element) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
if l.list == nil {
|
||||
l.list = list.New()
|
||||
}
|
||||
l.list.MoveToFront(e)
|
||||
l.mu.Unlock()
|
||||
}
|
||||
|
||||
// MoveToBack moves element <e> to the back of list <l>.
|
||||
@ -261,8 +320,11 @@ func (l *List) MoveToFront(e *Element) {
|
||||
// The element must not be nil.
|
||||
func (l *List) MoveToBack(e *Element) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
if l.list == nil {
|
||||
l.list = list.New()
|
||||
}
|
||||
l.list.MoveToBack(e)
|
||||
l.mu.Unlock()
|
||||
}
|
||||
|
||||
// PushBackList inserts a copy of an other list at the back of list <l>.
|
||||
@ -273,8 +335,11 @@ func (l *List) PushBackList(other *List) {
|
||||
defer other.mu.RUnlock()
|
||||
}
|
||||
l.mu.Lock()
|
||||
l.list.PushBackList(&other.list)
|
||||
l.mu.Unlock()
|
||||
defer l.mu.Unlock()
|
||||
if l.list == nil {
|
||||
l.list = list.New()
|
||||
}
|
||||
l.list.PushBackList(other.list)
|
||||
}
|
||||
|
||||
// PushFrontList inserts a copy of an other list at the front of list <l>.
|
||||
@ -285,8 +350,11 @@ func (l *List) PushFrontList(other *List) {
|
||||
defer other.mu.RUnlock()
|
||||
}
|
||||
l.mu.Lock()
|
||||
l.list.PushFrontList(&other.list)
|
||||
l.mu.Unlock()
|
||||
defer l.mu.Unlock()
|
||||
if l.list == nil {
|
||||
l.list = list.New()
|
||||
}
|
||||
l.list.PushFrontList(other.list)
|
||||
}
|
||||
|
||||
// InsertAfter inserts a new element <e> with value <v> immediately after <p> and returns <e>.
|
||||
@ -294,8 +362,11 @@ func (l *List) PushFrontList(other *List) {
|
||||
// The <p> must not be nil.
|
||||
func (l *List) InsertAfter(p *Element, v interface{}) (e *Element) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
if l.list == nil {
|
||||
l.list = list.New()
|
||||
}
|
||||
e = l.list.InsertAfter(v, p)
|
||||
l.mu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
@ -304,8 +375,11 @@ func (l *List) InsertAfter(p *Element, v interface{}) (e *Element) {
|
||||
// The <p> must not be nil.
|
||||
func (l *List) InsertBefore(p *Element, v interface{}) (e *Element) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
if l.list == nil {
|
||||
l.list = list.New()
|
||||
}
|
||||
e = l.list.InsertBefore(v, p)
|
||||
l.mu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
@ -314,25 +388,31 @@ func (l *List) InsertBefore(p *Element, v interface{}) (e *Element) {
|
||||
// The element must not be nil.
|
||||
func (l *List) Remove(e *Element) (value interface{}) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
if l.list == nil {
|
||||
l.list = list.New()
|
||||
}
|
||||
value = l.list.Remove(e)
|
||||
l.mu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
// Removes removes multiple elements <es> from <l> if <es> are elements of list <l>.
|
||||
func (l *List) Removes(es []*Element) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
if l.list == nil {
|
||||
l.list = list.New()
|
||||
}
|
||||
for _, e := range es {
|
||||
l.list.Remove(e)
|
||||
}
|
||||
l.mu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
// RemoveAll removes all elements from list <l>.
|
||||
func (l *List) RemoveAll() {
|
||||
l.mu.Lock()
|
||||
l.list = list.List{}
|
||||
l.list = list.New()
|
||||
l.mu.Unlock()
|
||||
}
|
||||
|
||||
@ -345,14 +425,19 @@ func (l *List) Clear() {
|
||||
func (l *List) RLockFunc(f func(list *list.List)) {
|
||||
l.mu.RLock()
|
||||
defer l.mu.RUnlock()
|
||||
f(&l.list)
|
||||
if l.list != nil {
|
||||
f(l.list)
|
||||
}
|
||||
}
|
||||
|
||||
// LockFunc locks writing with given callback function <f> within RWMutex.Lock.
|
||||
func (l *List) LockFunc(f func(list *list.List)) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
f(&l.list)
|
||||
if l.list == nil {
|
||||
l.list = list.New()
|
||||
}
|
||||
f(l.list)
|
||||
}
|
||||
|
||||
// Iterator is alias of IteratorAsc.
|
||||
@ -365,6 +450,9 @@ func (l *List) Iterator(f func(e *Element) bool) {
|
||||
func (l *List) IteratorAsc(f func(e *Element) bool) {
|
||||
l.mu.RLock()
|
||||
defer l.mu.RUnlock()
|
||||
if l.list == nil {
|
||||
return
|
||||
}
|
||||
length := l.list.Len()
|
||||
if length > 0 {
|
||||
for i, e := 0, l.list.Front(); i < length; i, e = i+1, e.Next() {
|
||||
@ -380,6 +468,9 @@ func (l *List) IteratorAsc(f func(e *Element) bool) {
|
||||
func (l *List) IteratorDesc(f func(e *Element) bool) {
|
||||
l.mu.RLock()
|
||||
defer l.mu.RUnlock()
|
||||
if l.list == nil {
|
||||
return
|
||||
}
|
||||
length := l.list.Len()
|
||||
if length > 0 {
|
||||
for i, e := 0, l.list.Back(); i < length; i, e = i+1, e.Prev() {
|
||||
@ -394,17 +485,14 @@ func (l *List) IteratorDesc(f func(e *Element) bool) {
|
||||
func (l *List) Join(glue string) string {
|
||||
l.mu.RLock()
|
||||
defer l.mu.RUnlock()
|
||||
if l.list == nil {
|
||||
return ""
|
||||
}
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
length := l.list.Len()
|
||||
if length > 0 {
|
||||
s := ""
|
||||
for i, e := 0, l.list.Front(); i < length; i, e = i+1, e.Next() {
|
||||
s = gconv.String(e.Value)
|
||||
if gstr.IsNumeric(s) {
|
||||
buffer.WriteString(s)
|
||||
} else {
|
||||
buffer.WriteString(`"` + gstr.QuoteMeta(s, `"\`) + `"`)
|
||||
}
|
||||
buffer.WriteString(gconv.String(e.Value))
|
||||
if i != length-1 {
|
||||
buffer.WriteString(glue)
|
||||
}
|
||||
@ -427,6 +515,9 @@ func (l *List) MarshalJSON() ([]byte, error) {
|
||||
func (l *List) UnmarshalJSON(b []byte) error {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
if l.list == nil {
|
||||
l.list = list.New()
|
||||
}
|
||||
var array []interface{}
|
||||
if err := json.Unmarshal(b, &array); err != nil {
|
||||
return err
|
||||
@ -439,6 +530,9 @@ func (l *List) UnmarshalJSON(b []byte) error {
|
||||
func (l *List) UnmarshalValue(value interface{}) (err error) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
if l.list == nil {
|
||||
l.list = list.New()
|
||||
}
|
||||
var array []interface{}
|
||||
switch value.(type) {
|
||||
case string, []byte:
|
||||
|
||||
@ -10,11 +10,12 @@ import (
|
||||
"container/list"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/garray"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
|
||||
"github.com/gogf/gf/container/glist"
|
||||
)
|
||||
|
||||
func Example_basic() {
|
||||
func ExampleNew() {
|
||||
n := 10
|
||||
l := glist.New()
|
||||
for i := 0; i < n; i++ {
|
||||
@ -31,14 +32,14 @@ func Example_basic() {
|
||||
fmt.Println(l.Len())
|
||||
|
||||
// Output:
|
||||
//10
|
||||
//[0 1 2 3 4 5 6 7 8 9]
|
||||
//[9 8 7 6 5 4 3 2 1 0]
|
||||
//0123456789
|
||||
//0
|
||||
// 10
|
||||
// [0 1 2 3 4 5 6 7 8 9]
|
||||
// [9 8 7 6 5 4 3 2 1 0]
|
||||
// 0123456789
|
||||
// 0
|
||||
}
|
||||
|
||||
func Example_iterate() {
|
||||
func ExampleList_RLockFunc() {
|
||||
// concurrent-safe list.
|
||||
l := glist.NewFrom(garray.NewArrayRange(1, 10, 1).Slice(), true)
|
||||
// iterate reading from head.
|
||||
@ -62,21 +63,39 @@ func Example_iterate() {
|
||||
})
|
||||
|
||||
fmt.Println()
|
||||
// Output:
|
||||
// 12345678910
|
||||
// 10987654321
|
||||
}
|
||||
|
||||
func ExampleList_IteratorAsc() {
|
||||
// concurrent-safe list.
|
||||
l := glist.NewFrom(garray.NewArrayRange(1, 10, 1).Slice(), true)
|
||||
// iterate reading from head using IteratorAsc.
|
||||
l.IteratorAsc(func(e *glist.Element) bool {
|
||||
fmt.Print(e.Value)
|
||||
return true
|
||||
})
|
||||
fmt.Println()
|
||||
|
||||
// Output:
|
||||
// 12345678910
|
||||
}
|
||||
|
||||
func ExampleList_IteratorDesc() {
|
||||
// concurrent-safe list.
|
||||
l := glist.NewFrom(garray.NewArrayRange(1, 10, 1).Slice(), true)
|
||||
// iterate reading from tail using IteratorDesc.
|
||||
l.IteratorDesc(func(e *glist.Element) bool {
|
||||
fmt.Print(e.Value)
|
||||
return true
|
||||
})
|
||||
// Output:
|
||||
// 10987654321
|
||||
}
|
||||
|
||||
fmt.Println()
|
||||
|
||||
func ExampleList_LockFunc() {
|
||||
// concurrent-safe list.
|
||||
l := glist.NewFrom(garray.NewArrayRange(1, 10, 1).Slice(), true)
|
||||
// iterate writing from head.
|
||||
l.LockFunc(func(list *list.List) {
|
||||
length := list.Len()
|
||||
@ -91,10 +110,51 @@ func Example_iterate() {
|
||||
})
|
||||
fmt.Println(l)
|
||||
|
||||
//output:
|
||||
//12345678910
|
||||
//10987654321
|
||||
//12345678910
|
||||
//10987654321
|
||||
//[1,2,3,4,5,"M",7,8,9,10]
|
||||
// Output:
|
||||
// [1,2,3,4,5,M,7,8,9,10]
|
||||
}
|
||||
|
||||
func ExampleList_PopBack() {
|
||||
l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})
|
||||
|
||||
fmt.Println(l.PopBack())
|
||||
|
||||
// Output:
|
||||
// 9
|
||||
}
|
||||
func ExampleList_PopBacks() {
|
||||
l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})
|
||||
|
||||
fmt.Println(l.PopBacks(2))
|
||||
|
||||
// Output:
|
||||
// [9 8]
|
||||
}
|
||||
|
||||
func ExampleList_PopFront() {
|
||||
l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})
|
||||
|
||||
fmt.Println(l.PopFront())
|
||||
|
||||
// Output:
|
||||
// 1
|
||||
}
|
||||
|
||||
func ExampleList_PopFronts() {
|
||||
l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})
|
||||
|
||||
fmt.Println(l.PopFronts(2))
|
||||
|
||||
// Output:
|
||||
// [1 2]
|
||||
}
|
||||
|
||||
func ExampleList_Join() {
|
||||
var l glist.List
|
||||
l.PushBacks(g.Slice{"a", "b", "c", "d"})
|
||||
|
||||
fmt.Println(l.Join(","))
|
||||
|
||||
// Output:
|
||||
// a,b,c,d
|
||||
}
|
||||
|
||||
@ -8,14 +8,13 @@ package glist
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 检查链表长度
|
||||
func checkListLen(t *gtest.T, l *List, len int) bool {
|
||||
if n := l.Len(); n != len {
|
||||
t.Errorf("l.Len() = %d, want %d", n, len)
|
||||
@ -24,7 +23,6 @@ func checkListLen(t *gtest.T, l *List, len int) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// 检查指针地址
|
||||
func checkListPointers(t *gtest.T, l *List, es []*Element) {
|
||||
if !checkListLen(t, l, len(es)) {
|
||||
return
|
||||
@ -605,6 +603,17 @@ func TestList_Removes(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestList_Pop(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9})
|
||||
|
||||
t.Assert(l.PopBack(), 9)
|
||||
t.Assert(l.PopBacks(2), []interface{}{8, 7})
|
||||
t.Assert(l.PopFront(), 1)
|
||||
t.Assert(l.PopFronts(2), []interface{}{2, 3})
|
||||
})
|
||||
}
|
||||
|
||||
func TestList_Clear(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := New()
|
||||
@ -672,15 +681,15 @@ func TestList_Iterator(t *testing.T) {
|
||||
func TestList_Join(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := NewFrom([]interface{}{1, 2, "a", `"b"`, `\c`})
|
||||
t.Assert(l.Join(","), `1,2,"a","\"b\"","\\c"`)
|
||||
t.Assert(l.Join("."), `1.2."a"."\"b\""."\\c"`)
|
||||
t.Assert(l.Join(","), `1,2,a,"b",\c`)
|
||||
t.Assert(l.Join("."), `1.2.a."b".\c`)
|
||||
})
|
||||
}
|
||||
|
||||
func TestList_String(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
l := NewFrom([]interface{}{1, 2, "a", `"b"`, `\c`})
|
||||
t.Assert(l.String(), `[1,2,"a","\"b\"","\\c"]`)
|
||||
t.Assert(l.String(), `[1,2,a,"b",\c]`)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
// If a copy of the MIT was not distributed with gm file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
// Package gmap provides concurrent-safe/unsafe map containers.
|
||||
// Package gmap provides most commonly used map container which also support concurrent-safe/unsafe switch feature.
|
||||
package gmap
|
||||
|
||||
type (
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
package gmap
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
|
||||
"github.com/gogf/gf/internal/empty"
|
||||
|
||||
@ -98,6 +98,7 @@ func (m *AnyAnyMap) MapStrAny() map[string]interface{} {
|
||||
}
|
||||
|
||||
// FilterEmpty deletes all key-value pair of which the value is empty.
|
||||
// Values like: 0, nil, false, "", len(slice/map/chan) == 0 are considered empty.
|
||||
func (m *AnyAnyMap) FilterEmpty() {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@ -108,6 +109,17 @@ func (m *AnyAnyMap) FilterEmpty() {
|
||||
}
|
||||
}
|
||||
|
||||
// FilterNil deletes all key-value pair of which the value is nil.
|
||||
func (m *AnyAnyMap) FilterNil() {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
for k, v := range m.data {
|
||||
if empty.IsNil(v) {
|
||||
delete(m.data, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Set sets key-value to the hash map.
|
||||
func (m *AnyAnyMap) Set(key interface{}, value interface{}) {
|
||||
m.mu.Lock()
|
||||
@ -445,6 +457,12 @@ func (m *AnyAnyMap) Merge(other *AnyAnyMap) {
|
||||
}
|
||||
}
|
||||
|
||||
// String returns the map as a string.
|
||||
func (m *AnyAnyMap) String() string {
|
||||
b, _ := m.MarshalJSON()
|
||||
return gconv.UnsafeBytesToStr(b)
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (m *AnyAnyMap) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(gconv.Map(m.Map()))
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
package gmap
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
|
||||
"github.com/gogf/gf/internal/empty"
|
||||
|
||||
@ -98,6 +98,7 @@ func (m *IntAnyMap) MapCopy() map[int]interface{} {
|
||||
}
|
||||
|
||||
// FilterEmpty deletes all key-value pair of which the value is empty.
|
||||
// Values like: 0, nil, false, "", len(slice/map/chan) == 0 are considered empty.
|
||||
func (m *IntAnyMap) FilterEmpty() {
|
||||
m.mu.Lock()
|
||||
for k, v := range m.data {
|
||||
@ -108,6 +109,17 @@ func (m *IntAnyMap) FilterEmpty() {
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// FilterNil deletes all key-value pair of which the value is nil.
|
||||
func (m *IntAnyMap) FilterNil() {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
for k, v := range m.data {
|
||||
if empty.IsNil(v) {
|
||||
delete(m.data, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Set sets key-value to the hash map.
|
||||
func (m *IntAnyMap) Set(key int, val interface{}) {
|
||||
m.mu.Lock()
|
||||
@ -443,6 +455,12 @@ func (m *IntAnyMap) Merge(other *IntAnyMap) {
|
||||
}
|
||||
}
|
||||
|
||||
// String returns the map as a string.
|
||||
func (m *IntAnyMap) String() string {
|
||||
b, _ := m.MarshalJSON()
|
||||
return gconv.UnsafeBytesToStr(b)
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (m *IntAnyMap) MarshalJSON() ([]byte, error) {
|
||||
m.mu.RLock()
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
package gmap
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
|
||||
"github.com/gogf/gf/internal/empty"
|
||||
@ -96,6 +96,7 @@ func (m *IntIntMap) MapCopy() map[int]int {
|
||||
}
|
||||
|
||||
// FilterEmpty deletes all key-value pair of which the value is empty.
|
||||
// Values like: 0, nil, false, "", len(slice/map/chan) == 0 are considered empty.
|
||||
func (m *IntIntMap) FilterEmpty() {
|
||||
m.mu.Lock()
|
||||
for k, v := range m.data {
|
||||
@ -425,6 +426,12 @@ func (m *IntIntMap) Merge(other *IntIntMap) {
|
||||
}
|
||||
}
|
||||
|
||||
// String returns the map as a string.
|
||||
func (m *IntIntMap) String() string {
|
||||
b, _ := m.MarshalJSON()
|
||||
return gconv.UnsafeBytesToStr(b)
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (m *IntIntMap) MarshalJSON() ([]byte, error) {
|
||||
m.mu.RLock()
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
package gmap
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
|
||||
"github.com/gogf/gf/internal/empty"
|
||||
|
||||
@ -96,6 +96,7 @@ func (m *IntStrMap) MapCopy() map[int]string {
|
||||
}
|
||||
|
||||
// FilterEmpty deletes all key-value pair of which the value is empty.
|
||||
// Values like: 0, nil, false, "", len(slice/map/chan) == 0 are considered empty.
|
||||
func (m *IntStrMap) FilterEmpty() {
|
||||
m.mu.Lock()
|
||||
for k, v := range m.data {
|
||||
@ -425,6 +426,12 @@ func (m *IntStrMap) Merge(other *IntStrMap) {
|
||||
}
|
||||
}
|
||||
|
||||
// String returns the map as a string.
|
||||
func (m *IntStrMap) String() string {
|
||||
b, _ := m.MarshalJSON()
|
||||
return gconv.UnsafeBytesToStr(b)
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (m *IntStrMap) MarshalJSON() ([]byte, error) {
|
||||
m.mu.RLock()
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
package gmap
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
|
||||
"github.com/gogf/gf/internal/empty"
|
||||
|
||||
@ -92,6 +92,7 @@ func (m *StrAnyMap) MapCopy() map[string]interface{} {
|
||||
}
|
||||
|
||||
// FilterEmpty deletes all key-value pair of which the value is empty.
|
||||
// Values like: 0, nil, false, "", len(slice/map/chan) == 0 are considered empty.
|
||||
func (m *StrAnyMap) FilterEmpty() {
|
||||
m.mu.Lock()
|
||||
for k, v := range m.data {
|
||||
@ -102,6 +103,17 @@ func (m *StrAnyMap) FilterEmpty() {
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// FilterNil deletes all key-value pair of which the value is nil.
|
||||
func (m *StrAnyMap) FilterNil() {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
for k, v := range m.data {
|
||||
if empty.IsNil(v) {
|
||||
delete(m.data, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Set sets key-value to the hash map.
|
||||
func (m *StrAnyMap) Set(key string, val interface{}) {
|
||||
m.mu.Lock()
|
||||
@ -439,6 +451,12 @@ func (m *StrAnyMap) Merge(other *StrAnyMap) {
|
||||
}
|
||||
}
|
||||
|
||||
// String returns the map as a string.
|
||||
func (m *StrAnyMap) String() string {
|
||||
b, _ := m.MarshalJSON()
|
||||
return gconv.UnsafeBytesToStr(b)
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (m *StrAnyMap) MarshalJSON() ([]byte, error) {
|
||||
m.mu.RLock()
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
package gmap
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
|
||||
"github.com/gogf/gf/internal/empty"
|
||||
"github.com/gogf/gf/internal/rwmutex"
|
||||
@ -96,6 +96,7 @@ func (m *StrIntMap) MapCopy() map[string]int {
|
||||
}
|
||||
|
||||
// FilterEmpty deletes all key-value pair of which the value is empty.
|
||||
// Values like: 0, nil, false, "", len(slice/map/chan) == 0 are considered empty.
|
||||
func (m *StrIntMap) FilterEmpty() {
|
||||
m.mu.Lock()
|
||||
for k, v := range m.data {
|
||||
@ -428,6 +429,12 @@ func (m *StrIntMap) Merge(other *StrIntMap) {
|
||||
}
|
||||
}
|
||||
|
||||
// String returns the map as a string.
|
||||
func (m *StrIntMap) String() string {
|
||||
b, _ := m.MarshalJSON()
|
||||
return gconv.UnsafeBytesToStr(b)
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (m *StrIntMap) MarshalJSON() ([]byte, error) {
|
||||
m.mu.RLock()
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
package gmap
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
|
||||
"github.com/gogf/gf/internal/empty"
|
||||
@ -97,6 +97,7 @@ func (m *StrStrMap) MapCopy() map[string]string {
|
||||
}
|
||||
|
||||
// FilterEmpty deletes all key-value pair of which the value is empty.
|
||||
// Values like: 0, nil, false, "", len(slice/map/chan) == 0 are considered empty.
|
||||
func (m *StrStrMap) FilterEmpty() {
|
||||
m.mu.Lock()
|
||||
for k, v := range m.data {
|
||||
@ -428,6 +429,12 @@ func (m *StrStrMap) Merge(other *StrStrMap) {
|
||||
}
|
||||
}
|
||||
|
||||
// String returns the map as a string.
|
||||
func (m *StrStrMap) String() string {
|
||||
b, _ := m.MarshalJSON()
|
||||
return gconv.UnsafeBytesToStr(b)
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (m *StrStrMap) MarshalJSON() ([]byte, error) {
|
||||
m.mu.RLock()
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
package gmap
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
|
||||
"github.com/gogf/gf/internal/empty"
|
||||
|
||||
@ -510,6 +510,12 @@ func (m *ListMap) Merge(other *ListMap) {
|
||||
})
|
||||
}
|
||||
|
||||
// String returns the map as a string.
|
||||
func (m *ListMap) String() string {
|
||||
b, _ := m.MarshalJSON()
|
||||
return gconv.UnsafeBytesToStr(b)
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (m *ListMap) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(gconv.Map(m.Map()))
|
||||
|
||||
207
container/gmap/gmap_z_example_any_any_test.go
Normal file
207
container/gmap/gmap_z_example_any_any_test.go
Normal file
@ -0,0 +1,207 @@
|
||||
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with gm file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gmap_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
)
|
||||
|
||||
func ExampleNew() {
|
||||
m := gmap.New()
|
||||
|
||||
// Add data.
|
||||
m.Set("key1", "val1")
|
||||
|
||||
// Print size.
|
||||
fmt.Println(m.Size())
|
||||
|
||||
addMap := make(map[interface{}]interface{})
|
||||
addMap["key2"] = "val2"
|
||||
addMap["key3"] = "val3"
|
||||
addMap[1] = 1
|
||||
|
||||
fmt.Println(m.Values())
|
||||
|
||||
// Batch add data.
|
||||
m.Sets(addMap)
|
||||
|
||||
// Gets the value of the corresponding key.
|
||||
fmt.Println(m.Get("key3"))
|
||||
|
||||
// Get the value by key, or set it with given key-value if not exist.
|
||||
fmt.Println(m.GetOrSet("key4", "val4"))
|
||||
|
||||
// Set key-value if the key does not exist, then return true; or else return false.
|
||||
fmt.Println(m.SetIfNotExist("key3", "val3"))
|
||||
|
||||
// Remove key
|
||||
m.Remove("key2")
|
||||
fmt.Println(m.Keys())
|
||||
|
||||
// Batch remove keys.
|
||||
m.Removes([]interface{}{"key1", 1})
|
||||
fmt.Println(m.Keys())
|
||||
|
||||
// Contains checks whether a key exists.
|
||||
fmt.Println(m.Contains("key3"))
|
||||
|
||||
// Flip exchanges key-value of the map, it will change key-value to value-key.
|
||||
m.Flip()
|
||||
fmt.Println(m.Map())
|
||||
|
||||
// Clear deletes all data of the map.
|
||||
m.Clear()
|
||||
|
||||
fmt.Println(m.Size())
|
||||
|
||||
// May Output:
|
||||
// 1
|
||||
// [val1]
|
||||
// val3
|
||||
// val4
|
||||
// false
|
||||
// [key4 key1 key3 1]
|
||||
// [key4 key3]
|
||||
// true
|
||||
// map[val3:key3 val4:key4]
|
||||
// 0
|
||||
}
|
||||
|
||||
func ExampleAnyAnyMap_Keys() {
|
||||
var m gmap.Map
|
||||
m.Sets(g.MapAnyAny{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
"k3": "v3",
|
||||
"k4": "v4",
|
||||
})
|
||||
fmt.Println(m.Keys())
|
||||
fmt.Println(m.Values())
|
||||
|
||||
// May Output:
|
||||
// [k1 k2 k3 k4]
|
||||
// [v2 v3 v4 v1]
|
||||
}
|
||||
|
||||
func ExampleAnyAnyMap_Values() {
|
||||
var m gmap.Map
|
||||
m.Sets(g.MapAnyAny{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
"k3": "v3",
|
||||
"k4": "v4",
|
||||
})
|
||||
fmt.Println(m.Keys())
|
||||
fmt.Println(m.Values())
|
||||
|
||||
// May Output:
|
||||
// [k1 k2 k3 k4]
|
||||
// [v2 v3 v4 v1]
|
||||
}
|
||||
|
||||
func ExampleAnyAnyMap_Flip() {
|
||||
var m gmap.Map
|
||||
m.Sets(g.MapAnyAny{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
})
|
||||
m.Flip()
|
||||
fmt.Println(m.Map())
|
||||
|
||||
// May Output:
|
||||
// map[v1:k1 v2:k2]
|
||||
}
|
||||
|
||||
func ExampleAnyAnyMap_Pop() {
|
||||
var m gmap.Map
|
||||
m.Sets(g.MapAnyAny{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
"k3": "v3",
|
||||
"k4": "v4",
|
||||
})
|
||||
fmt.Println(m.Pop())
|
||||
fmt.Println(m.Pops(2))
|
||||
fmt.Println(m.Size())
|
||||
|
||||
// May Output:
|
||||
// k1 v1
|
||||
// map[k2:v2 k4:v4]
|
||||
// 1
|
||||
}
|
||||
|
||||
func ExampleAnyAnyMap_Pops() {
|
||||
var m gmap.Map
|
||||
m.Sets(g.MapAnyAny{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
"k3": "v3",
|
||||
"k4": "v4",
|
||||
})
|
||||
fmt.Println(m.Pop())
|
||||
fmt.Println(m.Pops(2))
|
||||
fmt.Println(m.Size())
|
||||
|
||||
// May Output:
|
||||
// k1 v1
|
||||
// map[k2:v2 k4:v4]
|
||||
// 1
|
||||
}
|
||||
|
||||
func ExampleAnyAnyMap_FilterEmpty() {
|
||||
m := gmap.NewFrom(g.MapAnyAny{
|
||||
"k1": "",
|
||||
"k2": nil,
|
||||
"k3": 0,
|
||||
"k4": 1,
|
||||
})
|
||||
m.FilterEmpty()
|
||||
fmt.Println(m.Map())
|
||||
|
||||
// May Output:
|
||||
// map[k4:1]
|
||||
}
|
||||
|
||||
func ExampleAnyAnyMap_FilterNil() {
|
||||
m := gmap.NewFrom(g.MapAnyAny{
|
||||
"k1": "",
|
||||
"k2": nil,
|
||||
"k3": 0,
|
||||
"k4": 1,
|
||||
})
|
||||
m.FilterNil()
|
||||
fmt.Println(m.Map())
|
||||
|
||||
// May Output:
|
||||
// map[k1: k3:0 k4:1]
|
||||
}
|
||||
|
||||
func ExampleAnyAnyMap_SetIfNotExist() {
|
||||
var m gmap.Map
|
||||
fmt.Println(m.SetIfNotExist("k1", "v1"))
|
||||
fmt.Println(m.SetIfNotExist("k1", "v1"))
|
||||
fmt.Println(m.Map())
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
// map[k1:v1]
|
||||
}
|
||||
|
||||
func ExampleAnyAnyMap_Merge() {
|
||||
var m1, m2 gmap.Map
|
||||
m1.Set("key1", "val1")
|
||||
m2.Set("key2", "val2")
|
||||
m1.Merge(&m2)
|
||||
fmt.Println(m1.Map())
|
||||
|
||||
// May Output:
|
||||
// map[key1:val1 key2:val2]
|
||||
}
|
||||
@ -1,71 +0,0 @@
|
||||
package gmap_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
)
|
||||
|
||||
func Example_normalBasic() {
|
||||
m := gmap.New()
|
||||
|
||||
//Add data
|
||||
m.Set("key1", "val1")
|
||||
|
||||
//Print size
|
||||
fmt.Println(m.Size())
|
||||
//output 1
|
||||
|
||||
add_map := make(map[interface{}]interface{})
|
||||
add_map["key2"] = "val2"
|
||||
add_map["key3"] = "val3"
|
||||
add_map[1] = 1
|
||||
|
||||
fmt.Println(m.Values())
|
||||
|
||||
//Batch add data
|
||||
m.Sets(add_map)
|
||||
|
||||
//Gets the value of the corresponding key
|
||||
key3_val := m.Get("key3")
|
||||
fmt.Println(key3_val)
|
||||
|
||||
//Get the value by key, or set it with given key-value if not exist.
|
||||
get_or_set_val := m.GetOrSet("key4", "val4")
|
||||
fmt.Println(get_or_set_val)
|
||||
|
||||
// Set key-value if the key does not exist, then return true; or else return false.
|
||||
is_set := m.SetIfNotExist("key3", "val3")
|
||||
fmt.Println(is_set)
|
||||
|
||||
//Remove key
|
||||
m.Remove("key2")
|
||||
fmt.Println(m.Keys())
|
||||
|
||||
//Batch remove keys
|
||||
remove_keys := []interface{}{"key1", 1}
|
||||
m.Removes(remove_keys)
|
||||
fmt.Println(m.Keys())
|
||||
|
||||
//Contains checks whether a key exists.
|
||||
is_contain := m.Contains("key3")
|
||||
fmt.Println(is_contain)
|
||||
|
||||
//Flip exchanges key-value of the map, it will change key-value to value-key.
|
||||
m.Flip()
|
||||
fmt.Println(m.Map())
|
||||
|
||||
// Clear deletes all data of the map,
|
||||
m.Clear()
|
||||
|
||||
fmt.Println(m.Size())
|
||||
|
||||
}
|
||||
func Example_normalMerge() {
|
||||
m1 := gmap.New()
|
||||
m2 := gmap.New()
|
||||
m1.Set("key1", "val1")
|
||||
m2.Set("key2", "val2")
|
||||
m1.Merge(m2)
|
||||
fmt.Println(m1.Map())
|
||||
}
|
||||
@ -7,14 +7,14 @@
|
||||
package gmap_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/container/garray"
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Test_AnyAnyMap_Var(t *testing.T) {
|
||||
@ -218,6 +218,18 @@ func Test_AnyAnyMap_FilterEmpty(t *testing.T) {
|
||||
t.Assert(m.Get(1), nil)
|
||||
t.Assert(m.Get(2), 2)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewAnyAnyMap()
|
||||
m.Set(1, 0)
|
||||
m.Set("time1", time.Time{})
|
||||
m.Set("time2", time.Now())
|
||||
t.Assert(m.Get(1), 0)
|
||||
t.Assert(m.Get("time1"), time.Time{})
|
||||
m.FilterEmpty()
|
||||
t.Assert(m.Get(1), nil)
|
||||
t.Assert(m.Get("time1"), nil)
|
||||
t.AssertNE(m.Get("time2"), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AnyAnyMap_Json(t *testing.T) {
|
||||
|
||||
@ -7,9 +7,9 @@
|
||||
package gmap_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/container/garray"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"testing"
|
||||
|
||||
|
||||
@ -7,9 +7,9 @@
|
||||
package gmap_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/container/garray"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"testing"
|
||||
|
||||
|
||||
@ -7,9 +7,9 @@
|
||||
package gmap_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/container/garray"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"testing"
|
||||
|
||||
|
||||
@ -7,8 +7,8 @@
|
||||
package gmap_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/container/garray"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"testing"
|
||||
|
||||
|
||||
@ -7,9 +7,9 @@
|
||||
package gmap_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/container/garray"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"testing"
|
||||
|
||||
|
||||
@ -7,9 +7,9 @@
|
||||
package gmap_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/container/garray"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"testing"
|
||||
|
||||
|
||||
@ -7,9 +7,9 @@
|
||||
package gmap_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/container/garray"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"testing"
|
||||
|
||||
|
||||
@ -7,8 +7,8 @@
|
||||
package gmap_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"testing"
|
||||
|
||||
|
||||
@ -170,6 +170,7 @@ func (p *Pool) checkExpireItems() {
|
||||
if r := p.list.PopFront(); r != nil {
|
||||
item := r.(*poolItem)
|
||||
latestExpire = item.expire
|
||||
// TODO improve the auto-expiration mechanism of the pool.
|
||||
if item.expire > timestampMilli {
|
||||
p.list.PushFront(item)
|
||||
break
|
||||
|
||||
@ -9,7 +9,7 @@ package gset
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/internal/rwmutex"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
@ -61,12 +61,12 @@ func (set *Set) Iterator(f func(v interface{}) bool) {
|
||||
}
|
||||
|
||||
// Add adds one or multiple items to the set.
|
||||
func (set *Set) Add(item ...interface{}) {
|
||||
func (set *Set) Add(items ...interface{}) {
|
||||
set.mu.Lock()
|
||||
if set.data == nil {
|
||||
set.data = make(map[interface{}]struct{})
|
||||
}
|
||||
for _, v := range item {
|
||||
for _, v := range items {
|
||||
set.data[v] = struct{}{}
|
||||
}
|
||||
set.mu.Unlock()
|
||||
@ -456,9 +456,11 @@ func (set *Set) Pops(size int) []interface{} {
|
||||
func (set *Set) Walk(f func(item interface{}) interface{}) *Set {
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
m := make(map[interface{}]struct{}, len(set.data))
|
||||
for k, v := range set.data {
|
||||
set.data[f(k)] = v
|
||||
m[f(k)] = v
|
||||
}
|
||||
set.data = m
|
||||
return set
|
||||
}
|
||||
|
||||
|
||||
@ -9,7 +9,7 @@ package gset
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/internal/rwmutex"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
)
|
||||
@ -416,9 +416,11 @@ func (set *IntSet) Pops(size int) []int {
|
||||
func (set *IntSet) Walk(f func(item int) int) *IntSet {
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
m := make(map[int]struct{}, len(set.data))
|
||||
for k, v := range set.data {
|
||||
set.data[f(k)] = v
|
||||
m[f(k)] = v
|
||||
}
|
||||
set.data = m
|
||||
return set
|
||||
}
|
||||
|
||||
|
||||
@ -9,7 +9,7 @@ package gset
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/internal/rwmutex"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
@ -444,9 +444,11 @@ func (set *StrSet) Pops(size int) []string {
|
||||
func (set *StrSet) Walk(f func(item string) string) *StrSet {
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
m := make(map[string]struct{}, len(set.data))
|
||||
for k, v := range set.data {
|
||||
set.data[f(k)] = v
|
||||
m[f(k)] = v
|
||||
}
|
||||
set.data = m
|
||||
return set
|
||||
}
|
||||
|
||||
|
||||
166
container/gset/gset_z_example_any_test.go
Normal file
166
container/gset/gset_z_example_any_test.go
Normal file
@ -0,0 +1,166 @@
|
||||
// Copyright 2020 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with gm file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gset_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/gset"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func ExampleSet_Intersect() {
|
||||
s1 := gset.NewFrom(g.Slice{1, 2, 3})
|
||||
s2 := gset.NewFrom(g.Slice{4, 5, 6})
|
||||
s3 := gset.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7})
|
||||
|
||||
fmt.Println(s3.Intersect(s1).Slice())
|
||||
fmt.Println(s3.Diff(s1).Slice())
|
||||
fmt.Println(s1.Union(s2).Slice())
|
||||
fmt.Println(s1.Complement(s3).Slice())
|
||||
|
||||
// May Output:
|
||||
// [2 3 1]
|
||||
// [5 6 7 4]
|
||||
// [6 1 2 3 4 5]
|
||||
// [4 5 6 7]
|
||||
}
|
||||
|
||||
func ExampleSet_Diff() {
|
||||
s1 := gset.NewFrom(g.Slice{1, 2, 3})
|
||||
s2 := gset.NewFrom(g.Slice{4, 5, 6})
|
||||
s3 := gset.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7})
|
||||
|
||||
fmt.Println(s3.Intersect(s1).Slice())
|
||||
fmt.Println(s3.Diff(s1).Slice())
|
||||
fmt.Println(s1.Union(s2).Slice())
|
||||
fmt.Println(s1.Complement(s3).Slice())
|
||||
|
||||
// May Output:
|
||||
// [2 3 1]
|
||||
// [5 6 7 4]
|
||||
// [6 1 2 3 4 5]
|
||||
// [4 5 6 7]
|
||||
}
|
||||
|
||||
func ExampleSet_Union() {
|
||||
s1 := gset.NewFrom(g.Slice{1, 2, 3})
|
||||
s2 := gset.NewFrom(g.Slice{4, 5, 6})
|
||||
s3 := gset.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7})
|
||||
|
||||
fmt.Println(s3.Intersect(s1).Slice())
|
||||
fmt.Println(s3.Diff(s1).Slice())
|
||||
fmt.Println(s1.Union(s2).Slice())
|
||||
fmt.Println(s1.Complement(s3).Slice())
|
||||
|
||||
// May Output:
|
||||
// [2 3 1]
|
||||
// [5 6 7 4]
|
||||
// [6 1 2 3 4 5]
|
||||
// [4 5 6 7]
|
||||
}
|
||||
|
||||
func ExampleSet_Complement() {
|
||||
s1 := gset.NewFrom(g.Slice{1, 2, 3})
|
||||
s2 := gset.NewFrom(g.Slice{4, 5, 6})
|
||||
s3 := gset.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7})
|
||||
|
||||
fmt.Println(s3.Intersect(s1).Slice())
|
||||
fmt.Println(s3.Diff(s1).Slice())
|
||||
fmt.Println(s1.Union(s2).Slice())
|
||||
fmt.Println(s1.Complement(s3).Slice())
|
||||
|
||||
// May Output:
|
||||
// [2 3 1]
|
||||
// [5 6 7 4]
|
||||
// [6 1 2 3 4 5]
|
||||
// [4 5 6 7]
|
||||
}
|
||||
|
||||
func ExampleSet_IsSubsetOf() {
|
||||
var s1, s2 gset.Set
|
||||
s1.Add(g.Slice{1, 2, 3}...)
|
||||
s2.Add(g.Slice{2, 3}...)
|
||||
fmt.Println(s1.IsSubsetOf(&s2))
|
||||
fmt.Println(s2.IsSubsetOf(&s1))
|
||||
|
||||
// Output:
|
||||
// false
|
||||
// true
|
||||
}
|
||||
|
||||
func ExampleSet_AddIfNotExist() {
|
||||
var set gset.Set
|
||||
fmt.Println(set.AddIfNotExist(1))
|
||||
fmt.Println(set.AddIfNotExist(1))
|
||||
fmt.Println(set.Slice())
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
// [1]
|
||||
}
|
||||
|
||||
func ExampleSet_Pop() {
|
||||
var set gset.Set
|
||||
set.Add(1, 2, 3, 4)
|
||||
fmt.Println(set.Pop())
|
||||
fmt.Println(set.Pops(2))
|
||||
fmt.Println(set.Size())
|
||||
|
||||
// May Output:
|
||||
// 1
|
||||
// [2 3]
|
||||
// 1
|
||||
}
|
||||
|
||||
func ExampleSet_Pops() {
|
||||
var set gset.Set
|
||||
set.Add(1, 2, 3, 4)
|
||||
fmt.Println(set.Pop())
|
||||
fmt.Println(set.Pops(2))
|
||||
fmt.Println(set.Size())
|
||||
|
||||
// May Output:
|
||||
// 1
|
||||
// [2 3]
|
||||
// 1
|
||||
}
|
||||
|
||||
func ExampleSet_Join() {
|
||||
var set gset.Set
|
||||
set.Add("a", "b", "c", "d")
|
||||
fmt.Println(set.Join(","))
|
||||
|
||||
// May Output:
|
||||
// a,b,c,d
|
||||
}
|
||||
|
||||
func ExampleSet_Contains() {
|
||||
var set gset.StrSet
|
||||
set.Add("a")
|
||||
fmt.Println(set.Contains("a"))
|
||||
fmt.Println(set.Contains("A"))
|
||||
fmt.Println(set.ContainsI("A"))
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
// true
|
||||
}
|
||||
|
||||
func ExampleSet_ContainsI() {
|
||||
var set gset.StrSet
|
||||
set.Add("a")
|
||||
fmt.Println(set.Contains("a"))
|
||||
fmt.Println(set.Contains("A"))
|
||||
fmt.Println(set.ContainsI("A"))
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
// true
|
||||
}
|
||||
23
container/gset/gset_z_example_int_test.go
Normal file
23
container/gset/gset_z_example_int_test.go
Normal file
@ -0,0 +1,23 @@
|
||||
// Copyright 2020 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with gm file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gset_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/gset"
|
||||
)
|
||||
|
||||
func ExampleIntSet_Contains() {
|
||||
var set gset.IntSet
|
||||
set.Add(1)
|
||||
fmt.Println(set.Contains(1))
|
||||
fmt.Println(set.Contains(2))
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
}
|
||||
43
container/gset/gset_z_example_str_test.go
Normal file
43
container/gset/gset_z_example_str_test.go
Normal file
@ -0,0 +1,43 @@
|
||||
// Copyright 2020 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with gm file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gset_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/gset"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func ExampleStrSet_Contains() {
|
||||
var set gset.StrSet
|
||||
set.Add("a")
|
||||
fmt.Println(set.Contains("a"))
|
||||
fmt.Println(set.Contains("A"))
|
||||
fmt.Println(set.ContainsI("A"))
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
// true
|
||||
}
|
||||
|
||||
func ExampleStrSet_Walk() {
|
||||
var (
|
||||
set gset.StrSet
|
||||
names = g.SliceStr{"user", "user_detail"}
|
||||
prefix = "gf_"
|
||||
)
|
||||
set.Add(names...)
|
||||
// Add prefix for given table names.
|
||||
set.Walk(func(item string) string {
|
||||
return prefix + item
|
||||
})
|
||||
fmt.Println(set.Slice())
|
||||
|
||||
// May Output:
|
||||
// [gf_user gf_user_detail]
|
||||
}
|
||||
@ -9,8 +9,8 @@
|
||||
package gset_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@ -389,6 +389,19 @@ func TestSet_AddIfNotExistFunc(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestSet_Walk(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var set gset.Set
|
||||
set.Add(g.Slice{1, 2}...)
|
||||
set.Walk(func(item interface{}) interface{} {
|
||||
return gconv.Int(item) + 10
|
||||
})
|
||||
t.Assert(set.Size(), 2)
|
||||
t.Assert(set.Contains(11), true)
|
||||
t.Assert(set.Contains(12), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSet_AddIfNotExistFuncLock(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.New(true)
|
||||
|
||||
@ -9,8 +9,8 @@
|
||||
package gset_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@ -377,6 +377,19 @@ func TestIntSet_Json(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntSet_Walk(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var set gset.IntSet
|
||||
set.Add(g.SliceInt{1, 2}...)
|
||||
set.Walk(func(item int) int {
|
||||
return item + 10
|
||||
})
|
||||
t.Assert(set.Size(), 2)
|
||||
t.Assert(set.Contains(11), true)
|
||||
t.Assert(set.Contains(12), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntSet_UnmarshalValue(t *testing.T) {
|
||||
type V struct {
|
||||
Name string
|
||||
|
||||
@ -9,8 +9,8 @@
|
||||
package gset_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@ -423,6 +423,24 @@ func TestStrSet_Json(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrSet_Walk(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
set gset.StrSet
|
||||
names = g.SliceStr{"user", "user_detail"}
|
||||
prefix = "gf_"
|
||||
)
|
||||
set.Add(names...)
|
||||
// Add prefix for given table names.
|
||||
set.Walk(func(item string) string {
|
||||
return prefix + item
|
||||
})
|
||||
t.Assert(set.Size(), 2)
|
||||
t.Assert(set.Contains("gf_user"), true)
|
||||
t.Assert(set.Contains("gf_user_detail"), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrSet_UnmarshalValue(t *testing.T) {
|
||||
type V struct {
|
||||
Name string
|
||||
|
||||
@ -5,4 +5,7 @@
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
// Package gtree provides concurrent-safe/unsafe tree containers.
|
||||
//
|
||||
// Some implements are from: https://github.com/emirpasic/gods
|
||||
// Thanks!
|
||||
package gtree
|
||||
|
||||
@ -7,8 +7,8 @@
|
||||
package gtree
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
|
||||
|
||||
@ -8,8 +8,8 @@ package gtree
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
@ -620,7 +620,7 @@ func (tree *BTree) middle() int {
|
||||
func (tree *BTree) search(node *BTreeNode, key interface{}) (index int, found bool) {
|
||||
low, mid, high := 0, 0, len(node.Entries)-1
|
||||
for low <= high {
|
||||
mid = (high + low) / 2
|
||||
mid = low + int((high-low)/2)
|
||||
compare := tree.getComparator()(key, node.Entries[mid].Key)
|
||||
switch {
|
||||
case compare > 0:
|
||||
|
||||
@ -7,8 +7,8 @@
|
||||
package gtree
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"github.com/gogf/gf/util/gutil"
|
||||
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
package gtype
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
@ -9,8 +9,8 @@
|
||||
package gtype_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/container/gtype"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
package gtype_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"testing"
|
||||
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
package gtype_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
package gtype_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"testing"
|
||||
|
||||
|
||||
@ -7,8 +7,8 @@
|
||||
package gtype_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/container/gtype"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"math"
|
||||
|
||||
@ -7,8 +7,8 @@
|
||||
package gtype_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/container/gtype"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"math"
|
||||
|
||||
@ -7,8 +7,8 @@
|
||||
package gtype_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/container/gtype"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"math"
|
||||
|
||||
@ -7,8 +7,8 @@
|
||||
package gtype_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/container/gtype"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"math"
|
||||
|
||||
@ -7,8 +7,8 @@
|
||||
package gtype_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/container/gtype"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"sync"
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user