mirror of
https://gitee.com/johng/gf
synced 2026-07-04 21:03:13 +08:00
目录结构调整
This commit is contained in:
382
gexample/db/mysql/mysql.go
Normal file
382
gexample/db/mysql/mysql.go
Normal file
@ -0,0 +1,382 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"strconv"
|
||||
"gf/g/database/gdb"
|
||||
)
|
||||
|
||||
// 本文件用于gf框架的mysql数据库操作示例,不作为单元测试使用
|
||||
|
||||
var db gdb.Link
|
||||
|
||||
// 初始化配置及创建数据库
|
||||
func init () {
|
||||
gdb.AddDefaultConfigNode(gdb.ConfigNode {
|
||||
Host : "127.0.0.1",
|
||||
Port : "3306",
|
||||
User : "root",
|
||||
Pass : "123456",
|
||||
Name : "test2",
|
||||
Type : "mysql",
|
||||
Role : "master",
|
||||
Charset : "utf8",
|
||||
})
|
||||
db, _ = gdb.Instance()
|
||||
|
||||
//gdb.SetConfig(gdb.ConfigNode {
|
||||
// Host : "127.0.0.1",
|
||||
// Port : 3306,
|
||||
// User : "root",
|
||||
// Pass : "123456",
|
||||
// Name : "test",
|
||||
// Type : "mysql",
|
||||
//})
|
||||
//db, _ = gdb.Instance()
|
||||
|
||||
//gdb.SetConfig(gdb.Config {
|
||||
// "default" : gdb.ConfigGroup {
|
||||
// gdb.ConfigNode {
|
||||
// Host : "127.0.0.1",
|
||||
// Port : "3306",
|
||||
// User : "root",
|
||||
// Pass : "123456",
|
||||
// Name : "test",
|
||||
// Type : "mysql",
|
||||
// Role : "master",
|
||||
// Priority : 100,
|
||||
// },
|
||||
// gdb.ConfigNode {
|
||||
// Host : "127.0.0.2",
|
||||
// Port : "3306",
|
||||
// User : "root",
|
||||
// Pass : "123456",
|
||||
// Name : "test",
|
||||
// Type : "mysql",
|
||||
// Role : "master",
|
||||
// Priority : 100,
|
||||
// },
|
||||
// gdb.ConfigNode {
|
||||
// Host : "127.0.0.3",
|
||||
// Port : "3306",
|
||||
// User : "root",
|
||||
// Pass : "123456",
|
||||
// Name : "test",
|
||||
// Type : "mysql",
|
||||
// Role : "master",
|
||||
// Priority : 100,
|
||||
// },
|
||||
// gdb.ConfigNode {
|
||||
// Host : "127.0.0.4",
|
||||
// Port : "3306",
|
||||
// User : "root",
|
||||
// Pass : "123456",
|
||||
// Name : "test",
|
||||
// Type : "mysql",
|
||||
// Role : "master",
|
||||
// Priority : 100,
|
||||
// },
|
||||
// },
|
||||
//})
|
||||
//db, _ = gdb.Instance()
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 创建测试数据库
|
||||
func create() {
|
||||
fmt.Println("create:")
|
||||
_, err := db.Exec("CREATE DATABASE IF NOT EXISTS test")
|
||||
if (err != nil) {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
s := `
|
||||
CREATE TABLE IF NOT EXISTS user (
|
||||
uid INT(10) UNSIGNED AUTO_INCREMENT,
|
||||
name VARCHAR(45),
|
||||
PRIMARY KEY (uid)
|
||||
)
|
||||
ENGINE = InnoDB
|
||||
DEFAULT CHARACTER SET = utf8
|
||||
`
|
||||
_, err = db.Exec(s)
|
||||
if (err != nil) {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
s = `
|
||||
CREATE TABLE IF NOT EXISTS user_detail (
|
||||
uid INT(10) UNSIGNED AUTO_INCREMENT,
|
||||
site VARCHAR(255),
|
||||
PRIMARY KEY (uid)
|
||||
)
|
||||
ENGINE = InnoDB
|
||||
DEFAULT CHARACTER SET = utf8
|
||||
`
|
||||
|
||||
_, err = db.Exec(s)
|
||||
if (err != nil) {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 数据写入
|
||||
func insert() {
|
||||
fmt.Println("insert:")
|
||||
r, err := db.Insert("user", &gdb.Map {
|
||||
"name": "john",
|
||||
})
|
||||
if (err == nil) {
|
||||
uid, err2 := r.LastInsertId()
|
||||
if err2 == nil {
|
||||
r, err = db.Insert("user_detail", &gdb.Map {
|
||||
"uid" : uid,
|
||||
"site" : "http://johng.cn",
|
||||
})
|
||||
if err == nil {
|
||||
fmt.Printf("uid: %d\n", uid)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
} else {
|
||||
fmt.Println(err2)
|
||||
}
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
|
||||
// 基本sql查询
|
||||
func query() {
|
||||
fmt.Println("query:")
|
||||
list, err := db.GetAll("select * from user limit 2")
|
||||
if err == nil {
|
||||
fmt.Println(list)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// replace into
|
||||
func replace() {
|
||||
fmt.Println("replace:")
|
||||
r, err := db.Save("user", &gdb.Map {
|
||||
"uid" : 1,
|
||||
"name" : "john",
|
||||
})
|
||||
if (err == nil) {
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 数据保存
|
||||
func save() {
|
||||
fmt.Println("save:")
|
||||
r, err := db.Save("user", &gdb.Map {
|
||||
"uid" : 1,
|
||||
"name" : "john",
|
||||
})
|
||||
if (err == nil) {
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 批量写入
|
||||
func batchInsert() {
|
||||
fmt.Println("batchInsert:")
|
||||
err := db.BatchInsert("user", &gdb.List {
|
||||
{"name": "john_" + strconv.FormatInt(time.Now().UnixNano(), 10)},
|
||||
{"name": "john_" + strconv.FormatInt(time.Now().UnixNano(), 10)},
|
||||
{"name": "john_" + strconv.FormatInt(time.Now().UnixNano(), 10)},
|
||||
{"name": "john_" + strconv.FormatInt(time.Now().UnixNano(), 10)},
|
||||
}, 10)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 数据更新
|
||||
func update1() {
|
||||
fmt.Println("update1:")
|
||||
r, err := db.Update("user", &gdb.Map {"name": "john1"}, "uid=?", 1)
|
||||
if (err == nil) {
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 数据更新
|
||||
func update2() {
|
||||
fmt.Println("update2:")
|
||||
r, err := db.Update("user", "name='john2'", "uid=1")
|
||||
if (err == nil) {
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 数据更新
|
||||
func update3() {
|
||||
fmt.Println("update3:")
|
||||
r, err := db.Update("user", "name=?", "uid=?", "john2", 1)
|
||||
if (err == nil) {
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
|
||||
// 链式查询操作1
|
||||
func linkopSelect1() {
|
||||
fmt.Println("linkopSelect1:")
|
||||
r, err := db.Table("user u").LeftJoin("user_detail ud", "u.uid=ud.uid").Fields("u.*, ud.site").Condition("u.uid > ?", 1).Limit(0, 2).Select()
|
||||
if (err == nil) {
|
||||
fmt.Println(r)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 链式查询操作2
|
||||
func linkopSelect2() {
|
||||
fmt.Println("linkopSelect2:")
|
||||
r, err := db.Table("user u").LeftJoin("user_detail ud", "u.uid=ud.uid").Fields("u.*,ud.site").Condition("u.uid=?", 1).One()
|
||||
if (err == nil) {
|
||||
fmt.Println(r)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 链式查询操作3
|
||||
func linkopSelect3() {
|
||||
fmt.Println("linkopSelect3:")
|
||||
r, err := db.Table("user u").LeftJoin("user_detail ud", "u.uid=ud.uid").Fields("ud.site").Condition("u.uid=?", 1).Value()
|
||||
if (err == nil) {
|
||||
fmt.Println(r.(string))
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 错误操作
|
||||
func linkopUpdate1() {
|
||||
fmt.Println("linkopUpdate1:")
|
||||
r, err := db.Table("henghe_setting").Update()
|
||||
if (err == nil) {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 通过Map指针方式传参方式
|
||||
func linkopUpdate2() {
|
||||
fmt.Println("linkopUpdate2:")
|
||||
r, err := db.Table("user").Data(&gdb.Map{"name" : "john2"}).Condition("name=?", "john").Update()
|
||||
if (err == nil) {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 通过字符串方式传参
|
||||
func linkopUpdate3() {
|
||||
fmt.Println("linkopUpdate3:")
|
||||
r, err := db.Table("user").Data("name='john3'").Condition("name=?", "john2").Update()
|
||||
if (err == nil) {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 主从io复用测试,在mysql中使用 show full processlist 查看链接信息
|
||||
func keepPing() {
|
||||
fmt.Println("keepPing:")
|
||||
for {
|
||||
fmt.Println("ping...")
|
||||
err := db.PingMaster()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
err = db.PingSlave()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
time.Sleep(1*time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
// 数据库单例测试,在mysql中使用 show full processlist 查看链接信息
|
||||
func instance() {
|
||||
fmt.Println("instance:")
|
||||
db1, _ := gdb.Instance()
|
||||
db2, _ := gdb.Instance()
|
||||
db3, _ := gdb.Instance()
|
||||
for {
|
||||
fmt.Println("ping...")
|
||||
db1.PingMaster()
|
||||
db1.PingSlave()
|
||||
db2.PingMaster()
|
||||
db2.PingSlave()
|
||||
db3.PingMaster()
|
||||
db3.PingSlave()
|
||||
time.Sleep(1*time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func main() {
|
||||
|
||||
//create()
|
||||
//create()
|
||||
//insert()
|
||||
//query()
|
||||
//replace()
|
||||
//save()
|
||||
//batchInsert()
|
||||
//update1()
|
||||
//update2()
|
||||
//update3()
|
||||
//linkopSelect1()
|
||||
//linkopSelect2()
|
||||
//linkopSelect3()
|
||||
//linkopUpdate1()
|
||||
//linkopUpdate2()
|
||||
//linkopUpdate3()
|
||||
keepPing()
|
||||
}
|
||||
284
gexample/db/pgsql/pgsql.go
Normal file
284
gexample/db/pgsql/pgsql.go
Normal file
@ -0,0 +1,284 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"strconv"
|
||||
"gf/g/database/gdb"
|
||||
)
|
||||
|
||||
// 本文件用于gf框架的postgresql数据库操作示例,不作为单元测试使用
|
||||
|
||||
var db gdb.Link
|
||||
|
||||
func init () {
|
||||
gdb.AddDefaultConfigNode(gdb.ConfigNode {
|
||||
Host : "127.0.0.1",
|
||||
Port : 5432,
|
||||
User : "postgres",
|
||||
Pass : "123456",
|
||||
Name : "test",
|
||||
Type : "pgsql",
|
||||
})
|
||||
db, _ = gdb.Instance()
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 创建测试数据库
|
||||
func create() {
|
||||
fmt.Println("create:")
|
||||
_, err := db.Exec("CREATE SCHEMA IF NOT EXISTS \"test\"")
|
||||
if (err != nil) {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
s := `CREATE TABLE IF NOT EXISTS "user" (
|
||||
uid int PRIMARY KEY,
|
||||
name TEXT NOT NULL
|
||||
)
|
||||
`
|
||||
_, err = db.Exec(s)
|
||||
if (err != nil) {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
s = `
|
||||
CREATE TABLE IF NOT EXISTS user_detail (
|
||||
uid int PRIMARY KEY,
|
||||
site TEXT NOT NULL
|
||||
)
|
||||
`
|
||||
_, err = db.Exec(s)
|
||||
if (err != nil) {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 数据写入
|
||||
func insert() {
|
||||
fmt.Println("insert:")
|
||||
r, err := db.Insert("user", &gdb.Map {
|
||||
"uid" : 1,
|
||||
"name": "john",
|
||||
})
|
||||
if (err == nil) {
|
||||
uid, err2 := r.LastInsertId()
|
||||
if err2 == nil {
|
||||
r, err = db.Insert("user_detail", &gdb.Map {
|
||||
"uid" : string(uid),
|
||||
"site" : "http://johng.cn",
|
||||
})
|
||||
if err == nil {
|
||||
fmt.Printf("uid: %d\n", uid)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
} else {
|
||||
fmt.Println(err2)
|
||||
}
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
|
||||
// 基本sql查询
|
||||
func query() {
|
||||
fmt.Println("query:")
|
||||
list, err := db.GetAll("select * from \"user\"")
|
||||
if err == nil {
|
||||
fmt.Println(list)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// replace into
|
||||
func replace() {
|
||||
fmt.Println("replace:")
|
||||
r, err := db.Save("user", &gdb.Map {
|
||||
"uid": "1",
|
||||
"name": "john",
|
||||
})
|
||||
if (err == nil) {
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 数据保存
|
||||
func save() {
|
||||
fmt.Println("save:")
|
||||
r, err := db.Save("user", &gdb.Map {
|
||||
"uid" : "1",
|
||||
"name" : "john",
|
||||
})
|
||||
if (err == nil) {
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 批量写入
|
||||
func batchInsert() {
|
||||
fmt.Println("batchInsert:")
|
||||
err := db.BatchInsert("user", &gdb.List {
|
||||
{"name": "john_" + strconv.FormatInt(time.Now().UnixNano(), 10)},
|
||||
{"name": "john_" + strconv.FormatInt(time.Now().UnixNano(), 10)},
|
||||
{"name": "john_" + strconv.FormatInt(time.Now().UnixNano(), 10)},
|
||||
{"name": "john_" + strconv.FormatInt(time.Now().UnixNano(), 10)},
|
||||
}, 10)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 数据更新
|
||||
func update1() {
|
||||
fmt.Println("update1:")
|
||||
r, err := db.Update("user", &gdb.Map {"name": "john1"}, "uid=?", 1)
|
||||
if (err == nil) {
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 数据更新
|
||||
func update2() {
|
||||
fmt.Println("update2:")
|
||||
r, err := db.Update("user", "name='john2'", "uid=1")
|
||||
if (err == nil) {
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 数据更新
|
||||
func update3() {
|
||||
fmt.Println("update3:")
|
||||
r, err := db.Update("user", "name=?", "uid=?", "john2", 1)
|
||||
if (err == nil) {
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
|
||||
// 链式查询操作
|
||||
func linkopSelect() {
|
||||
fmt.Println("linkopSelect:")
|
||||
r, err := db.Table("user u").
|
||||
LeftJoin("user_detail ud", "u.uid=ud.uid").
|
||||
Fields("u.*, ud.site").
|
||||
Condition("u.uid > ?", 1).
|
||||
Limit(0, 2).Select()
|
||||
if (err == nil) {
|
||||
fmt.Println(r)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 错误操作
|
||||
func linkopUpdate1() {
|
||||
fmt.Println("linkopUpdate1:")
|
||||
r, err := db.Table("henghe_setting").Update()
|
||||
if (err == nil) {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 通过Map指针方式传参方式
|
||||
func linkopUpdate2() {
|
||||
fmt.Println("linkopUpdate2:")
|
||||
r, err := db.Table("user").Data(&gdb.Map{"name" : "john2"}).Condition("name=?", "john").Update()
|
||||
if (err == nil) {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 通过字符串方式传参
|
||||
func linkopUpdate3() {
|
||||
fmt.Println("linkopUpdate3:")
|
||||
r, err := db.Table("user").Data("name='john3'").Condition("name=?", "john2").Update()
|
||||
if (err == nil) {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 主从io复用测试,在mysql中使用 show full processlist 查看链接信息
|
||||
func keepPing() {
|
||||
fmt.Println("keepPing:")
|
||||
for {
|
||||
fmt.Println("ping...")
|
||||
db.PingMaster()
|
||||
db.PingSlave()
|
||||
time.Sleep(1*time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
// 数据库单例测试,在mysql中使用 show full processlist 查看链接信息
|
||||
func instance() {
|
||||
fmt.Println("instance:")
|
||||
db1, _ := gdb.Instance()
|
||||
db2, _ := gdb.Instance()
|
||||
db3, _ := gdb.Instance()
|
||||
for {
|
||||
fmt.Println("ping...")
|
||||
db1.PingMaster()
|
||||
db1.PingSlave()
|
||||
db2.PingMaster()
|
||||
db2.PingSlave()
|
||||
db3.PingMaster()
|
||||
db3.PingSlave()
|
||||
time.Sleep(1*time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func main() {
|
||||
create()
|
||||
create()
|
||||
insert()
|
||||
query()
|
||||
replace()
|
||||
save()
|
||||
batchInsert()
|
||||
update1()
|
||||
update2()
|
||||
update3()
|
||||
linkopSelect()
|
||||
linkopUpdate1()
|
||||
linkopUpdate2()
|
||||
linkopUpdate3()
|
||||
}
|
||||
20
gexample/encoding/ghash.go
Normal file
20
gexample/encoding/ghash.go
Normal file
@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"fmt"
|
||||
"gf/g/encoding/ghash"
|
||||
)
|
||||
|
||||
func main () {
|
||||
m := make(map[uint64]bool)
|
||||
for i := 0; i < 100000000; i++ {
|
||||
hash := ghash.BKDRHash64([]byte("key_" + strconv.Itoa(i)))
|
||||
if _, ok := m[hash]; ok {
|
||||
fmt.Printf("duplicated hash %d\n", hash)
|
||||
} else {
|
||||
m[hash] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
112
gexample/encoding/json.go
Normal file
112
gexample/encoding/json.go
Normal file
@ -0,0 +1,112 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
//"encoding/json"
|
||||
"gf/g/encoding/gjson"
|
||||
)
|
||||
|
||||
type City struct {
|
||||
Age string
|
||||
CityId int
|
||||
CityName string
|
||||
ProvinceId int
|
||||
//CityOrder int
|
||||
}
|
||||
|
||||
func main() {
|
||||
//data := `[{"CityId":1, "CityName":"北京", "ProvinceId":1, "CityOrder":1}, {"CityId":5, "CityName":"成都", "ProvinceId":27, "CityOrder":1}]`
|
||||
data := `{"name":"中国","age":31,"list":[["a","b","c"],["d","e","f"]],"items":{"title":"make\"he moon","name":"make'he moon","content":"'[}]{[}he moon"}}`
|
||||
//data := `[{"CityId":18,"CityName":"西安","ProvinceId":27,"CityOrder":1},{"CityId":53,"CityName":"广州","ProvinceId":27,"CityOrder":1}]`
|
||||
//data := `{"name" : "中国", "age" : 31, "items":[1,2,3]}`
|
||||
//data := `[["a","b","c"],["d","e","f"]]`
|
||||
//data := `["a","b","c"]`
|
||||
//json := `
|
||||
//[1,{"a":2},
|
||||
//{"a":{}},
|
||||
//{"a":[]},
|
||||
//{"a":[{}]},
|
||||
//{"{[a" : "\"2,:3," a ":33}]"}]` // 错误的json
|
||||
//data := `["a","b","c"` // 错误的json
|
||||
//data := `,{ "name" : "中国", "age" : 31, "items":[1,2]:}` //错误的json
|
||||
|
||||
v := gjson.DecodeToJson(&data)
|
||||
fmt.Println(v.GetNumber("list"))
|
||||
|
||||
//v := map[string]interface{} {
|
||||
//
|
||||
// "name" : "中国",
|
||||
// "age" : 11,
|
||||
// "list" : []interface{} {
|
||||
// 1,2,3,4,
|
||||
// },
|
||||
//}
|
||||
//r, _ := json.MarshalIndent(v, "", "\t")
|
||||
//fmt.Println(string(r))
|
||||
//s, _ := gjson.Encode(v)
|
||||
//fmt.Println(*s)
|
||||
|
||||
|
||||
//p, err := gjson.Decode(&data)
|
||||
//if err == nil {
|
||||
// //p.Print()
|
||||
// //fmt.Println(p.Get("0"))
|
||||
// fmt.Println(p.GetMap("0"))
|
||||
//} else {
|
||||
// fmt.Println(err)
|
||||
//}
|
||||
//fmt.Println()
|
||||
//fmt.Println()
|
||||
////v := make(map[string]interface{})
|
||||
////i := 31
|
||||
////j := "john"
|
||||
////v["age"] = i
|
||||
////v["name"] = make(map[string]interface{})
|
||||
////t := v["name"]
|
||||
////t.(map[string]interface{})["n"] = j
|
||||
////
|
||||
////fmt.Println(v)
|
||||
//var s struct{
|
||||
// v interface{}
|
||||
// p interface{}
|
||||
//}
|
||||
//v := make(map[string]interface{})
|
||||
//s.v = v
|
||||
//s.p = &v
|
||||
//c := (*s.p.(*map[string]interface{}))
|
||||
//c["name1"] = "john1"
|
||||
//
|
||||
//t := make(map[string]interface{})
|
||||
//c["/"] = t
|
||||
//s.p = &t
|
||||
//t["name2"] = "john2"
|
||||
//
|
||||
//c2 := (*s.p.(*map[string]interface{}))
|
||||
//c2["name3"] = "john3"
|
||||
//
|
||||
////t2[2] = 100
|
||||
//fmt.Println(s.v)
|
||||
|
||||
|
||||
//a := map[string]interface{} {
|
||||
// "name" : "john",
|
||||
// "list" : []interface{}{
|
||||
// 1,2,3, "fuck",
|
||||
// },
|
||||
// "item" : map[string]string {
|
||||
// "n1" : "v1",
|
||||
// "n2" : "v2",
|
||||
// "n3" : "v3",
|
||||
// },
|
||||
//}
|
||||
//fmt.Println(json.M)
|
||||
|
||||
//
|
||||
//var a = []int{1,2,3}
|
||||
//var b = []int{4,5,6, 7,8}
|
||||
//cc := make([]int, len(a) + 12)
|
||||
//a = cc
|
||||
//copy(a, b)
|
||||
//fmt.Println(a)
|
||||
//fmt.Println(b)
|
||||
}
|
||||
31
gexample/encoding/json_test.go
Normal file
31
gexample/encoding/json_test.go
Normal file
@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"gf/g/encoding/gjson"
|
||||
"encoding/json"
|
||||
"log"
|
||||
)
|
||||
|
||||
// go test json_test.go -bench=".*"
|
||||
|
||||
var data = `[{"CityId":1, "CityName":"北京", "ProvinceId":1, "CityOrder":1}, {"CityId":5, "CityName":"成都", "ProvinceId":27, "CityOrder":1}]`
|
||||
|
||||
func BenchmarkJsonDecode(b *testing.B) {
|
||||
b.N = 1000000
|
||||
for i := 0; i < b.N; i++ {
|
||||
gjson.Decode(data)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJsonDecodeByUnmarshal(b *testing.B) {
|
||||
b.N = 1000000
|
||||
for i := 0; i < b.N; i++ {
|
||||
var citys interface{}
|
||||
if err := json.Unmarshal([]byte(data), &citys); err != nil {
|
||||
log.Fatalf("JSON unmarshaling failed: %s", err)
|
||||
}
|
||||
//fmt.Println(citys)
|
||||
}
|
||||
}
|
||||
|
||||
156
gexample/net/gluster_client.go
Normal file
156
gexample/net/gluster_client.go
Normal file
@ -0,0 +1,156 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
var kvUrl string = "http://192.168.2.102:4168/kv"
|
||||
var nodeUrl string = "http://192.168.2.102:4168/node"
|
||||
var serviceUrl string = "http://192.168.2.102:4168/service"
|
||||
|
||||
// kv操作
|
||||
func addKV() {
|
||||
c := ghttp.NewClient()
|
||||
r := c.Put(kvUrl, "{\"name1\":\"john1\", \"name2\":\"john2\"}")
|
||||
fmt.Println("addKV:", r.ReadAll())
|
||||
}
|
||||
|
||||
func getAllKV() {
|
||||
c := ghttp.NewClient()
|
||||
r := c.Get(kvUrl)
|
||||
fmt.Println("getAllKV:", r.ReadAll())
|
||||
}
|
||||
|
||||
func getOneKV() {
|
||||
c := ghttp.NewClient()
|
||||
r := c.Get(kvUrl + "?k=name1")
|
||||
fmt.Println("getOneKV:", r.ReadAll())
|
||||
}
|
||||
|
||||
func editKV() {
|
||||
c := ghttp.NewClient()
|
||||
r := c.Post(kvUrl, "{\"name1\":\"john3\", \"name2\":\"john4\"}")
|
||||
fmt.Println("editKV:", r.ReadAll())
|
||||
}
|
||||
|
||||
func removeKV() {
|
||||
c := ghttp.NewClient()
|
||||
r := c.Delete(kvUrl, "[\"name1\"]")
|
||||
fmt.Println("removeKV:", r.ReadAll())
|
||||
}
|
||||
|
||||
|
||||
// node操作
|
||||
func addNode() {
|
||||
c := ghttp.NewClient()
|
||||
r := c.Put(nodeUrl, "[\"172.17.42.1\"]")
|
||||
fmt.Println("addNode:", r.ReadAll())
|
||||
}
|
||||
|
||||
func getAllNode() {
|
||||
c := ghttp.NewClient()
|
||||
r := c.Get(nodeUrl)
|
||||
fmt.Println("getAllNode:", r.ReadAll())
|
||||
}
|
||||
|
||||
func removeNode() {
|
||||
c := ghttp.NewClient()
|
||||
r := c.Delete(nodeUrl, "[\"172.17.42.1\"]")
|
||||
fmt.Println("removeNode:", r.ReadAll())
|
||||
}
|
||||
|
||||
|
||||
// service操作
|
||||
func getAllService() {
|
||||
c := ghttp.NewClient()
|
||||
r := c.Get(serviceUrl)
|
||||
fmt.Println("getAllService:", r.ReadAll())
|
||||
}
|
||||
|
||||
func getOneService() {
|
||||
c := ghttp.NewClient()
|
||||
r := c.Get(serviceUrl + "?name=Site Database")
|
||||
fmt.Println("getOneService:", r.ReadAll())
|
||||
}
|
||||
|
||||
func addDatabaseService() {
|
||||
c := ghttp.NewClient()
|
||||
s := `
|
||||
{
|
||||
"name" : "Site Database",
|
||||
"type" : "mysql",
|
||||
"list" : [
|
||||
{"host":"192.168.2.102", "port":"3306", "user":"root", "pass":"123456", "database":"test"},
|
||||
{"host":"192.168.2.124", "port":"3306", "user":"root", "pass":"123456", "database":"tongwujie"}
|
||||
]
|
||||
}
|
||||
`
|
||||
r := c.Put(serviceUrl, s)
|
||||
fmt.Println("addDatabaseService:", r.ReadAll())
|
||||
}
|
||||
|
||||
func editDatabaseService() {
|
||||
c := ghttp.NewClient()
|
||||
s := `
|
||||
{
|
||||
"name" : "Site Database2",
|
||||
"type" : "mysql",
|
||||
"list" : [
|
||||
{"host":"192.168.2.102", "port":"3306", "user":"root", "pass":"123456", "database":"test"},
|
||||
{"host":"192.168.2.124", "port":"3306", "user":"root", "pass":"123456", "database":"tongwujie"}
|
||||
]
|
||||
}
|
||||
`
|
||||
r := c.Post(serviceUrl, s)
|
||||
fmt.Println("editDatabaseService:", r.ReadAll())
|
||||
}
|
||||
|
||||
func removeDatabaseService() {
|
||||
c := ghttp.NewClient()
|
||||
r := c.Delete(serviceUrl, "[\"Site Database2\"]")
|
||||
fmt.Println("removeDatabaseService:", r.ReadAll())
|
||||
}
|
||||
|
||||
|
||||
func addWebService() {
|
||||
c := ghttp.NewClient()
|
||||
s := `
|
||||
{
|
||||
"name" : "Site",
|
||||
"type" : "web",
|
||||
"list" : [
|
||||
{"url":"http://baidu.com", "check":"http://itsadeadlink.com"},
|
||||
{"url":"http://baidu.com"}
|
||||
]
|
||||
}
|
||||
`
|
||||
r := c.Put(serviceUrl, s)
|
||||
fmt.Println("addWebService:", r.ReadAll())
|
||||
}
|
||||
|
||||
func editWebService() {
|
||||
c := ghttp.NewClient()
|
||||
s := `
|
||||
{
|
||||
"name" : "Site2",
|
||||
"type" : "web",
|
||||
"list" : [
|
||||
{"url":"http://baidu.com"},
|
||||
{"url":"http://baidu.com"}
|
||||
]
|
||||
}
|
||||
`
|
||||
r := c.Post(serviceUrl, s)
|
||||
fmt.Println("editWebService:", r.ReadAll())
|
||||
}
|
||||
|
||||
func removeWebService() {
|
||||
c := ghttp.NewClient()
|
||||
r := c.Delete(serviceUrl, "[\"Site2\"]")
|
||||
fmt.Println("removeWebService:", r.ReadAll())
|
||||
}
|
||||
|
||||
func main() {
|
||||
addWebService()
|
||||
}
|
||||
14
gexample/net/http_client.go
Normal file
14
gexample/net/http_client.go
Normal file
@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
|
||||
func main() {
|
||||
c := ghttp.NewClient()
|
||||
r := c.Get("http://192.168.2.124")
|
||||
|
||||
fmt.Println(r.StatusCode)
|
||||
}
|
||||
29
gexample/net/http_server.go
Normal file
29
gexample/net/http_server.go
Normal file
@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"io"
|
||||
"gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
func HelloServer1(w http.ResponseWriter, r *http.Request) {
|
||||
io.WriteString(w, "hello1!\n")
|
||||
}
|
||||
func HelloServer2(w http.ResponseWriter, r *http.Request) {
|
||||
io.WriteString(w, "hello2\n")
|
||||
}
|
||||
func main() {
|
||||
s := ghttp.New()
|
||||
s.SetAddr(":8199")
|
||||
s.SetIndexFolder(true)
|
||||
s.SetServerRoot("/home/john/Workspace/")
|
||||
s.BindHandleByMap(ghttp.HandlerMap {
|
||||
"/h": HelloServer1,
|
||||
"/h1": HelloServer1,
|
||||
"/h2": HelloServer1,
|
||||
"/h3": HelloServer1,
|
||||
})
|
||||
s.BindHandle("/hello1", HelloServer1)
|
||||
s.BindHandle("/hello2", HelloServer2)
|
||||
s.Run()
|
||||
}
|
||||
25
gexample/net/raft.go
Normal file
25
gexample/net/raft.go
Normal file
@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gf/g/net/graft"
|
||||
"gf/g/net/gip"
|
||||
"log"
|
||||
)
|
||||
|
||||
|
||||
|
||||
func main() {
|
||||
ips, err := gip.IntranetIP()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, ip := range ips {
|
||||
//fmt.Println(ip)
|
||||
graft.NewServerByIp(ip).Run()
|
||||
}
|
||||
select {
|
||||
|
||||
}
|
||||
}
|
||||
29
gexample/net/raft_client.go
Normal file
29
gexample/net/raft_client.go
Normal file
@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net"
|
||||
"log"
|
||||
"gf/g/net/graft"
|
||||
"fmt"
|
||||
"gf/g/encoding/gjson"
|
||||
)
|
||||
|
||||
func rpcLogSet() {
|
||||
conn, err := net.Dial("tcp", "192.168.2.124:4167")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
entry := graft.LogRequest{}
|
||||
entry.Key = "name3"
|
||||
entry.Value = "john3"
|
||||
fmt.Println(*gjson.Encode(entry))
|
||||
e := graft.SendMsg(conn, 100, *gjson.Encode(entry))
|
||||
fmt.Println(e)
|
||||
conn.Close()
|
||||
}
|
||||
|
||||
func main() {
|
||||
rpcLogSet()
|
||||
}
|
||||
23
gexample/net/scanner.go
Normal file
23
gexample/net/scanner.go
Normal file
@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gf/g/net/gscanner"
|
||||
"net"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
//gscanner.New().SetTimeout(3*time.Second).ScanIp("192.168.2.1", "192.168.2.255", 80, func(conn net.Conn){
|
||||
// fmt.Println(conn.RemoteAddr())
|
||||
//})
|
||||
|
||||
gscanner.New().SetTimeout(3*time.Second).ScanIp("120.76.249.1", "120.76.249.255", 80, func(conn net.Conn){
|
||||
fmt.Println(conn.RemoteAddr())
|
||||
})
|
||||
|
||||
//gscanner.New().SetTimeout(6*time.Second).ScanPort("120.76.249.2", func(conn net.Conn){
|
||||
// //fmt.Println("yes")
|
||||
// fmt.Println(conn.RemoteAddr())
|
||||
//})
|
||||
}
|
||||
47
gexample/net/tcp_server.go
Normal file
47
gexample/net/tcp_server.go
Normal file
@ -0,0 +1,47 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net"
|
||||
"fmt"
|
||||
"gf/g/net/gtcp"
|
||||
"io"
|
||||
"log"
|
||||
"time"
|
||||
"gf/g/util/gutil"
|
||||
)
|
||||
|
||||
func main() {
|
||||
gtcp.NewServer(":8999", func(conn net.Conn) {
|
||||
|
||||
|
||||
try := 0
|
||||
buffersize := 5
|
||||
data := make([]byte, 0)
|
||||
for {
|
||||
buffer := make([]byte, buffersize)
|
||||
length, err := conn.Read(buffer)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
if err != io.EOF {
|
||||
log.Println("node recieve:", err, "try:", try)
|
||||
}
|
||||
if try > 2 {
|
||||
break;
|
||||
}
|
||||
try ++
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
} else {
|
||||
if length == buffersize {
|
||||
data = gutil.MergeSlice(data, buffer)
|
||||
} else {
|
||||
data = gutil.MergeSlice(data, buffer[0:length])
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println(string(data))
|
||||
}).Run()
|
||||
select {
|
||||
|
||||
}
|
||||
}
|
||||
21
gexample/net/udp.go
Normal file
21
gexample/net/udp.go
Normal file
@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
conn, err := net.Dial("udp", "127.0.0.1:8999")
|
||||
defer conn.Close()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
conn.Write([]byte(""))
|
||||
var msg [20]byte
|
||||
n, err := conn.Read(msg[0:])
|
||||
|
||||
fmt.Println(string(msg[0:n]))
|
||||
}
|
||||
19
gexample/net/udp_server.go
Normal file
19
gexample/net/udp_server.go
Normal file
@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net"
|
||||
"fmt"
|
||||
"gf/g/net/gudp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
gudp.NewServer(":8999", func(conn *net.UDPConn) {
|
||||
var buf [1024]byte
|
||||
count, raddr, err := conn.ReadFromUDP(buf[0:])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
fmt.Println(raddr.String() + ":", string(buf[0:count]))
|
||||
_, err = conn.WriteToUDP([]byte("hi"), raddr)
|
||||
}).Run()
|
||||
}
|
||||
21
gexample/os/console.go
Normal file
21
gexample/os/console.go
Normal file
@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gf/g/os/gconsole"
|
||||
)
|
||||
|
||||
func doEcho() {
|
||||
fmt.Println("do echo")
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(gconsole.Value.GetAll())
|
||||
|
||||
fmt.Println(gconsole.Value.GetIndex(1))
|
||||
|
||||
gconsole.BindHandle("echo", doEcho)
|
||||
gconsole.RunHandle("echo")
|
||||
|
||||
gconsole.AutoRun()
|
||||
}
|
||||
47
gexample/os/file.go
Normal file
47
gexample/os/file.go
Normal file
@ -0,0 +1,47 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gf/g/os/gfile"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var dirpath1 = "/home/john/Workspace/temp/"
|
||||
var dirpath2 = "/home/john/Workspace/temp/1"
|
||||
var filepath1 = "/home/john/Workspace/temp/test.php"
|
||||
var filepath2 = "/tmp/tmp.test"
|
||||
|
||||
|
||||
type BinData struct{
|
||||
name string
|
||||
age int
|
||||
}
|
||||
|
||||
func info () {
|
||||
fmt.Println(gfile.Info(dirpath1))
|
||||
}
|
||||
|
||||
func scanDir() {
|
||||
files := gfile.ScanDir(dirpath1)
|
||||
fmt.Println(files)
|
||||
}
|
||||
|
||||
func getContents() {
|
||||
fmt.Printf("%s\n", gfile.GetContents(filepath1))
|
||||
}
|
||||
|
||||
func putContents() {
|
||||
fmt.Println(gfile.PutContentsAppend(filepath2, []byte("123")))
|
||||
}
|
||||
|
||||
func putBinContents() {
|
||||
data := []byte(BinData{"john", 31})
|
||||
fmt.Println(gfile.PutContents(filepath2, data))
|
||||
}
|
||||
|
||||
func main() {
|
||||
//info()
|
||||
//getContents()
|
||||
//putContents()
|
||||
putBinContents()
|
||||
//scanDir()
|
||||
}
|
||||
43
gexample/os/gcache_test.go
Normal file
43
gexample/os/gcache_test.go
Normal file
@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"gf/g/os/gcache"
|
||||
)
|
||||
|
||||
var cache *gcache.Cache = gcache.New()
|
||||
|
||||
func BenchmarkSet(b *testing.B) {
|
||||
b.N = 1000000
|
||||
for i := 0; i < 1000000; i ++ {
|
||||
cache.Set(string(i), i, 0)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSetWithExpire(b *testing.B) {
|
||||
b.N = 1000000
|
||||
for i := 0; i < 1000000; i ++ {
|
||||
cache.Set(string(i), i, 60)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGet1(b *testing.B) {
|
||||
b.N = 1000000
|
||||
for i := 0; i < 1000000; i ++ {
|
||||
cache.Get(string(i))
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGet2(b *testing.B) {
|
||||
b.N = 1000000
|
||||
for i := 0; i < 1000000; i ++ {
|
||||
cache.Get(string(i))
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkRemove(b *testing.B) {
|
||||
b.N = 1000000
|
||||
for i := 0; i < 1000000; i ++ {
|
||||
cache.Remove(string(i))
|
||||
}
|
||||
}
|
||||
70
gexample/os/gfilespace.go
Normal file
70
gexample/os/gfilespace.go
Normal file
@ -0,0 +1,70 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gf/g/os/gfilespace"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
|
||||
|
||||
func main() {
|
||||
|
||||
//t1 := gtime.Microsecond()
|
||||
space := gfilespace.New()
|
||||
|
||||
space.AddBlock(0, 10)
|
||||
space.AddBlock(11, 10)
|
||||
space.AddBlock(23, 10)
|
||||
fmt.Println(space.GetAllBlocks())
|
||||
fmt.Println(space.Contains(24, 10))
|
||||
//t1 := gtime.Microsecond()
|
||||
//for i := 1; i <= 10; i++ {
|
||||
// space.AddBlock(i*grand.Rand(0, 10000000), uint(i*10))
|
||||
// //space.AddBlock(i, uint(i*100))
|
||||
// //fmt.Println(space.GetAllBlocks())
|
||||
//}
|
||||
//fmt.Println("create", gtime.Microsecond() - t1)
|
||||
//e := space.Export()
|
||||
//space2 := gfilespace.New()
|
||||
//fmt.Println(e)
|
||||
//space2.Import(e)
|
||||
//fmt.Println(space2.Export())
|
||||
//t2 := gtime.Microsecond()
|
||||
//fmt.Println(space.GetBlock(10))
|
||||
//fmt.Println(space.GetBlock(10))
|
||||
//fmt.Println(space.GetBlock(10))
|
||||
//fmt.Println(space.GetBlock(10))
|
||||
//fmt.Println(space.GetBlock(10))
|
||||
//fmt.Println(space.GetBlock(10))
|
||||
//fmt.Println(space.GetBlock(10))
|
||||
//fmt.Println("get", gtime.Microsecond() - t2)
|
||||
//
|
||||
//fmt.Println(space.GetAllBlocks())
|
||||
//fmt.Println(space.GetAllSizes())
|
||||
|
||||
|
||||
|
||||
//add block: 1792 192
|
||||
//[{0 192} {512 192} {768 384} {1408 960}]
|
||||
//add block: 320 192
|
||||
//[{0 192} {320 192} {512 192} {768 384} {1408 960}]
|
||||
|
||||
//add mt block 1618432 64
|
||||
//[{1618432 64}]
|
||||
//[{1618432 64}]
|
||||
//add mt block 1618496 64
|
||||
//[{1618432 128}]
|
||||
//[{1618432 64}]
|
||||
//space.AddBlock(467264, 64)
|
||||
//space.AddBlock(467200, 128)
|
||||
|
||||
|
||||
|
||||
|
||||
//space.Empty()
|
||||
|
||||
//fmt.Println(gtime.Microsecond() - t1)
|
||||
|
||||
//fmt.Println(space.GetBlock(15))
|
||||
//fmt.Println(space.GetBlock(15))
|
||||
}
|
||||
60
gexample/other/reflect.go
Normal file
60
gexample/other/reflect.go
Normal file
@ -0,0 +1,60 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
//"reflect"
|
||||
"reflect"
|
||||
)
|
||||
//import "reflect"
|
||||
|
||||
type gtInterface interface {
|
||||
Run()
|
||||
}
|
||||
|
||||
type st struct {
|
||||
age int
|
||||
name string
|
||||
}
|
||||
|
||||
type mySt struct {
|
||||
st
|
||||
}
|
||||
|
||||
func (_ st) Echo(str string) {
|
||||
fmt.Printf("echo(%s)\n", str)
|
||||
}
|
||||
func (_ *st) Echo2(str string) {
|
||||
fmt.Printf("echo2(%s)\n", str)
|
||||
}
|
||||
|
||||
func (_ st) Echo3() {
|
||||
fmt.Println("echo3()")
|
||||
}
|
||||
|
||||
func Echo3() {
|
||||
fmt.Println("echo3()")
|
||||
}
|
||||
|
||||
type DefaultFunc func()
|
||||
|
||||
func Call(i DefaultFunc) {
|
||||
i()
|
||||
//reflect.ValueOf(i).Call([]reflect.Value{})
|
||||
}
|
||||
func main() {
|
||||
s := st {16,"john"}
|
||||
|
||||
|
||||
//p := reflect.ValueOf("halloo")
|
||||
v := reflect.ValueOf(s)
|
||||
//v2 := reflect.ValueOf(&s)
|
||||
//// 调用st结构体的方法
|
||||
//v.MethodByName("Echo").Call([]reflect.Value{p})
|
||||
//// 我们需要调用的是实体结构体指针的方法,注意v2与v2的区别,以及方法定义的区别
|
||||
//v2.MethodByName("Echo2").Call([]reflect.Value{p})
|
||||
//v.MethodByName()
|
||||
fmt.Println(v.Type())
|
||||
|
||||
|
||||
|
||||
}
|
||||
261
gexample/other/test.go
Normal file
261
gexample/other/test.go
Normal file
@ -0,0 +1,261 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
func main() {
|
||||
var mu sync.RWMutex
|
||||
|
||||
mu.Lock()
|
||||
mu.RLock()
|
||||
|
||||
fmt.Println(1)
|
||||
|
||||
mu.RUnlock()
|
||||
mu.Unlock()
|
||||
|
||||
return
|
||||
//a := 1497965
|
||||
//for i := 0; i < 10000; i++ {
|
||||
// fmt.Println(a*i)
|
||||
//}
|
||||
//
|
||||
//fmt.Println(math.MaxUint64)
|
||||
//m := make(map[int]int, 0)
|
||||
//for i := 0; i < 10000000; i ++ {
|
||||
// m[i] = i
|
||||
//}
|
||||
//t1 := gtime.Microsecond()
|
||||
//for i := 0; i < 10; i ++ {
|
||||
// if _, ok := m[i]; ok {
|
||||
//
|
||||
// }
|
||||
//}
|
||||
|
||||
//b := make([]byte, 100000)
|
||||
//removeBlock(b, 80000)
|
||||
//fmt.Println(gtime.Microsecond() - t1)
|
||||
|
||||
return
|
||||
//slice := []int{1,2,3,4,5,6,7,8,9}
|
||||
//index := 1
|
||||
////fmt.Println(append(slice[:index], slice[index+1:]...))
|
||||
//
|
||||
////rear:=append([]int{}, slice[index:]...)
|
||||
////slice=append(slice[0:index], 88)
|
||||
////slice=append(slice, rear...)
|
||||
////
|
||||
////fmt.Println(slice)
|
||||
//
|
||||
//fmt.Println(append(append(slice[0 : index], 88), append([]int{}, slice[index : ]...)...))
|
||||
//return
|
||||
//a := gbinary.EncodeBits(nil, 100, 10)
|
||||
//fmt.Println(a)
|
||||
//b := gbinary.EncodeBitsToBytes(a)
|
||||
//fmt.Println(b)
|
||||
//fmt.Println(gbinary.EncodeInt32(1))
|
||||
//return
|
||||
//return
|
||||
|
||||
//fmt.Println(gbinary.DecodeToInt64([]byte{1}))
|
||||
//return
|
||||
//fmt.Println(gbinary.EncodeInt32(1)[0:3])
|
||||
//b := []int{1,2,3}
|
||||
//c := []int{4}
|
||||
//copy(b[1:], c)
|
||||
//fmt.Println(b)
|
||||
//return
|
||||
//space, err := gfilespace.New("/tmp/test")
|
||||
//if err != nil {
|
||||
// fmt.Println(err)
|
||||
//}
|
||||
//for i := 0; i < 10; i++ {
|
||||
// space.AddBlock(int64(i), uint32((i + 1)*10))
|
||||
//}
|
||||
//fmt.Println(space.GetBlock(50))
|
||||
//return
|
||||
|
||||
|
||||
//db.Set([]byte("1"), []byte(grand.RandStr(10)))
|
||||
//grand.RandStr(10)
|
||||
//db.Set([]byte("r88U89b6Vv"), []byte("john211111111111111111111111"))
|
||||
//db.Get([]byte("name2"))
|
||||
//fmt.Println(e)
|
||||
|
||||
//fmt.Println(string(v))
|
||||
//r := int32(binary.LittleEndian.Uint32(b))
|
||||
//fmt.Println(int32(r))
|
||||
//binary.BigEndian.Uint16(b)
|
||||
//gbinary.DecodeToInt32([]byte{1,2,3,4})
|
||||
//fmt.Println(gtime.Microsecond() - t1)
|
||||
//fmt.Println([]byte{byte(i)})
|
||||
|
||||
//b := make([]byte, 0)
|
||||
//a := ghash.BKDRHash([]byte("john"))
|
||||
//for i := 0; i < 1000; i++ {
|
||||
// r, e := gbinary.Encode([]byte("key_" + strconv.Itoa(i)), a, a)
|
||||
// if e != nil {
|
||||
// fmt.Println(e)
|
||||
// return
|
||||
// }
|
||||
// b = append(b, r...)
|
||||
//}
|
||||
//fmt.Printf("length: %d\n", len(b)/1024)
|
||||
//fmt.Printf("compressed: %d\n", len(gcompress.Zlib(b))/1024)
|
||||
//t1 := gtime.Microsecond()
|
||||
////gcompress.Zlib(b)
|
||||
//gbinary.Encode([]byte("key_" + strconv.Itoa(100)), a, a)
|
||||
//fmt.Println(gtime.Microsecond() - t1)
|
||||
//return
|
||||
//t1 := gtime.Second()
|
||||
//m := make(map[uint64]bool)
|
||||
//c := 0
|
||||
//for i := 0; i < 100000000; i++ {
|
||||
// key := ghash.SDBMHash64([]byte("this is test key" + strconv.Itoa(i)))
|
||||
// if _, ok := m[key]; ok {
|
||||
// c++
|
||||
// } else {
|
||||
// m[key] = true
|
||||
// }
|
||||
//}
|
||||
//fmt.Println(gtime.Second() - t1)
|
||||
//fmt.Println("conflicts:", c)
|
||||
//fmt.Println(ghash.BKDRHash([]byte("johnWRWEREWREWRWEREWRRRRRRRRRRRRRRRRRRRRRRRRRRRRR")))
|
||||
//t1 := gtime.Microsecond()
|
||||
//ghash.BKDRHash64([]byte("john"))
|
||||
////fmt.Println(ghash.ELFHash([]byte("john")))
|
||||
////fmt.Println(ghash.JSHash([]byte("john")))
|
||||
//fmt.Println(gtime.Microsecond() - t1)
|
||||
//fmt.Println(ghash.BKDRHash64([]byte("john29384723894723894789sdkjfhsjkdh")))
|
||||
//return
|
||||
//btree := gbtree.New(3)
|
||||
//t1 := gtime.Microsecond()
|
||||
//for i := 1; i <= 11; i++ {
|
||||
// btree.Set([]byte{byte(i)}, []byte{byte(i)})
|
||||
//}
|
||||
//fmt.Println(gtime.Microsecond() - t1)
|
||||
//btree.Print()
|
||||
//fmt.Println()
|
||||
//fmt.Println()
|
||||
//btree.Remove([]byte{11})
|
||||
//btree.Print()
|
||||
|
||||
//t2 := gtime.Microsecond()
|
||||
//btree.Get([]byte("key2"))
|
||||
//fmt.Println(btree.Get([]byte{200}))
|
||||
//fmt.Println(gtime.Microsecond() - t2)
|
||||
|
||||
//return
|
||||
////m := gmap.NewStringInterfaceMap()
|
||||
//t1 := gtime.Microsecond()
|
||||
//gcrc32.EncodeString("123")
|
||||
//fmt.Println(gtime.Microsecond() - t1)
|
||||
//return
|
||||
//db, err := leveldb.OpenFile("/tmp/lv.db", nil)
|
||||
//fmt.Println(err)
|
||||
//defer db.Close()
|
||||
//t1 := gtime.Microsecond()
|
||||
//size := 10000000
|
||||
|
||||
//for i := 0; i < size; i++ {
|
||||
// //r := []byte(grand.RandStr(10))
|
||||
// //if err := db.Set(r, r); err != nil {
|
||||
// t3 := gtime.Microsecond()
|
||||
// if err := db.Put([]byte("key1_" + strconv.Itoa(i)), []byte("value1_" + strconv.Itoa(i)), nil); err != nil {
|
||||
// //if err := db.Set(gbinary.EncodeInt32(int32(i)), gbinary.EncodeInt32(int32(i))); err != nil {
|
||||
// fmt.Println(err)
|
||||
// }
|
||||
// t4 := gtime.Microsecond()
|
||||
// if t4 - t3 > 1000 {
|
||||
// fmt.Println(t4-t3)
|
||||
// }
|
||||
//}
|
||||
|
||||
//for i := 0; i < size; i++ {
|
||||
// //r := []byte(grand.RandStr(10))
|
||||
// //if err := db.Set(r, r); err != nil {
|
||||
// t3 := gtime.Microsecond()
|
||||
// v, err := db.Get([]byte("key1_" + strconv.Itoa(i)), nil)
|
||||
// if err != nil {
|
||||
// //if err := db.Set(gbinary.EncodeInt32(int32(i)), gbinary.EncodeInt32(int32(i))); err != nil {
|
||||
// fmt.Println(err)
|
||||
// }
|
||||
// if len(v) == 0 {
|
||||
// fmt.Println("none")
|
||||
// }
|
||||
// t4 := gtime.Microsecond()
|
||||
// if t4 - t3 > 1000 {
|
||||
// fmt.Println(t4-t3)
|
||||
// }
|
||||
//}
|
||||
//fmt.Println(gtime.Microsecond() - t1)
|
||||
//
|
||||
//return
|
||||
//db, err := bolt.Open("/tmp/my.db", 0600, nil)
|
||||
//if err != nil {
|
||||
// log.Fatal(err)
|
||||
//}
|
||||
//defer db.Close()
|
||||
//
|
||||
//tx, err := db.Begin(true)
|
||||
//if err != nil {
|
||||
// log.Fatal(err)
|
||||
//}
|
||||
//defer tx.Rollback()
|
||||
|
||||
// Use the transaction...
|
||||
//_, err = tx.CreateBucket([]byte("MyBucket"))
|
||||
//if err != nil {
|
||||
// log.Fatal(err)
|
||||
//}
|
||||
|
||||
// Commit the transaction and check for error.
|
||||
//if err := tx.Commit(); err != nil {
|
||||
// log.Fatal(err)
|
||||
//}
|
||||
//t1 := gtime.Microsecond()
|
||||
//db.Update(func(tx *bolt.Tx) error {
|
||||
// b := tx.Bucket([]byte("MyBucket"))
|
||||
// err := b.Put([]byte("answer"), []byte("11"))
|
||||
// return err
|
||||
//})
|
||||
//fmt.Println(gtime.Microsecond() - t1)
|
||||
//
|
||||
//t2 := gtime.Microsecond()
|
||||
//db.View(func(tx *bolt.Tx) error {
|
||||
// b := tx.Bucket([]byte("MyBucket"))
|
||||
// v := b.Get([]byte("answer"))
|
||||
// fmt.Printf("The answer is: %s\n", v)
|
||||
// return nil
|
||||
//})
|
||||
//fmt.Println(gtime.Microsecond() - t2)
|
||||
|
||||
|
||||
//return
|
||||
////db, err := gkvdb.New("/tmp/test2", "t")
|
||||
//fmt.Println(err)
|
||||
////fmt.Println(db.Set("1", []byte("1")))
|
||||
//t1 := gtime.Microsecond()
|
||||
////fmt.Println(db.Get("1"))
|
||||
//fmt.Println(db.Set("1", []byte("1")))
|
||||
//fmt.Println(gtime.Microsecond() - t1)
|
||||
////fmt.Println(db.Set("name", []byte("222")))
|
||||
//return
|
||||
//for i := 0; i < 10000000; i++ {
|
||||
// gfile.PutContentsAppend("/tmp/test", "1234567890")
|
||||
//}
|
||||
//
|
||||
//file, _ := gfile.OpenWithFlag("/tmp/test", os.O_RDWR|os.O_CREATE)
|
||||
//fmt.Println(gfile.GetBinContentByTwoOffsets(file, 100, 110))
|
||||
////n, err := file.WriteAt([]byte("123"), 2286 445 522*8)
|
||||
////n, err := file.WriteAt([]byte("123"), 1000000*(16))
|
||||
////fmt.Println(n)
|
||||
////fmt.Println(err)
|
||||
//defer file.Close()
|
||||
//fmt.Println(gcrc32.EncodeString("123"))
|
||||
}
|
||||
37
gexample/other/test_test.go
Normal file
37
gexample/other/test_test.go
Normal file
@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"gf/g/core/types/gmap"
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
func BenchmarkSet1(b *testing.B) {
|
||||
a := gmap.NewStringStringMap()
|
||||
m1 := make(map[string]string)
|
||||
m2 := make(map[string]string)
|
||||
for i := 0; i < 1000000; i ++ {
|
||||
m1[string(i)] = string(i)
|
||||
}
|
||||
for i := 0; i < 1000000; i ++ {
|
||||
m2[string(i)] = string(i) + "_2"
|
||||
}
|
||||
a.BatchSet(m1)
|
||||
a.BatchSet(m2)
|
||||
}
|
||||
|
||||
func BenchmarkSet2 (b *testing.B) {
|
||||
a := gmap.NewStringStringMap()
|
||||
m1 := make(map[string]string)
|
||||
m2 := make(map[string]string)
|
||||
for i := 0; i < 1000000; i ++ {
|
||||
m1[string(i)] = string(i)
|
||||
}
|
||||
for i := 0; i < 1000000; i ++ {
|
||||
m2[string(i)] = string(i) + "_2"
|
||||
}
|
||||
a.BatchSet2(m1)
|
||||
a.BatchSet2(m2)
|
||||
}
|
||||
44
gexample/types/gbtree.go
Normal file
44
gexample/types/gbtree.go
Normal file
@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gf/g/core/types/gbtree"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Block struct {
|
||||
index int // 文件偏移量
|
||||
size uint // 区块大小(byte)
|
||||
}
|
||||
|
||||
func (block *Block) Less(item gbtree.Item) bool {
|
||||
if block.index < item.(*Block).index {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func main () {
|
||||
tr := gbtree.New(10)
|
||||
|
||||
//t1 := gtime.Microsecond()
|
||||
for i := 0; i < 10; i++ {
|
||||
tr.ReplaceOrInsert(&Block{i, uint(i*10)})
|
||||
}
|
||||
//fmt.Println("create", gtime.Microsecond() - t1)
|
||||
|
||||
//t2 := gtime.Microsecond()
|
||||
//b := &Block{9, 10}
|
||||
//fmt.Println(tr.Get(b))
|
||||
//fmt.Println(tr.Delete(b))
|
||||
//fmt.Println(tr.Get(b))
|
||||
//fmt.Println("get", gtime.Microsecond() - t2)
|
||||
|
||||
//t3 := gtime.Microsecond()
|
||||
//var b Block
|
||||
tr.AscendGreaterOrEqual(&Block{2, 0}, func(item gbtree.Item) bool {
|
||||
fmt.Println(item)
|
||||
return true
|
||||
})
|
||||
//fmt.Println("asc fetch", gtime.Microsecond() - t3, b)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user