mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
gofmt
This commit is contained in:
@ -3,15 +3,14 @@ package main
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
array := []uint{1, 1, 2, 3, 3, 4, 4, 5, 5, 6, 6}
|
||||
for i := 0; i < len(array) - 1; i++ {
|
||||
for j := i + 1; j < len(array); j++ {
|
||||
if array[i] == array[j] {
|
||||
array = append(array[ : j], array[j + 1 : ]...)
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println(array)
|
||||
array := []uint{1, 1, 2, 3, 3, 4, 4, 5, 5, 6, 6}
|
||||
for i := 0; i < len(array)-1; i++ {
|
||||
for j := i + 1; j < len(array); j++ {
|
||||
if array[i] == array[j] {
|
||||
array = append(array[:j], array[j+1:]...)
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println(array)
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,59 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/container/garray"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/container/garray"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 创建普通的int类型数组,并关闭默认的并发安全特性
|
||||
a := garray.NewIntArray(true)
|
||||
|
||||
func main () {
|
||||
// 创建普通的int类型数组,并关闭默认的并发安全特性
|
||||
a := garray.NewIntArray(true)
|
||||
// 添加数据项
|
||||
for i := 0; i < 10; i++ {
|
||||
a.Append(i)
|
||||
}
|
||||
|
||||
// 添加数据项
|
||||
for i := 0; i < 10; i++ {
|
||||
a.Append(i)
|
||||
}
|
||||
// 获取当前数组长度
|
||||
fmt.Println(a.Len())
|
||||
|
||||
// 获取当前数组长度
|
||||
fmt.Println(a.Len())
|
||||
// 获取当前数据项列表
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// 获取当前数据项列表
|
||||
fmt.Println(a.Slice())
|
||||
// 获取指定索引项
|
||||
fmt.Println(a.Get(6))
|
||||
|
||||
// 获取指定索引项
|
||||
fmt.Println(a.Get(6))
|
||||
// 在指定索引前插入数据项
|
||||
a.InsertAfter(9, 11)
|
||||
// 在指定索引后插入数据项
|
||||
a.InsertBefore(10, 10)
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// 在指定索引前插入数据项
|
||||
a.InsertAfter(9, 11)
|
||||
// 在指定索引后插入数据项
|
||||
a.InsertBefore(10, 10)
|
||||
fmt.Println(a.Slice())
|
||||
// 修改指定索引的数据项
|
||||
a.Set(0, 100)
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// 修改指定索引的数据项
|
||||
a.Set(0, 100)
|
||||
fmt.Println(a.Slice())
|
||||
// 搜索数据项,返回搜索到的索引位置
|
||||
fmt.Println(a.Search(5))
|
||||
|
||||
// 搜索数据项,返回搜索到的索引位置
|
||||
fmt.Println(a.Search(5))
|
||||
// 删除指定索引的数据项
|
||||
a.Remove(0)
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// 删除指定索引的数据项
|
||||
a.Remove(0)
|
||||
fmt.Println(a.Slice())
|
||||
// 并发安全,写锁操作
|
||||
a.LockFunc(func(array []int) {
|
||||
// 将末尾项改为100
|
||||
array[len(array)-1] = 100
|
||||
})
|
||||
|
||||
// 并发安全,写锁操作
|
||||
a.LockFunc(func(array []int) {
|
||||
// 将末尾项改为100
|
||||
array[len(array) - 1] = 100
|
||||
})
|
||||
// 并发安全,读锁操作
|
||||
a.RLockFunc(func(array []int) {
|
||||
fmt.Println(array[len(array)-1])
|
||||
})
|
||||
|
||||
// 并发安全,读锁操作
|
||||
a.RLockFunc(func(array []int) {
|
||||
fmt.Println(array[len(array) - 1])
|
||||
})
|
||||
|
||||
// 清空数组
|
||||
fmt.Println(a.Slice())
|
||||
a.Clear()
|
||||
fmt.Println(a.Slice())
|
||||
// 清空数组
|
||||
fmt.Println(a.Slice())
|
||||
a.Clear()
|
||||
fmt.Println(a.Slice())
|
||||
}
|
||||
|
||||
@ -1,40 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/container/garray"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/container/garray"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 自定义排序数组,降序排序(SortedIntArray管理的数据是升序)
|
||||
a := garray.NewSortedArray(func(v1, v2 interface{}) int {
|
||||
if v1.(int) < v2.(int) {
|
||||
return 1
|
||||
}
|
||||
if v1.(int) > v2.(int) {
|
||||
return -1
|
||||
}
|
||||
return 0
|
||||
})
|
||||
|
||||
func main () {
|
||||
// 自定义排序数组,降序排序(SortedIntArray管理的数据是升序)
|
||||
a := garray.NewSortedArray(func(v1, v2 interface{}) int {
|
||||
if v1.(int) < v2.(int) {
|
||||
return 1
|
||||
}
|
||||
if v1.(int) > v2.(int) {
|
||||
return -1
|
||||
}
|
||||
return 0
|
||||
})
|
||||
// 添加数据
|
||||
a.Add(2)
|
||||
a.Add(3)
|
||||
a.Add(1)
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// 添加数据
|
||||
a.Add(2)
|
||||
a.Add(3)
|
||||
a.Add(1)
|
||||
fmt.Println(a.Slice())
|
||||
// 添加重复数据
|
||||
a.Add(3)
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// 添加重复数据
|
||||
a.Add(3)
|
||||
fmt.Println(a.Slice())
|
||||
// 检索数据,返回最后对比的索引位置,检索结果
|
||||
// 检索结果:0: 匹配; <0:参数小于对比值; >0:参数大于对比值
|
||||
fmt.Println(a.Search(1))
|
||||
|
||||
// 检索数据,返回最后对比的索引位置,检索结果
|
||||
// 检索结果:0: 匹配; <0:参数小于对比值; >0:参数大于对比值
|
||||
fmt.Println(a.Search(1))
|
||||
|
||||
// 设置不可重复
|
||||
a.SetUnique(true)
|
||||
fmt.Println(a.Slice())
|
||||
a.Add(1)
|
||||
fmt.Println(a.Slice())
|
||||
// 设置不可重复
|
||||
a.SetUnique(true)
|
||||
fmt.Println(a.Slice())
|
||||
a.Add(1)
|
||||
fmt.Println(a.Slice())
|
||||
}
|
||||
|
||||
@ -1,18 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/container/garray"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/container/garray"
|
||||
)
|
||||
|
||||
func main() {
|
||||
a := garray.NewIntArray()
|
||||
a.Append(1, 2, 3)
|
||||
|
||||
func main () {
|
||||
a := garray.NewIntArray()
|
||||
a.Append(1, 2, 3)
|
||||
v := a.Slice()
|
||||
v[0] = 4
|
||||
|
||||
v := a.Slice()
|
||||
v[0] = 4
|
||||
|
||||
g.Dump(a.Slice())
|
||||
g.Dump(v)
|
||||
g.Dump(a.Slice())
|
||||
g.Dump(v)
|
||||
}
|
||||
|
||||
@ -1,20 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/container/garray"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/container/garray"
|
||||
)
|
||||
|
||||
func main() {
|
||||
array := garray.NewSortedStringArray()
|
||||
array.Add("1")
|
||||
array.Add("2")
|
||||
array.Add("3")
|
||||
array.Add("4")
|
||||
array.Add("5")
|
||||
array.Add("6")
|
||||
array.Add("7")
|
||||
array.Add("8")
|
||||
array.Add("9")
|
||||
g.Dump(array.Slice())
|
||||
}
|
||||
func main() {
|
||||
array := garray.NewSortedStringArray()
|
||||
array.Add("1")
|
||||
array.Add("2")
|
||||
array.Add("3")
|
||||
array.Add("4")
|
||||
array.Add("5")
|
||||
array.Add("6")
|
||||
array.Add("7")
|
||||
array.Add("8")
|
||||
array.Add("9")
|
||||
g.Dump(array.Slice())
|
||||
}
|
||||
|
||||
@ -1,26 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/container/garray"
|
||||
"strings"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/container/garray"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
array := garray.NewSortedStringArray()
|
||||
array.Add("/api/ctl/show")
|
||||
array.Add("/api/ctl/post")
|
||||
array.Add("/api/obj/rest")
|
||||
array.Add("/api/handler")
|
||||
array.Add("/api/obj/delete")
|
||||
array.Add("/api/obj/show")
|
||||
array.Add("/api/obj/my-show")
|
||||
array.Add("/api/*")
|
||||
array.Add("/api/ctl/rest")
|
||||
array.Add("/api/ctl/my-show")
|
||||
g.Dump(array.Slice())
|
||||
func main() {
|
||||
array := garray.NewSortedStringArray()
|
||||
array.Add("/api/ctl/show")
|
||||
array.Add("/api/ctl/post")
|
||||
array.Add("/api/obj/rest")
|
||||
array.Add("/api/handler")
|
||||
array.Add("/api/obj/delete")
|
||||
array.Add("/api/obj/show")
|
||||
array.Add("/api/obj/my-show")
|
||||
array.Add("/api/*")
|
||||
array.Add("/api/ctl/rest")
|
||||
array.Add("/api/ctl/my-show")
|
||||
g.Dump(array.Slice())
|
||||
|
||||
fmt.Println(strings.Compare("/api/ctl/post", "/api/*"))
|
||||
fmt.Println(strings.Compare("/api/*", "/api/ctl/my-show"))
|
||||
}
|
||||
fmt.Println(strings.Compare("/api/ctl/post", "/api/*"))
|
||||
fmt.Println(strings.Compare("/api/*", "/api/ctl/my-show"))
|
||||
}
|
||||
|
||||
@ -1,73 +1,73 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/container/gmap"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/container/gmap"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 创建一个默认的gmap对象,
|
||||
// 默认情况下该gmap对象支持并发安全特性,
|
||||
// 初始化时可以给定false参数关闭并发安全特性,当做一个普通的map使用。
|
||||
m := gmap.New()
|
||||
// 创建一个默认的gmap对象,
|
||||
// 默认情况下该gmap对象支持并发安全特性,
|
||||
// 初始化时可以给定false参数关闭并发安全特性,当做一个普通的map使用。
|
||||
m := gmap.New()
|
||||
|
||||
// 设置键值对
|
||||
for i := 0; i < 10; i++ {
|
||||
m.Set(i, i)
|
||||
}
|
||||
// 查询大小
|
||||
fmt.Println(m.Size())
|
||||
// 批量设置键值对(不同的数据类型对象参数不同)
|
||||
m.BatchSet(map[interface{}]interface{}{
|
||||
10 : 10,
|
||||
11 : 11,
|
||||
})
|
||||
fmt.Println(m.Size())
|
||||
// 设置键值对
|
||||
for i := 0; i < 10; i++ {
|
||||
m.Set(i, i)
|
||||
}
|
||||
// 查询大小
|
||||
fmt.Println(m.Size())
|
||||
// 批量设置键值对(不同的数据类型对象参数不同)
|
||||
m.BatchSet(map[interface{}]interface{}{
|
||||
10: 10,
|
||||
11: 11,
|
||||
})
|
||||
fmt.Println(m.Size())
|
||||
|
||||
// 查询是否存在
|
||||
fmt.Println(m.Contains(1))
|
||||
// 查询是否存在
|
||||
fmt.Println(m.Contains(1))
|
||||
|
||||
// 查询键值
|
||||
fmt.Println(m.Get(1))
|
||||
// 查询键值
|
||||
fmt.Println(m.Get(1))
|
||||
|
||||
// 删除数据项
|
||||
m.Remove(9)
|
||||
fmt.Println(m.Size())
|
||||
// 删除数据项
|
||||
m.Remove(9)
|
||||
fmt.Println(m.Size())
|
||||
|
||||
// 批量删除
|
||||
m.BatchRemove([]interface{}{10, 11})
|
||||
fmt.Println(m.Size())
|
||||
// 批量删除
|
||||
m.BatchRemove([]interface{}{10, 11})
|
||||
fmt.Println(m.Size())
|
||||
|
||||
// 当前键名列表(随机排序)
|
||||
fmt.Println(m.Keys())
|
||||
// 当前键值列表(随机排序)
|
||||
fmt.Println(m.Values())
|
||||
// 当前键名列表(随机排序)
|
||||
fmt.Println(m.Keys())
|
||||
// 当前键值列表(随机排序)
|
||||
fmt.Println(m.Values())
|
||||
|
||||
// 查询键名,当键值不存在时,写入给定的默认值
|
||||
fmt.Println(m.GetWithDefault(100, 100))
|
||||
// 查询键名,当键值不存在时,写入给定的默认值
|
||||
fmt.Println(m.GetWithDefault(100, 100))
|
||||
|
||||
// 删除键值对,并返回对应的键值
|
||||
fmt.Println(m.GetAndRemove(100))
|
||||
// 删除键值对,并返回对应的键值
|
||||
fmt.Println(m.GetAndRemove(100))
|
||||
|
||||
// 遍历map
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
fmt.Printf("%v:%v ", k, v)
|
||||
return true
|
||||
})
|
||||
// 遍历map
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
fmt.Printf("%v:%v ", k, v)
|
||||
return true
|
||||
})
|
||||
|
||||
// 自定义写锁操作
|
||||
m.LockFunc(func(m map[interface{}]interface{}) {
|
||||
m[99] = 99
|
||||
})
|
||||
// 自定义写锁操作
|
||||
m.LockFunc(func(m map[interface{}]interface{}) {
|
||||
m[99] = 99
|
||||
})
|
||||
|
||||
// 自定义读锁操作
|
||||
m.RLockFunc(func(m map[interface{}]interface{}) {
|
||||
fmt.Println(m[99])
|
||||
})
|
||||
// 自定义读锁操作
|
||||
m.RLockFunc(func(m map[interface{}]interface{}) {
|
||||
fmt.Println(m[99])
|
||||
})
|
||||
|
||||
// 清空map
|
||||
m.Clear()
|
||||
// 清空map
|
||||
m.Clear()
|
||||
|
||||
// 判断map是否为空
|
||||
fmt.Println(m.IsEmpty())
|
||||
// 判断map是否为空
|
||||
fmt.Println(m.IsEmpty())
|
||||
}
|
||||
|
||||
@ -1,17 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/container/gmap"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/container/gmap"
|
||||
)
|
||||
|
||||
func main() {
|
||||
m := gmap.New()
|
||||
m.Set("1", "1")
|
||||
m := gmap.New()
|
||||
m.Set("1", "1")
|
||||
|
||||
m1 := m.Clone()
|
||||
m1["2"] = "2"
|
||||
m1 := m.Clone()
|
||||
m1["2"] = "2"
|
||||
|
||||
g.Dump(m.Clone())
|
||||
g.Dump(m1)
|
||||
g.Dump(m.Clone())
|
||||
g.Dump(m1)
|
||||
}
|
||||
|
||||
@ -1,39 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
"fmt"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 验证 map 的delete方法是否并发安全
|
||||
func main() {
|
||||
// 创建一个初始化的map
|
||||
m := make(map[int]int)
|
||||
for i := 0; i < 10000; i++ {
|
||||
m[i] = i
|
||||
}
|
||||
// 创建一个初始化的map
|
||||
m := make(map[int]int)
|
||||
for i := 0; i < 10000; i++ {
|
||||
m[i] = i
|
||||
}
|
||||
|
||||
fmt.Println("map size:", len(m))
|
||||
fmt.Println("map size:", len(m))
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
ev := make(chan struct{}, 0)
|
||||
wg := sync.WaitGroup{}
|
||||
ev := make(chan struct{}, 0)
|
||||
|
||||
// 创建10个并发的goroutine,使用ev控制并发开始事件,更容易模拟data race
|
||||
for i := 0; i < 10; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
<- ev
|
||||
fmt.Println("start")
|
||||
for i := 0; i < 10000; i++ {
|
||||
delete(m, i)
|
||||
}
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
// 创建10个并发的goroutine,使用ev控制并发开始事件,更容易模拟data race
|
||||
for i := 0; i < 10; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
<-ev
|
||||
fmt.Println("start")
|
||||
for i := 0; i < 10000; i++ {
|
||||
delete(m, i)
|
||||
}
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(time.Second)
|
||||
|
||||
close(ev)
|
||||
wg.Wait()
|
||||
close(ev)
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
@ -1,25 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/container/gpool"
|
||||
"fmt"
|
||||
"time"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/container/gpool"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main () {
|
||||
// 创建一个对象池,过期时间为1000毫秒
|
||||
p := gpool.New(1000, nil)
|
||||
func main() {
|
||||
// 创建一个对象池,过期时间为1000毫秒
|
||||
p := gpool.New(1000, nil)
|
||||
|
||||
// 从池中取一个对象,返回nil及错误信息
|
||||
fmt.Println(p.Get())
|
||||
// 从池中取一个对象,返回nil及错误信息
|
||||
fmt.Println(p.Get())
|
||||
|
||||
// 丢一个对象到池中
|
||||
p.Put(1)
|
||||
// 丢一个对象到池中
|
||||
p.Put(1)
|
||||
|
||||
// 重新从池中取一个对象,返回1
|
||||
fmt.Println(p.Get())
|
||||
// 重新从池中取一个对象,返回1
|
||||
fmt.Println(p.Get())
|
||||
|
||||
// 等待1秒后重试,发现对象已过期,返回nil及错误信息
|
||||
time.Sleep(time.Second)
|
||||
fmt.Println(p.Get())
|
||||
// 等待1秒后重试,发现对象已过期,返回nil及错误信息
|
||||
time.Sleep(time.Second)
|
||||
fmt.Println(p.Get())
|
||||
}
|
||||
|
||||
@ -1,33 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/os/gtimer"
|
||||
"time"
|
||||
"github.com/gogf/gf/g/os/gtime"
|
||||
"github.com/gogf/gf/g/container/gqueue"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/container/gqueue"
|
||||
"github.com/gogf/gf/g/os/gtime"
|
||||
"github.com/gogf/gf/g/os/gtimer"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
q := gqueue.New()
|
||||
// 数据生产者,每隔1秒往队列写数据
|
||||
gtimer.SetInterval(time.Second, func() {
|
||||
v := gtime.Now().String()
|
||||
q.Push(v)
|
||||
fmt.Println("Push:", v)
|
||||
})
|
||||
q := gqueue.New()
|
||||
// 数据生产者,每隔1秒往队列写数据
|
||||
gtimer.SetInterval(time.Second, func() {
|
||||
v := gtime.Now().String()
|
||||
q.Push(v)
|
||||
fmt.Println("Push:", v)
|
||||
})
|
||||
|
||||
// 3秒后关闭队列
|
||||
gtimer.SetTimeout(3*time.Second, func() {
|
||||
q.Close()
|
||||
})
|
||||
// 3秒后关闭队列
|
||||
gtimer.SetTimeout(3*time.Second, func() {
|
||||
q.Close()
|
||||
})
|
||||
|
||||
// 消费者,不停读取队列数据并输出到终端
|
||||
for {
|
||||
if v := q.Pop(); v != nil {
|
||||
fmt.Println(" Pop:", v)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
// 消费者,不停读取队列数据并输出到终端
|
||||
for {
|
||||
if v := q.Pop(); v != nil {
|
||||
fmt.Println(" Pop:", v)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,27 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/container/gqueue"
|
||||
"github.com/gogf/gf/g/os/gtimer"
|
||||
"time"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/container/gqueue"
|
||||
"github.com/gogf/gf/g/os/gtimer"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
q := gqueue.New()
|
||||
// 数据生产者,每隔1秒往队列写数据
|
||||
gtimer.SetInterval(time.Second, func() {
|
||||
for i := 0; i < 10; i++ {
|
||||
q.Push(i)
|
||||
}
|
||||
})
|
||||
q := gqueue.New()
|
||||
// 数据生产者,每隔1秒往队列写数据
|
||||
gtimer.SetInterval(time.Second, func() {
|
||||
for i := 0; i < 10; i++ {
|
||||
q.Push(i)
|
||||
}
|
||||
})
|
||||
|
||||
// 消费者,不停读取队列数据并输出到终端
|
||||
for {
|
||||
if v := q.Pop(); v != nil {
|
||||
fmt.Println(" Pop:", v)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
// 消费者,不停读取队列数据并输出到终端
|
||||
for {
|
||||
if v := q.Pop(); v != nil {
|
||||
fmt.Println(" Pop:", v)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,29 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/container/gqueue"
|
||||
"github.com/gogf/gf/g/os/gtime"
|
||||
"github.com/gogf/gf/g/os/gtimer"
|
||||
"time"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/container/gqueue"
|
||||
"github.com/gogf/gf/g/os/gtime"
|
||||
"github.com/gogf/gf/g/os/gtimer"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
queue := gqueue.New()
|
||||
// 数据生产者,每隔1秒往队列写数据
|
||||
gtimer.SetInterval(time.Second, func() {
|
||||
queue.Push(gtime.Now().String())
|
||||
})
|
||||
queue := gqueue.New()
|
||||
// 数据生产者,每隔1秒往队列写数据
|
||||
gtimer.SetInterval(time.Second, func() {
|
||||
queue.Push(gtime.Now().String())
|
||||
})
|
||||
|
||||
// 消费者,不停读取队列数据并输出到终端
|
||||
for {
|
||||
select {
|
||||
case v := <-queue.C:
|
||||
if v != nil {
|
||||
fmt.Println(v)
|
||||
} else {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
// 消费者,不停读取队列数据并输出到终端
|
||||
for {
|
||||
select {
|
||||
case v := <-queue.C:
|
||||
if v != nil {
|
||||
fmt.Println(v)
|
||||
} else {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,27 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/container/gring"
|
||||
"fmt"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/container/gring"
|
||||
)
|
||||
|
||||
func main() {
|
||||
r1 := gring.New(10)
|
||||
for i := 0; i < 5; i ++ {
|
||||
r1.Set(i).Next()
|
||||
}
|
||||
fmt.Println("Len:", r1.Len())
|
||||
fmt.Println("Cap:", r1.Cap())
|
||||
fmt.Println(r1.SlicePrev())
|
||||
fmt.Println(r1.SliceNext())
|
||||
r1 := gring.New(10)
|
||||
for i := 0; i < 5; i++ {
|
||||
r1.Set(i).Next()
|
||||
}
|
||||
fmt.Println("Len:", r1.Len())
|
||||
fmt.Println("Cap:", r1.Cap())
|
||||
fmt.Println(r1.SlicePrev())
|
||||
fmt.Println(r1.SliceNext())
|
||||
|
||||
r2 := gring.New(10)
|
||||
for i := 0; i < 10; i ++ {
|
||||
r2.Set(i).Next()
|
||||
}
|
||||
fmt.Println("Len:", r2.Len())
|
||||
fmt.Println("Cap:", r2.Cap())
|
||||
fmt.Println(r2.SlicePrev())
|
||||
fmt.Println(r2.SliceNext())
|
||||
r2 := gring.New(10)
|
||||
for i := 0; i < 10; i++ {
|
||||
r2.Set(i).Next()
|
||||
}
|
||||
fmt.Println("Len:", r2.Len())
|
||||
fmt.Println("Cap:", r2.Cap())
|
||||
fmt.Println(r2.SlicePrev())
|
||||
fmt.Println(r2.SliceNext())
|
||||
|
||||
}
|
||||
|
||||
@ -1,57 +1,57 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/container/gring"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/container/gring"
|
||||
)
|
||||
|
||||
type Player struct {
|
||||
position int // 位置
|
||||
alive bool // 是否存活
|
||||
position int // 位置
|
||||
alive bool // 是否存活
|
||||
}
|
||||
|
||||
const (
|
||||
playerCount = 41 // 玩家人数
|
||||
startPos = 1 // 开始报数位置
|
||||
playerCount = 41 // 玩家人数
|
||||
startPos = 1 // 开始报数位置
|
||||
)
|
||||
|
||||
var (
|
||||
deadline = 3
|
||||
deadline = 3
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 关闭并发安全,当前场景没有必要
|
||||
r := gring.New(playerCount, false)
|
||||
// 关闭并发安全,当前场景没有必要
|
||||
r := gring.New(playerCount, false)
|
||||
|
||||
// 设置所有玩家初始值
|
||||
for i := 1; i <= playerCount; i++ {
|
||||
r.Put(&Player{i, true})
|
||||
}
|
||||
// 设置所有玩家初始值
|
||||
for i := 1; i <= playerCount; i++ {
|
||||
r.Put(&Player{i, true})
|
||||
}
|
||||
|
||||
// 如果开始报数的位置不为1,则设置开始位置
|
||||
if startPos > 1 {
|
||||
r.Move(startPos - 1)
|
||||
}
|
||||
// 如果开始报数的位置不为1,则设置开始位置
|
||||
if startPos > 1 {
|
||||
r.Move(startPos - 1)
|
||||
}
|
||||
|
||||
counter := 1 // 报数从1开始,因为下面的循环从第二个开始计算
|
||||
deadCount := 0 // 死亡人数,初始值为0
|
||||
counter := 1 // 报数从1开始,因为下面的循环从第二个开始计算
|
||||
deadCount := 0 // 死亡人数,初始值为0
|
||||
|
||||
// 直到所有人都死亡,否则循环一直执行
|
||||
for deadCount < playerCount {
|
||||
// 跳到下一个人
|
||||
r.Next()
|
||||
// 直到所有人都死亡,否则循环一直执行
|
||||
for deadCount < playerCount {
|
||||
// 跳到下一个人
|
||||
r.Next()
|
||||
|
||||
// 如果是活着的人,则报数
|
||||
if r.Val().(*Player).alive {
|
||||
counter++
|
||||
}
|
||||
// 如果是活着的人,则报数
|
||||
if r.Val().(*Player).alive {
|
||||
counter++
|
||||
}
|
||||
|
||||
// 如果报数为deadline,则此人淘汰出局
|
||||
if counter == deadline {
|
||||
r.Val().(*Player).alive = false
|
||||
fmt.Printf("Player %d died!\n", r.Val().(*Player).position)
|
||||
deadCount++
|
||||
counter = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
// 如果报数为deadline,则此人淘汰出局
|
||||
if counter == deadline {
|
||||
r.Val().(*Player).alive = false
|
||||
fmt.Printf("Player %d died!\n", r.Val().(*Player).position)
|
||||
deadCount++
|
||||
counter = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,52 +1,52 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/container/gset"
|
||||
"fmt"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/container/gset"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 创建一个非并发安全的集合对象
|
||||
s := gset.New(false)
|
||||
// 创建一个非并发安全的集合对象
|
||||
s := gset.New(false)
|
||||
|
||||
// 添加数据项
|
||||
s.Add(1)
|
||||
// 添加数据项
|
||||
s.Add(1)
|
||||
|
||||
// 批量添加数据项
|
||||
s.BatchAdd([]interface{}{1, 2, 3})
|
||||
// 批量添加数据项
|
||||
s.BatchAdd([]interface{}{1, 2, 3})
|
||||
|
||||
// 集合数据项大小
|
||||
fmt.Println(s.Size())
|
||||
// 集合数据项大小
|
||||
fmt.Println(s.Size())
|
||||
|
||||
// 集合中是否存在指定数据项
|
||||
fmt.Println(s.Contains(2))
|
||||
// 集合中是否存在指定数据项
|
||||
fmt.Println(s.Contains(2))
|
||||
|
||||
// 返回数据项slice
|
||||
fmt.Println(s.Slice())
|
||||
// 返回数据项slice
|
||||
fmt.Println(s.Slice())
|
||||
|
||||
// 删除数据项
|
||||
s.Remove(3)
|
||||
// 删除数据项
|
||||
s.Remove(3)
|
||||
|
||||
// 遍历数据项
|
||||
s.Iterator(func(v interface{}) bool {
|
||||
fmt.Println("Iterator:", v)
|
||||
return true
|
||||
})
|
||||
// 遍历数据项
|
||||
s.Iterator(func(v interface{}) bool {
|
||||
fmt.Println("Iterator:", v)
|
||||
return true
|
||||
})
|
||||
|
||||
// 将集合转换为字符串
|
||||
fmt.Println(s.String())
|
||||
// 将集合转换为字符串
|
||||
fmt.Println(s.String())
|
||||
|
||||
// 并发安全写锁操作
|
||||
s.LockFunc(func(m map[interface{}]struct{}) {
|
||||
m[4] = struct{}{}
|
||||
})
|
||||
// 并发安全写锁操作
|
||||
s.LockFunc(func(m map[interface{}]struct{}) {
|
||||
m[4] = struct{}{}
|
||||
})
|
||||
|
||||
// 并发安全读锁操作
|
||||
s.RLockFunc(func(m map[interface{}]struct{}) {
|
||||
fmt.Println(m)
|
||||
})
|
||||
// 并发安全读锁操作
|
||||
s.RLockFunc(func(m map[interface{}]struct{}) {
|
||||
fmt.Println(m)
|
||||
})
|
||||
|
||||
// 清空集合
|
||||
s.Clear()
|
||||
fmt.Println(s.Size())
|
||||
// 清空集合
|
||||
s.Clear()
|
||||
fmt.Println(s.Size())
|
||||
}
|
||||
|
||||
@ -1,20 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/container/gtype"
|
||||
"fmt"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/container/gtype"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 创建一个Int型的并发安全基本类型对象
|
||||
i := gtype.NewInt()
|
||||
// 创建一个Int型的并发安全基本类型对象
|
||||
i := gtype.NewInt()
|
||||
|
||||
// 设置值
|
||||
i.Set(10)
|
||||
// 设置值
|
||||
i.Set(10)
|
||||
|
||||
// 获取值
|
||||
fmt.Println(i.Val())
|
||||
// 获取值
|
||||
fmt.Println(i.Val())
|
||||
|
||||
// (整型/浮点型有效)数值 增加/删除 delta
|
||||
fmt.Println(i.Add(-1))
|
||||
// (整型/浮点型有效)数值 增加/删除 delta
|
||||
fmt.Println(i.Add(-1))
|
||||
}
|
||||
|
||||
@ -1,36 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"fmt"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var v g.Var
|
||||
var v g.Var
|
||||
|
||||
v.Set("123")
|
||||
v.Set("123")
|
||||
|
||||
fmt.Println(v.Val())
|
||||
fmt.Println(v.Val())
|
||||
|
||||
// 基本类型转换
|
||||
fmt.Println(v.Int())
|
||||
fmt.Println(v.Uint())
|
||||
fmt.Println(v.Float64())
|
||||
// 基本类型转换
|
||||
fmt.Println(v.Int())
|
||||
fmt.Println(v.Uint())
|
||||
fmt.Println(v.Float64())
|
||||
|
||||
// slice转换
|
||||
fmt.Println(v.Ints())
|
||||
fmt.Println(v.Floats())
|
||||
fmt.Println(v.Strings())
|
||||
// slice转换
|
||||
fmt.Println(v.Ints())
|
||||
fmt.Println(v.Floats())
|
||||
fmt.Println(v.Strings())
|
||||
|
||||
// struct转换
|
||||
type Score struct {
|
||||
Value int
|
||||
}
|
||||
s := new(Score)
|
||||
v.Struct(s)
|
||||
fmt.Println(s)
|
||||
// struct转换
|
||||
type Score struct {
|
||||
Value int
|
||||
}
|
||||
s := new(Score)
|
||||
v.Struct(s)
|
||||
fmt.Println(s)
|
||||
|
||||
// 只读接口
|
||||
r := v.ReadOnly()
|
||||
fmt.Println(r.String())
|
||||
// 只读接口
|
||||
r := v.ReadOnly()
|
||||
fmt.Println(r.String())
|
||||
}
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"github.com/gogf/gf/g/database/gdb"
|
||||
"github.com/gogf/gf/g"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/database/gdb"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 本文件用于gf框架的mysql数据库操作示例,不作为单元测试使用
|
||||
@ -12,90 +12,88 @@ import (
|
||||
var db gdb.DB
|
||||
|
||||
// 初始化配置及创建数据库
|
||||
func init () {
|
||||
gdb.AddDefaultConfigNode(gdb.ConfigNode {
|
||||
Host : "127.0.0.1",
|
||||
Port : "3306",
|
||||
User : "root",
|
||||
Pass : "12345678",
|
||||
Name : "test",
|
||||
Type : "mysql",
|
||||
Role : "master",
|
||||
Charset : "utf8",
|
||||
})
|
||||
db, _ = gdb.New()
|
||||
func init() {
|
||||
gdb.AddDefaultConfigNode(gdb.ConfigNode{
|
||||
Host: "127.0.0.1",
|
||||
Port: "3306",
|
||||
User: "root",
|
||||
Pass: "12345678",
|
||||
Name: "test",
|
||||
Type: "mysql",
|
||||
Role: "master",
|
||||
Charset: "utf8",
|
||||
})
|
||||
db, _ = gdb.New()
|
||||
|
||||
//gins.Config().SetPath("/home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/geg/frame")
|
||||
//db = g.Database()
|
||||
//gins.Config().SetPath("/home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/geg/frame")
|
||||
//db = g.Database()
|
||||
|
||||
//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.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()
|
||||
//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)
|
||||
}
|
||||
fmt.Println("create:")
|
||||
_, err := db.Exec("CREATE DATABASE IF NOT EXISTS test")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
s := `
|
||||
s := `
|
||||
CREATE TABLE IF NOT EXISTS user (
|
||||
uid INT(10) UNSIGNED AUTO_INCREMENT,
|
||||
name VARCHAR(45),
|
||||
@ -104,12 +102,12 @@ func create() {
|
||||
ENGINE = InnoDB
|
||||
DEFAULT CHARACTER SET = utf8
|
||||
`
|
||||
_, err = db.Exec(s)
|
||||
if (err != nil) {
|
||||
fmt.Println(err)
|
||||
}
|
||||
_, err = db.Exec(s)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
s = `
|
||||
s = `
|
||||
CREATE TABLE IF NOT EXISTS user_detail (
|
||||
uid INT(10) UNSIGNED AUTO_INCREMENT,
|
||||
site VARCHAR(255),
|
||||
@ -119,419 +117,416 @@ func create() {
|
||||
DEFAULT CHARACTER SET = utf8
|
||||
`
|
||||
|
||||
_, err = db.Exec(s)
|
||||
if (err != nil) {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
_, 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()
|
||||
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()
|
||||
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()
|
||||
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()
|
||||
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_1"},
|
||||
{"name": "john_2"},
|
||||
{"name": "john_3"},
|
||||
{"name": "john_4"},
|
||||
}, 10)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
fmt.Println("batchInsert:")
|
||||
_, err := db.BatchInsert("user", gdb.List{
|
||||
{"name": "john_1"},
|
||||
{"name": "john_2"},
|
||||
{"name": "john_3"},
|
||||
{"name": "john_4"},
|
||||
}, 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()
|
||||
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", gdb.Map{"name" : "john6"}, "uid=?", 1)
|
||||
if err == nil {
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
fmt.Println("update2:")
|
||||
r, err := db.Update("user", gdb.Map{"name": "john6"}, "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()
|
||||
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").Where("u.uid > ?", 1).Limit(0, 2).Select()
|
||||
if err == nil {
|
||||
fmt.Println(r)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
fmt.Println("linkopSelect1:")
|
||||
r, err := db.Table("user u").LeftJoin("user_detail ud", "u.uid=ud.uid").Fields("u.*, ud.site").Where("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").Where("u.uid=?", 1).One()
|
||||
if err == nil {
|
||||
fmt.Println(r)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
fmt.Println("linkopSelect2:")
|
||||
r, err := db.Table("user u").LeftJoin("user_detail ud", "u.uid=ud.uid").Fields("u.*,ud.site").Where("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").Where("u.uid=?", 1).Value()
|
||||
if err == nil {
|
||||
fmt.Println(r.String())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
fmt.Println("linkopSelect3:")
|
||||
r, err := db.Table("user u").LeftJoin("user_detail ud", "u.uid=ud.uid").Fields("ud.site").Where("u.uid=?", 1).Value()
|
||||
if err == nil {
|
||||
fmt.Println(r.String())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 链式查询数量1
|
||||
func linkopCount1() {
|
||||
fmt.Println("linkopCount1:")
|
||||
r, err := db.Table("user u").Fields("uid").LeftJoin("user_detail ud", "u.uid=ud.uid").Where("u.uid=?", 1).Count()
|
||||
if err == nil {
|
||||
fmt.Println(r)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
fmt.Println("linkopCount1:")
|
||||
r, err := db.Table("user u").Fields("uid").LeftJoin("user_detail ud", "u.uid=ud.uid").Where("u.uid=?", 1).Count()
|
||||
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()
|
||||
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"}).Where("name=?", "john_1").Update()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
fmt.Println("linkopUpdate2:")
|
||||
r, err := db.Table("user").Data(gdb.Map{"name": "john2"}).Where("name=?", "john_1").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'").Where("name=?", "john2").Update()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
fmt.Println("linkopUpdate3:")
|
||||
r, err := db.Table("user").Data("name='john3'").Where("name=?", "john2").Update()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// Where条件使用Map
|
||||
func linkopUpdate4() {
|
||||
fmt.Println("linkopUpdate4:")
|
||||
r, err := db.Table("user").Data(gdb.Map{"name" : "john11111"}).Where(g.Map{"uid" : 1}).Update()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
fmt.Println("linkopUpdate4:")
|
||||
r, err := db.Table("user").Data(gdb.Map{"name": "john11111"}).Where(g.Map{"uid": 1}).Update()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 链式批量写入
|
||||
func linkopBatchInsert1() {
|
||||
fmt.Println("linkopBatchInsert1:")
|
||||
r, err := db.Table("user").Data(gdb.List{
|
||||
{"name": "john_1"},
|
||||
{"name": "john_2"},
|
||||
{"name": "john_3"},
|
||||
{"name": "john_4"},
|
||||
}).Insert()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
fmt.Println("linkopBatchInsert1:")
|
||||
r, err := db.Table("user").Data(gdb.List{
|
||||
{"name": "john_1"},
|
||||
{"name": "john_2"},
|
||||
{"name": "john_3"},
|
||||
{"name": "john_4"},
|
||||
}).Insert()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 链式批量写入,指定每批次写入的条数
|
||||
func linkopBatchInsert2() {
|
||||
fmt.Println("linkopBatchInsert2:")
|
||||
r, err := db.Table("user").Data(gdb.List{
|
||||
{"name": "john_1"},
|
||||
{"name": "john_2"},
|
||||
{"name": "john_3"},
|
||||
{"name": "john_4"},
|
||||
}).Batch(2).Insert()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
fmt.Println("linkopBatchInsert2:")
|
||||
r, err := db.Table("user").Data(gdb.List{
|
||||
{"name": "john_1"},
|
||||
{"name": "john_2"},
|
||||
{"name": "john_3"},
|
||||
{"name": "john_4"},
|
||||
}).Batch(2).Insert()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 链式批量保存
|
||||
func linkopBatchSave() {
|
||||
fmt.Println("linkopBatchSave:")
|
||||
r, err := db.Table("user").Data(gdb.List{
|
||||
{"uid":1, "name": "john_1"},
|
||||
{"uid":2, "name": "john_2"},
|
||||
{"uid":3, "name": "john_3"},
|
||||
{"uid":4, "name": "john_4"},
|
||||
}).Save()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
fmt.Println("linkopBatchSave:")
|
||||
r, err := db.Table("user").Data(gdb.List{
|
||||
{"uid": 1, "name": "john_1"},
|
||||
{"uid": 2, "name": "john_2"},
|
||||
{"uid": 3, "name": "john_3"},
|
||||
{"uid": 4, "name": "john_4"},
|
||||
}).Save()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 事务操作示例1
|
||||
func transaction1() {
|
||||
fmt.Println("transaction1:")
|
||||
if tx, err := db.Begin(); err == nil {
|
||||
r, err := tx.Save("user", gdb.Map{
|
||||
"uid" : 1,
|
||||
"name" : "john",
|
||||
})
|
||||
tx.Rollback()
|
||||
fmt.Println(r, err)
|
||||
}
|
||||
fmt.Println()
|
||||
fmt.Println("transaction1:")
|
||||
if tx, err := db.Begin(); err == nil {
|
||||
r, err := tx.Save("user", gdb.Map{
|
||||
"uid": 1,
|
||||
"name": "john",
|
||||
})
|
||||
tx.Rollback()
|
||||
fmt.Println(r, err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 事务操作示例2
|
||||
func transaction2() {
|
||||
fmt.Println("transaction2:")
|
||||
if tx, err := db.Begin(); err == nil {
|
||||
r, err := tx.Table("user").Data(gdb.Map{"uid":1, "name": "john_1"}).Save()
|
||||
tx.Commit()
|
||||
fmt.Println(r, err)
|
||||
}
|
||||
fmt.Println()
|
||||
fmt.Println("transaction2:")
|
||||
if tx, err := db.Begin(); err == nil {
|
||||
r, err := tx.Table("user").Data(gdb.Map{"uid": 1, "name": "john_1"}).Save()
|
||||
tx.Commit()
|
||||
fmt.Println(r, 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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
// like语句查询
|
||||
func likeQuery() {
|
||||
fmt.Println("likeQuery:")
|
||||
if r, err := db.Table("user").Where("name like ?", "%john%").Select(); err == nil {
|
||||
fmt.Println(r)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println("likeQuery:")
|
||||
if r, err := db.Table("user").Where("name like ?", "%john%").Select(); err == nil {
|
||||
fmt.Println(r)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// mapToStruct
|
||||
func mapToStruct() {
|
||||
type User struct {
|
||||
Uid int
|
||||
Name string
|
||||
}
|
||||
fmt.Println("mapToStruct:")
|
||||
if r, err := db.Table("user").Where("uid=?", 1).One(); err == nil {
|
||||
u := User{}
|
||||
if err := r.ToStruct(&u); err == nil {
|
||||
fmt.Println(r)
|
||||
fmt.Println(u)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
type User struct {
|
||||
Uid int
|
||||
Name string
|
||||
}
|
||||
fmt.Println("mapToStruct:")
|
||||
if r, err := db.Table("user").Where("uid=?", 1).One(); err == nil {
|
||||
u := User{}
|
||||
if err := r.ToStruct(&u); err == nil {
|
||||
fmt.Println(r)
|
||||
fmt.Println(u)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
fmt.Println("Func :", v.Func)
|
||||
}
|
||||
for k, v := range db.GetQueriedSqls() {
|
||||
fmt.Println(k, ":")
|
||||
fmt.Println("Sql :", v.Sql)
|
||||
fmt.Println("Args :", v.Args)
|
||||
fmt.Println("Error:", v.Error)
|
||||
fmt.Println("Func :", v.Func)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
//data := g.Map{
|
||||
// "nickname" : "john",
|
||||
//}
|
||||
//db.SetDebug(true)
|
||||
//r, err := db.Table("user").Where("id=1").Data(data).Update()
|
||||
//fmt.Println(err)
|
||||
//fmt.Println(r.RowsAffected())
|
||||
//data := g.Map{
|
||||
// "nickname" : "john",
|
||||
//}
|
||||
//db.SetDebug(true)
|
||||
//r, err := db.Table("user").Where("id=1").Data(data).Update()
|
||||
//fmt.Println(err)
|
||||
//fmt.Println(r.RowsAffected())
|
||||
|
||||
//data2 := g.Map{
|
||||
// "adsys1" : "ss",
|
||||
//}
|
||||
//db.SetDebug(true)
|
||||
//r, err := db.Table("cd_adsys").Where("adsys0=1").Data(data2).Update()
|
||||
//fmt.Println(err)
|
||||
//fmt.Println(r.RowsAffected())
|
||||
//return
|
||||
//db.SetDebug(true)
|
||||
//r, err := db.Table("test").Where("id=1").One()
|
||||
//fmt.Println(r["datetime"])
|
||||
//fmt.Println(r["datetime"].Time().Date())
|
||||
//fmt.Println(err)
|
||||
//create()
|
||||
//create()
|
||||
//insert()
|
||||
//query()
|
||||
//replace()
|
||||
//save()
|
||||
//batchInsert()
|
||||
//update1()
|
||||
//update2()
|
||||
//update3()
|
||||
linkopSelect1()
|
||||
//linkopSelect2()
|
||||
//linkopSelect3()
|
||||
//linkopCount1()
|
||||
//linkopUpdate1()
|
||||
//linkopUpdate2()
|
||||
//linkopUpdate3()
|
||||
//linkopUpdate4()
|
||||
//
|
||||
//transaction1()
|
||||
//transaction2()
|
||||
//
|
||||
//keepPing()
|
||||
//likeQuery()
|
||||
//mapToStruct()
|
||||
//getQueriedSqls()
|
||||
}
|
||||
//data2 := g.Map{
|
||||
// "adsys1" : "ss",
|
||||
//}
|
||||
//db.SetDebug(true)
|
||||
//r, err := db.Table("cd_adsys").Where("adsys0=1").Data(data2).Update()
|
||||
//fmt.Println(err)
|
||||
//fmt.Println(r.RowsAffected())
|
||||
//return
|
||||
//db.SetDebug(true)
|
||||
//r, err := db.Table("test").Where("id=1").One()
|
||||
//fmt.Println(r["datetime"])
|
||||
//fmt.Println(r["datetime"].Time().Date())
|
||||
//fmt.Println(err)
|
||||
//create()
|
||||
//create()
|
||||
//insert()
|
||||
//query()
|
||||
//replace()
|
||||
//save()
|
||||
//batchInsert()
|
||||
//update1()
|
||||
//update2()
|
||||
//update3()
|
||||
linkopSelect1()
|
||||
//linkopSelect2()
|
||||
//linkopSelect3()
|
||||
//linkopCount1()
|
||||
//linkopUpdate1()
|
||||
//linkopUpdate2()
|
||||
//linkopUpdate3()
|
||||
//linkopUpdate4()
|
||||
//
|
||||
//transaction1()
|
||||
//transaction2()
|
||||
//
|
||||
//keepPing()
|
||||
//likeQuery()
|
||||
//mapToStruct()
|
||||
//getQueriedSqls()
|
||||
}
|
||||
|
||||
@ -1,51 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/database/gdb"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/crypto/gaes"
|
||||
"github.com/gogf/gf/g"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/crypto/gaes"
|
||||
"github.com/gogf/gf/g/database/gdb"
|
||||
)
|
||||
|
||||
func main() {
|
||||
gdb.AddDefaultConfigNode(gdb.ConfigNode {
|
||||
Host : "127.0.0.1",
|
||||
Port : "3306",
|
||||
User : "root",
|
||||
Pass : "123456",
|
||||
Name : "test",
|
||||
Type : "mysql",
|
||||
Role : "master",
|
||||
Charset : "utf8",
|
||||
})
|
||||
db, err := gdb.New()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
gdb.AddDefaultConfigNode(gdb.ConfigNode{
|
||||
Host: "127.0.0.1",
|
||||
Port: "3306",
|
||||
User: "root",
|
||||
Pass: "123456",
|
||||
Name: "test",
|
||||
Type: "mysql",
|
||||
Role: "master",
|
||||
Charset: "utf8",
|
||||
})
|
||||
db, err := gdb.New()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
key := "0123456789123456"
|
||||
key := "0123456789123456"
|
||||
|
||||
name := "john"
|
||||
encryptedName, err := gaes.Encrypt([]byte(name), []byte(key))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
name := "john"
|
||||
encryptedName, err := gaes.Encrypt([]byte(name), []byte(key))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
// 写入
|
||||
r, err := db.Table("user").Data(g.Map{
|
||||
"uid": 1,
|
||||
"name": encryptedName,
|
||||
}).Save()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println(r.RowsAffected())
|
||||
|
||||
// 写入
|
||||
r, err := db.Table("user").Data(g.Map{
|
||||
"uid" : 1,
|
||||
"name" : encryptedName,
|
||||
}).Save()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println(r.RowsAffected())
|
||||
|
||||
// 查询
|
||||
one, err := db.Table("user").Where("name=?", encryptedName).One()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println(one.ToMap())
|
||||
}
|
||||
// 查询
|
||||
one, err := db.Table("user").Where("name=?", encryptedName).One()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println(one.ToMap())
|
||||
}
|
||||
|
||||
@ -1,38 +1,38 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/database/gdb"
|
||||
"github.com/gogf/gf/g/util/gutil"
|
||||
"github.com/gogf/gf/g/database/gdb"
|
||||
"github.com/gogf/gf/g/util/gutil"
|
||||
)
|
||||
|
||||
func main() {
|
||||
gdb.AddDefaultConfigNode(gdb.ConfigNode {
|
||||
Host : "127.0.0.1",
|
||||
Port : "3306",
|
||||
User : "root",
|
||||
Pass : "123456",
|
||||
Name : "test",
|
||||
Type : "mysql",
|
||||
Role : "master",
|
||||
Charset : "utf8",
|
||||
})
|
||||
db, err := gdb.New()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// 开启调试模式,以便于记录所有执行的SQL
|
||||
db.SetDebug(true)
|
||||
gdb.AddDefaultConfigNode(gdb.ConfigNode{
|
||||
Host: "127.0.0.1",
|
||||
Port: "3306",
|
||||
User: "root",
|
||||
Pass: "123456",
|
||||
Name: "test",
|
||||
Type: "mysql",
|
||||
Role: "master",
|
||||
Charset: "utf8",
|
||||
})
|
||||
db, err := gdb.New()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// 开启调试模式,以便于记录所有执行的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())
|
||||
}
|
||||
// 执行2次查询并将查询结果缓存3秒,并可执行缓存名称(可选)
|
||||
for i := 0; i < 2; i++ {
|
||||
r, _ := db.Table("user").Cache(3, "vip-user").Where("uid=?", 1).One()
|
||||
gutil.Dump(r.ToMap())
|
||||
}
|
||||
|
||||
// 执行更新操作,并清理指定名称的查询缓存
|
||||
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("uid=?", 1).Update()
|
||||
|
||||
// 再次执行查询,启用查询缓存特性
|
||||
r, _ := db.Table("user").Cache(3, "vip-user").Where("uid=?", 1).One()
|
||||
gutil.Dump(r.ToMap())
|
||||
}
|
||||
// 再次执行查询,启用查询缓存特性
|
||||
r, _ := db.Table("user").Cache(3, "vip-user").Where("uid=?", 1).One()
|
||||
gutil.Dump(r.ToMap())
|
||||
}
|
||||
|
||||
@ -1,16 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
g.Config().AddPath("/home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/geg/frame")
|
||||
if r, err := g.DB().Table("user").Where("uid=?", 1).One(); err == nil {
|
||||
fmt.Println(r["uid"].Int())
|
||||
fmt.Println(r["name"].String())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
g.Config().AddPath("/home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/geg/frame")
|
||||
if r, err := g.DB().Table("user").Where("uid=?", 1).One(); err == nil {
|
||||
fmt.Println(r["uid"].Int())
|
||||
fmt.Println(r["name"].String())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,29 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/os/gtime"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/os/gtime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.Database()
|
||||
db.SetDebug(true)
|
||||
db := g.Database()
|
||||
db.SetDebug(true)
|
||||
|
||||
//r, err := db.Table("user").Data("create_time", gtime.Now().String()).Insert()
|
||||
//if err == nil {
|
||||
// fmt.Println(r.LastInsertId())
|
||||
//} else {
|
||||
// panic(err)
|
||||
//}
|
||||
//r, err := db.Table("user").Data("create_time", gtime.Now().String()).Insert()
|
||||
//if err == nil {
|
||||
// fmt.Println(r.LastInsertId())
|
||||
//} else {
|
||||
// panic(err)
|
||||
//}
|
||||
|
||||
r, err := db.Table("user").Data(g.Map{
|
||||
"name" : "john",
|
||||
"create_time" : gtime.Now().String(),
|
||||
}).Insert()
|
||||
if err == nil {
|
||||
fmt.Println(r.LastInsertId())
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
r, err := db.Table("user").Data(g.Map{
|
||||
"name": "john",
|
||||
"create_time": gtime.Now().String(),
|
||||
}).Insert()
|
||||
if err == nil {
|
||||
fmt.Println(r.LastInsertId())
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,36 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/database/gdb"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/database/gdb"
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
)
|
||||
|
||||
func main() {
|
||||
gdb.AddDefaultConfigNode(gdb.ConfigNode{
|
||||
Host: "127.0.0.1",
|
||||
Port: "3306",
|
||||
User: "root",
|
||||
Pass: "12345678",
|
||||
Name: "test",
|
||||
Type: "mysql",
|
||||
Role: "master",
|
||||
Charset: "utf8",
|
||||
})
|
||||
db, err := gdb.New()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
glog.SetPath("/tmp")
|
||||
db.SetDebug(true)
|
||||
// 执行3条SQL查询
|
||||
for i := 1; i <= 3; i++ {
|
||||
db.Table("user").Where("uid=?", i).One()
|
||||
}
|
||||
// 构造一条错误查询
|
||||
db.Table("user").Where("no_such_field=?", "just_test").One()
|
||||
gdb.AddDefaultConfigNode(gdb.ConfigNode{
|
||||
Host: "127.0.0.1",
|
||||
Port: "3306",
|
||||
User: "root",
|
||||
Pass: "12345678",
|
||||
Name: "test",
|
||||
Type: "mysql",
|
||||
Role: "master",
|
||||
Charset: "utf8",
|
||||
})
|
||||
db, err := gdb.New()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
glog.SetPath("/tmp")
|
||||
db.SetDebug(true)
|
||||
// 执行3条SQL查询
|
||||
for i := 1; i <= 3; i++ {
|
||||
db.Table("user").Where("uid=?", i).One()
|
||||
}
|
||||
// 构造一条错误查询
|
||||
db.Table("user").Where("no_such_field=?", "just_test").One()
|
||||
|
||||
db.Table("user").Data(g.Map{"name":"smith"}).Where("uid=?", 1).Save()
|
||||
db.Table("user").Data(g.Map{"name": "smith"}).Where("uid=?", 1).Save()
|
||||
|
||||
//db.PrintQueriedSqls()
|
||||
}
|
||||
//db.PrintQueriedSqls()
|
||||
}
|
||||
|
||||
@ -1,36 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/database/gdb"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/encoding/gparser"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/database/gdb"
|
||||
"github.com/gogf/gf/g/encoding/gparser"
|
||||
)
|
||||
|
||||
func main() {
|
||||
gdb.AddDefaultConfigNode(gdb.ConfigNode {
|
||||
Host : "127.0.0.1",
|
||||
Port : "3306",
|
||||
User : "root",
|
||||
Pass : "12345678",
|
||||
Name : "test",
|
||||
Type : "mysql",
|
||||
Role : "master",
|
||||
Charset : "utf8",
|
||||
})
|
||||
db := g.DB()
|
||||
one, err := db.Table("user").Where("id=?", 1).One()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
gdb.AddDefaultConfigNode(gdb.ConfigNode{
|
||||
Host: "127.0.0.1",
|
||||
Port: "3306",
|
||||
User: "root",
|
||||
Pass: "12345678",
|
||||
Name: "test",
|
||||
Type: "mysql",
|
||||
Role: "master",
|
||||
Charset: "utf8",
|
||||
})
|
||||
db := g.DB()
|
||||
one, err := db.Table("user").Where("id=?", 1).One()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// 使用内置方法转换为json/xml
|
||||
fmt.Println(one.ToJson())
|
||||
fmt.Println(one.ToXml())
|
||||
// 使用内置方法转换为json/xml
|
||||
fmt.Println(one.ToJson())
|
||||
fmt.Println(one.ToXml())
|
||||
|
||||
// 自定义方法方法转换为json/xml
|
||||
jsonContent, _ := gparser.VarToJson(one.ToMap())
|
||||
fmt.Println(string(jsonContent))
|
||||
xmlContent, _ := gparser.VarToXml(one.ToMap())
|
||||
fmt.Println(string(xmlContent))
|
||||
}
|
||||
// 自定义方法方法转换为json/xml
|
||||
jsonContent, _ := gparser.VarToJson(one.ToMap())
|
||||
fmt.Println(string(jsonContent))
|
||||
xmlContent, _ := gparser.VarToXml(one.ToMap())
|
||||
fmt.Println(string(xmlContent))
|
||||
}
|
||||
|
||||
@ -1,24 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"time"
|
||||
"github.com/gogf/gf/g"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
db.SetMaxIdleConns(10)
|
||||
db.SetMaxOpenConns(10)
|
||||
db.SetConnMaxLifetime(10)
|
||||
db := g.DB()
|
||||
db.SetMaxIdleConns(10)
|
||||
db.SetMaxOpenConns(10)
|
||||
db.SetConnMaxLifetime(10)
|
||||
|
||||
// 开启调试模式,以便于记录所有执行的SQL
|
||||
db.SetDebug(true)
|
||||
// 开启调试模式,以便于记录所有执行的SQL
|
||||
db.SetDebug(true)
|
||||
|
||||
for {
|
||||
for i := 0; i < 10; i++ {
|
||||
go db.Table("user").All()
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
for {
|
||||
for i := 0; i < 10; i++ {
|
||||
go db.Table("user").All()
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,17 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
// 开启调试模式,以便于记录所有执行的SQL
|
||||
db.SetDebug(true)
|
||||
db := g.DB()
|
||||
// 开启调试模式,以便于记录所有执行的SQL
|
||||
db.SetDebug(true)
|
||||
|
||||
r, _ := db.Table("test").Where("id IN (?)", []interface{}{1, 2}).All()
|
||||
if r != nil {
|
||||
fmt.Println(r.ToList())
|
||||
}
|
||||
}
|
||||
r, _ := db.Table("test").Where("id IN (?)", []interface{}{1, 2}).All()
|
||||
if r != nil {
|
||||
fmt.Println(r.ToList())
|
||||
}
|
||||
}
|
||||
|
||||
@ -44,4 +44,4 @@ package main
|
||||
// if _, err := db.Exec(sql); err != nil {
|
||||
// fmt.Println(err)
|
||||
// }
|
||||
//}
|
||||
//}
|
||||
|
||||
@ -1,20 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
"github.com/gogf/gf/g/database/gredis"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/database/gredis"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
)
|
||||
|
||||
// 使用原生gredis.New操作redis,但是注意需要自己调用Close方法关闭redis链接池
|
||||
func main() {
|
||||
redis := gredis.New(gredis.Config{
|
||||
Host : "127.0.0.1",
|
||||
Port : 6379,
|
||||
})
|
||||
defer redis.Close()
|
||||
redis.Do("SET", "k", "v")
|
||||
v, _ := redis.Do("GET", "k")
|
||||
fmt.Println(gconv.String(v))
|
||||
redis := gredis.New(gredis.Config{
|
||||
Host: "127.0.0.1",
|
||||
Port: 6379,
|
||||
})
|
||||
defer redis.Close()
|
||||
redis.Do("SET", "k", "v")
|
||||
v, _ := redis.Do("GET", "k")
|
||||
fmt.Println(gconv.String(v))
|
||||
}
|
||||
|
||||
|
||||
@ -1,15 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
)
|
||||
|
||||
// 使用框架封装的g.Redis()方法获得redis操作对象单例,不需要开发者显示调用Close方法
|
||||
func main() {
|
||||
g.Redis().Do("SET", "k", "v")
|
||||
v, _ := g.Redis().Do("GET", "k")
|
||||
fmt.Println(gconv.String(v))
|
||||
g.Redis().Do("SET", "k", "v")
|
||||
v, _ := g.Redis().Do("GET", "k")
|
||||
fmt.Println(gconv.String(v))
|
||||
}
|
||||
|
||||
|
||||
@ -1,16 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
conn := g.Redis().Conn()
|
||||
defer conn.Close()
|
||||
conn.Do("SET", "k", "v")
|
||||
v, _ := conn.Do("GET", "k")
|
||||
fmt.Println(gconv.String(v))
|
||||
conn := g.Redis().Conn()
|
||||
defer conn.Close()
|
||||
conn.Do("SET", "k", "v")
|
||||
v, _ := conn.Do("GET", "k")
|
||||
fmt.Println(gconv.String(v))
|
||||
}
|
||||
|
||||
|
||||
@ -1,21 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
conn := g.Redis().Conn()
|
||||
defer conn.Close()
|
||||
conn.Send("SET", "foo", "bar")
|
||||
conn.Send("GET", "foo")
|
||||
conn.Flush()
|
||||
// reply from SET
|
||||
conn.Receive()
|
||||
// reply from GET
|
||||
v, _ := conn.Receive()
|
||||
fmt.Println(gconv.String(v))
|
||||
conn := g.Redis().Conn()
|
||||
defer conn.Close()
|
||||
conn.Send("SET", "foo", "bar")
|
||||
conn.Send("GET", "foo")
|
||||
conn.Flush()
|
||||
// reply from SET
|
||||
conn.Receive()
|
||||
// reply from GET
|
||||
v, _ := conn.Receive()
|
||||
fmt.Println(gconv.String(v))
|
||||
}
|
||||
|
||||
|
||||
@ -1,24 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
conn := g.Redis().Conn()
|
||||
defer conn.Close()
|
||||
_, err := conn.Do("SUBSCRIBE", "channel")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for {
|
||||
reply, err := conn.Receive()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(gconv.Strings(reply))
|
||||
}
|
||||
conn := g.Redis().Conn()
|
||||
defer conn.Close()
|
||||
_, err := conn.Do("SUBSCRIBE", "channel")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for {
|
||||
reply, err := conn.Receive()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(gconv.Strings(reply))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,18 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/encoding/gbase64"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/encoding/gbase64"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := "john"
|
||||
b := gbase64.Encode(s)
|
||||
c, e := gbase64.Decode(b)
|
||||
fmt.Println(b)
|
||||
fmt.Println(c)
|
||||
fmt.Println(e)
|
||||
s := "john"
|
||||
b := gbase64.Encode(s)
|
||||
c, e := gbase64.Decode(b)
|
||||
fmt.Println(b)
|
||||
fmt.Println(c)
|
||||
fmt.Println(e)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,61 +1,61 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
"github.com/gogf/gf/g/encoding/gbinary"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/encoding/gbinary"
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 使用gbinary.Encoded对基本数据类型进行二进制打包
|
||||
if buffer, err := gbinary.Encode(18, 300, 1.01); err != nil {
|
||||
glog.Error(err)
|
||||
} else {
|
||||
fmt.Println(buffer)
|
||||
}
|
||||
// 使用gbinary.Encoded对基本数据类型进行二进制打包
|
||||
if buffer, err := gbinary.Encode(18, 300, 1.01); err != nil {
|
||||
glog.Error(err)
|
||||
} else {
|
||||
fmt.Println(buffer)
|
||||
}
|
||||
|
||||
// 使用gbinary.Decode对整形二进制解包,注意第二个及其后参数为字长确定的整形变量的指针地址,字长确定的类型,
|
||||
// 例如:int8/16/32/64、uint8/16/32/64、float32/64
|
||||
// 这里的1.01默认为float64类型(64位系统下)
|
||||
if buffer, err := gbinary.Encode(18, 300, 1.01); err != nil {
|
||||
glog.Error(err)
|
||||
} else {
|
||||
var i1 int8
|
||||
var i2 int16
|
||||
var f3 float64
|
||||
if err := gbinary.Decode(buffer, &i1, &i2, &f3); err != nil {
|
||||
glog.Error(err)
|
||||
} else {
|
||||
fmt.Println(i1, i2, f3)
|
||||
}
|
||||
}
|
||||
// 使用gbinary.Decode对整形二进制解包,注意第二个及其后参数为字长确定的整形变量的指针地址,字长确定的类型,
|
||||
// 例如:int8/16/32/64、uint8/16/32/64、float32/64
|
||||
// 这里的1.01默认为float64类型(64位系统下)
|
||||
if buffer, err := gbinary.Encode(18, 300, 1.01); err != nil {
|
||||
glog.Error(err)
|
||||
} else {
|
||||
var i1 int8
|
||||
var i2 int16
|
||||
var f3 float64
|
||||
if err := gbinary.Decode(buffer, &i1, &i2, &f3); err != nil {
|
||||
glog.Error(err)
|
||||
} else {
|
||||
fmt.Println(i1, i2, f3)
|
||||
}
|
||||
}
|
||||
|
||||
// 编码/解析 int,自动识别变量长度
|
||||
fmt.Println(gbinary.DecodeToInt(gbinary.EncodeInt(1)))
|
||||
fmt.Println(gbinary.DecodeToInt(gbinary.EncodeInt(300)))
|
||||
fmt.Println(gbinary.DecodeToInt(gbinary.EncodeInt(70000)))
|
||||
fmt.Println(gbinary.DecodeToInt(gbinary.EncodeInt(2000000000)))
|
||||
fmt.Println(gbinary.DecodeToInt(gbinary.EncodeInt(500000000000)))
|
||||
// 编码/解析 int,自动识别变量长度
|
||||
fmt.Println(gbinary.DecodeToInt(gbinary.EncodeInt(1)))
|
||||
fmt.Println(gbinary.DecodeToInt(gbinary.EncodeInt(300)))
|
||||
fmt.Println(gbinary.DecodeToInt(gbinary.EncodeInt(70000)))
|
||||
fmt.Println(gbinary.DecodeToInt(gbinary.EncodeInt(2000000000)))
|
||||
fmt.Println(gbinary.DecodeToInt(gbinary.EncodeInt(500000000000)))
|
||||
|
||||
// 编码/解析 uint,自动识别变量长度
|
||||
fmt.Println(gbinary.DecodeToUint(gbinary.EncodeUint(1)))
|
||||
fmt.Println(gbinary.DecodeToUint(gbinary.EncodeUint(300)))
|
||||
fmt.Println(gbinary.DecodeToUint(gbinary.EncodeUint(70000)))
|
||||
fmt.Println(gbinary.DecodeToUint(gbinary.EncodeUint(2000000000)))
|
||||
fmt.Println(gbinary.DecodeToUint(gbinary.EncodeUint(500000000000)))
|
||||
// 编码/解析 uint,自动识别变量长度
|
||||
fmt.Println(gbinary.DecodeToUint(gbinary.EncodeUint(1)))
|
||||
fmt.Println(gbinary.DecodeToUint(gbinary.EncodeUint(300)))
|
||||
fmt.Println(gbinary.DecodeToUint(gbinary.EncodeUint(70000)))
|
||||
fmt.Println(gbinary.DecodeToUint(gbinary.EncodeUint(2000000000)))
|
||||
fmt.Println(gbinary.DecodeToUint(gbinary.EncodeUint(500000000000)))
|
||||
|
||||
// 编码/解析 int8/16/32/64
|
||||
fmt.Println(gbinary.DecodeToInt8(gbinary.EncodeInt8(int8(100))))
|
||||
fmt.Println(gbinary.DecodeToInt16(gbinary.EncodeInt16(int16(100))))
|
||||
fmt.Println(gbinary.DecodeToInt32(gbinary.EncodeInt32(int32(100))))
|
||||
fmt.Println(gbinary.DecodeToInt64(gbinary.EncodeInt64(int64(100))))
|
||||
// 编码/解析 int8/16/32/64
|
||||
fmt.Println(gbinary.DecodeToInt8(gbinary.EncodeInt8(int8(100))))
|
||||
fmt.Println(gbinary.DecodeToInt16(gbinary.EncodeInt16(int16(100))))
|
||||
fmt.Println(gbinary.DecodeToInt32(gbinary.EncodeInt32(int32(100))))
|
||||
fmt.Println(gbinary.DecodeToInt64(gbinary.EncodeInt64(int64(100))))
|
||||
|
||||
// 编码/解析 uint8/16/32/64
|
||||
fmt.Println(gbinary.DecodeToUint8(gbinary.EncodeUint8(uint8(100))))
|
||||
fmt.Println(gbinary.DecodeToUint16(gbinary.EncodeUint16(uint16(100))))
|
||||
fmt.Println(gbinary.DecodeToUint32(gbinary.EncodeUint32(uint32(100))))
|
||||
fmt.Println(gbinary.DecodeToUint64(gbinary.EncodeUint64(uint64(100))))
|
||||
// 编码/解析 uint8/16/32/64
|
||||
fmt.Println(gbinary.DecodeToUint8(gbinary.EncodeUint8(uint8(100))))
|
||||
fmt.Println(gbinary.DecodeToUint16(gbinary.EncodeUint16(uint16(100))))
|
||||
fmt.Println(gbinary.DecodeToUint32(gbinary.EncodeUint32(uint32(100))))
|
||||
fmt.Println(gbinary.DecodeToUint64(gbinary.EncodeUint64(uint64(100))))
|
||||
|
||||
// 编码/解析 string
|
||||
fmt.Println(gbinary.DecodeToString(gbinary.EncodeString("I'm string!")))
|
||||
// 编码/解析 string
|
||||
fmt.Println(gbinary.DecodeToString(gbinary.EncodeString("I'm string!")))
|
||||
}
|
||||
|
||||
@ -1,32 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/encoding/gbinary"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/encoding/gbinary"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 传感器状态,0:已下线, 1:开启, 2:关闭, 3:待机
|
||||
count := 100
|
||||
status := 1
|
||||
// 传感器状态,0:已下线, 1:开启, 2:关闭, 3:待机
|
||||
count := 100
|
||||
status := 1
|
||||
|
||||
// 网关编码
|
||||
bits := make([]gbinary.Bit, 0)
|
||||
for i := 0; i < count; i++ {
|
||||
bits = gbinary.EncodeBits(bits, status, 2)
|
||||
}
|
||||
buffer := gbinary.EncodeBitsToBytes(bits)
|
||||
fmt.Println("buffer length:", len(buffer))
|
||||
// 网关编码
|
||||
bits := make([]gbinary.Bit, 0)
|
||||
for i := 0; i < count; i++ {
|
||||
bits = gbinary.EncodeBits(bits, status, 2)
|
||||
}
|
||||
buffer := gbinary.EncodeBitsToBytes(bits)
|
||||
fmt.Println("buffer length:", len(buffer))
|
||||
|
||||
/* 上报过程忽略,这里只展示编码/解码示例 */
|
||||
/* 上报过程忽略,这里只展示编码/解码示例 */
|
||||
|
||||
// 平台解码
|
||||
alivecount := 0
|
||||
sensorbits := gbinary.DecodeBytesToBits(buffer)
|
||||
for i := 0; i < len(sensorbits); i += 2 {
|
||||
if gbinary.DecodeBits(sensorbits[i:i+2]) == 1 {
|
||||
alivecount++
|
||||
}
|
||||
}
|
||||
fmt.Println("alived sensor:", alivecount)
|
||||
// 平台解码
|
||||
alivecount := 0
|
||||
sensorbits := gbinary.DecodeBytesToBits(buffer)
|
||||
for i := 0; i < len(sensorbits); i += 2 {
|
||||
if gbinary.DecodeBits(sensorbits[i:i+2]) == 1 {
|
||||
alivecount++
|
||||
}
|
||||
}
|
||||
fmt.Println("alived sensor:", alivecount)
|
||||
}
|
||||
|
||||
@ -1,32 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/encoding/gbinary"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/encoding/gbinary"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Meta元数据文件数据结构:[键名哈希64(64bit,8byte) 键名长度(8bit,1byte) 键值长度(24bit,3byte) 数据文件偏移量(40bit,5byte)](变长)
|
||||
hash := 521369841259754125
|
||||
klen := 12
|
||||
vlen := 35535
|
||||
offset := 80000000
|
||||
// Meta元数据文件数据结构:[键名哈希64(64bit,8byte) 键名长度(8bit,1byte) 键值长度(24bit,3byte) 数据文件偏移量(40bit,5byte)](变长)
|
||||
hash := 521369841259754125
|
||||
klen := 12
|
||||
vlen := 35535
|
||||
offset := 80000000
|
||||
|
||||
// 编码
|
||||
bits := make([]gbinary.Bit, 0)
|
||||
bits = gbinary.EncodeBits(bits, hash, 64)
|
||||
bits = gbinary.EncodeBits(bits, klen, 8)
|
||||
bits = gbinary.EncodeBits(bits, vlen, 24)
|
||||
bits = gbinary.EncodeBits(bits, offset, 40)
|
||||
buffer := gbinary.EncodeBitsToBytes(bits)
|
||||
fmt.Println("meta length:", len(buffer))
|
||||
// 编码
|
||||
bits := make([]gbinary.Bit, 0)
|
||||
bits = gbinary.EncodeBits(bits, hash, 64)
|
||||
bits = gbinary.EncodeBits(bits, klen, 8)
|
||||
bits = gbinary.EncodeBits(bits, vlen, 24)
|
||||
bits = gbinary.EncodeBits(bits, offset, 40)
|
||||
buffer := gbinary.EncodeBitsToBytes(bits)
|
||||
fmt.Println("meta length:", len(buffer))
|
||||
|
||||
/* 文件存储及数据查询过程忽略,这里只展示元数据编码/解码示例 */
|
||||
/* 文件存储及数据查询过程忽略,这里只展示元数据编码/解码示例 */
|
||||
|
||||
// 解码
|
||||
metabits := gbinary.DecodeBytesToBits(buffer)
|
||||
fmt.Println("hash :", gbinary.DecodeBits(metabits[0 : 64]))
|
||||
fmt.Println("klen :", gbinary.DecodeBits(metabits[64 : 72]))
|
||||
fmt.Println("vlen :", gbinary.DecodeBits(metabits[72 : 96]))
|
||||
fmt.Println("offset:", gbinary.DecodeBits(metabits[96 : 136]))
|
||||
// 解码
|
||||
metabits := gbinary.DecodeBytesToBits(buffer)
|
||||
fmt.Println("hash :", gbinary.DecodeBits(metabits[0:64]))
|
||||
fmt.Println("klen :", gbinary.DecodeBits(metabits[64:72]))
|
||||
fmt.Println("vlen :", gbinary.DecodeBits(metabits[72:96]))
|
||||
fmt.Println("offset:", gbinary.DecodeBits(metabits[96:136]))
|
||||
}
|
||||
|
||||
@ -1,22 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(g.Config().Get("redis"))
|
||||
fmt.Println(g.Config().Get("redis"))
|
||||
|
||||
type RedisConfig struct {
|
||||
Disk string
|
||||
Cache string
|
||||
}
|
||||
type RedisConfig struct {
|
||||
Disk string
|
||||
Cache string
|
||||
}
|
||||
|
||||
redisCfg := new(RedisConfig)
|
||||
fmt.Println(g.Config().GetToStruct("redis", redisCfg))
|
||||
fmt.Println(redisCfg)
|
||||
redisCfg := new(RedisConfig)
|
||||
fmt.Println(g.Config().GetToStruct("redis", redisCfg))
|
||||
fmt.Println(redisCfg)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,20 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/encoding/ghash"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/encoding/ghash"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,15 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
"github.com/gogf/gf/g/encoding/gjson"
|
||||
"github.com/gogf/gf/g/os/gtime"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/encoding/gjson"
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
"github.com/gogf/gf/g/os/gtime"
|
||||
)
|
||||
|
||||
func getByPattern() {
|
||||
data :=
|
||||
`{
|
||||
data :=
|
||||
`{
|
||||
"users" : {
|
||||
"count" : 100,
|
||||
"list" : [
|
||||
@ -18,143 +18,137 @@ func getByPattern() {
|
||||
]
|
||||
}
|
||||
}`
|
||||
j, err := gjson.DecodeToJson([]byte(data))
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
} else {
|
||||
fmt.Println("John Score:", j.GetFloat32("users.list.1.score"))
|
||||
}
|
||||
j, err := gjson.DecodeToJson([]byte(data))
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
} else {
|
||||
fmt.Println("John Score:", j.GetFloat32("users.list.1.score"))
|
||||
}
|
||||
}
|
||||
|
||||
// 当键名存在"."号时,检索优先级:键名->层级,因此不会引起歧义
|
||||
func testMultiDots() {
|
||||
data :=
|
||||
`{
|
||||
data :=
|
||||
`{
|
||||
"users" : {
|
||||
"count" : 100
|
||||
},
|
||||
"users.count" : 101
|
||||
}`
|
||||
j, err := gjson.DecodeToJson([]byte(data))
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
} else {
|
||||
fmt.Println("Users Count:", j.GetInt("users.count"))
|
||||
}
|
||||
j, err := gjson.DecodeToJson([]byte(data))
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
} else {
|
||||
fmt.Println("Users Count:", j.GetInt("users.count"))
|
||||
}
|
||||
}
|
||||
|
||||
// 设置数据
|
||||
func testSet() {
|
||||
data :=
|
||||
`{
|
||||
data :=
|
||||
`{
|
||||
"users" : {
|
||||
"count" : 100
|
||||
}
|
||||
}`
|
||||
j, err := gjson.DecodeToJson([]byte(data))
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
} else {
|
||||
j.Set("users.count", 1)
|
||||
j.Set("users.list", []string{"John", "小明"})
|
||||
c, _ := j.ToJson()
|
||||
fmt.Println(string(c))
|
||||
}
|
||||
j, err := gjson.DecodeToJson([]byte(data))
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
} else {
|
||||
j.Set("users.count", 1)
|
||||
j.Set("users.list", []string{"John", "小明"})
|
||||
c, _ := j.ToJson()
|
||||
fmt.Println(string(c))
|
||||
}
|
||||
}
|
||||
|
||||
// 将Json数据转换为其他数据格式
|
||||
func testConvert() {
|
||||
data :=
|
||||
`{
|
||||
data :=
|
||||
`{
|
||||
"users" : {
|
||||
"count" : 100,
|
||||
"list" : ["John", "小明"]
|
||||
}
|
||||
}`
|
||||
j, err := gjson.DecodeToJson([]byte(data))
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
} else {
|
||||
c, _ := j.ToJson()
|
||||
fmt.Println("JSON:")
|
||||
fmt.Println(string(c))
|
||||
fmt.Println("======================")
|
||||
j, err := gjson.DecodeToJson([]byte(data))
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
} else {
|
||||
c, _ := j.ToJson()
|
||||
fmt.Println("JSON:")
|
||||
fmt.Println(string(c))
|
||||
fmt.Println("======================")
|
||||
|
||||
fmt.Println("XML:")
|
||||
c, _ = j.ToXmlIndent()
|
||||
fmt.Println(string(c))
|
||||
fmt.Println("======================")
|
||||
fmt.Println("XML:")
|
||||
c, _ = j.ToXmlIndent()
|
||||
fmt.Println(string(c))
|
||||
fmt.Println("======================")
|
||||
|
||||
fmt.Println("YAML:")
|
||||
c, _ = j.ToYaml()
|
||||
fmt.Println(string(c))
|
||||
fmt.Println("======================")
|
||||
fmt.Println("YAML:")
|
||||
c, _ = j.ToYaml()
|
||||
fmt.Println(string(c))
|
||||
fmt.Println("======================")
|
||||
|
||||
fmt.Println("TOML:")
|
||||
c, _ = j.ToToml()
|
||||
fmt.Println(string(c))
|
||||
}
|
||||
fmt.Println("TOML:")
|
||||
c, _ = j.ToToml()
|
||||
fmt.Println(string(c))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func testSplitChar() {
|
||||
var v interface{}
|
||||
j := gjson.New(nil)
|
||||
t1 := gtime.Nanosecond()
|
||||
j.Set("a.b.c.d.e.f.g.h.i.j.k", 1)
|
||||
t2 := gtime.Nanosecond()
|
||||
fmt.Println(t2 - t1)
|
||||
var v interface{}
|
||||
j := gjson.New(nil)
|
||||
t1 := gtime.Nanosecond()
|
||||
j.Set("a.b.c.d.e.f.g.h.i.j.k", 1)
|
||||
t2 := gtime.Nanosecond()
|
||||
fmt.Println(t2 - t1)
|
||||
|
||||
t5 := gtime.Nanosecond()
|
||||
v = j.Get("a.b.c.d.e.f.g.h.i.j.k")
|
||||
t6 := gtime.Nanosecond()
|
||||
fmt.Println(v)
|
||||
fmt.Println(t6 - t5)
|
||||
t5 := gtime.Nanosecond()
|
||||
v = j.Get("a.b.c.d.e.f.g.h.i.j.k")
|
||||
t6 := gtime.Nanosecond()
|
||||
fmt.Println(v)
|
||||
fmt.Println(t6 - t5)
|
||||
|
||||
j.SetSplitChar('#')
|
||||
j.SetSplitChar('#')
|
||||
|
||||
|
||||
|
||||
t7 := gtime.Nanosecond()
|
||||
v = j.Get("a#b#c#d#e#f#g#h#i#j#k")
|
||||
t8 := gtime.Nanosecond()
|
||||
fmt.Println(v)
|
||||
fmt.Println(t8 - t7)
|
||||
t7 := gtime.Nanosecond()
|
||||
v = j.Get("a#b#c#d#e#f#g#h#i#j#k")
|
||||
t8 := gtime.Nanosecond()
|
||||
fmt.Println(v)
|
||||
fmt.Println(t8 - t7)
|
||||
}
|
||||
|
||||
|
||||
func testViolenceCheck() {
|
||||
j := gjson.New(nil)
|
||||
t1 := gtime.Nanosecond()
|
||||
j.Set("a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a", 1)
|
||||
t2 := gtime.Nanosecond()
|
||||
fmt.Println(t2 - t1)
|
||||
j := gjson.New(nil)
|
||||
t1 := gtime.Nanosecond()
|
||||
j.Set("a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a", 1)
|
||||
t2 := gtime.Nanosecond()
|
||||
fmt.Println(t2 - t1)
|
||||
|
||||
t3 := gtime.Nanosecond()
|
||||
j.Set("a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a", 1)
|
||||
t4 := gtime.Nanosecond()
|
||||
fmt.Println(t4 - t3)
|
||||
t3 := gtime.Nanosecond()
|
||||
j.Set("a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a", 1)
|
||||
t4 := gtime.Nanosecond()
|
||||
fmt.Println(t4 - t3)
|
||||
|
||||
t5 := gtime.Nanosecond()
|
||||
j.Get("a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a")
|
||||
t6 := gtime.Nanosecond()
|
||||
fmt.Println(t6 - t5)
|
||||
t5 := gtime.Nanosecond()
|
||||
j.Get("a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a")
|
||||
t6 := gtime.Nanosecond()
|
||||
fmt.Println(t6 - t5)
|
||||
|
||||
j.SetViolenceCheck(false)
|
||||
|
||||
j.SetViolenceCheck(false)
|
||||
t7 := gtime.Nanosecond()
|
||||
j.Set("a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a", 1)
|
||||
t8 := gtime.Nanosecond()
|
||||
fmt.Println(t8 - t7)
|
||||
|
||||
|
||||
t7 := gtime.Nanosecond()
|
||||
j.Set("a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a", 1)
|
||||
t8 := gtime.Nanosecond()
|
||||
fmt.Println(t8 - t7)
|
||||
|
||||
t9 := gtime.Nanosecond()
|
||||
j.Get("a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a")
|
||||
t10 := gtime.Nanosecond()
|
||||
fmt.Println(t10 - t9)
|
||||
t9 := gtime.Nanosecond()
|
||||
j.Get("a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a")
|
||||
t10 := gtime.Nanosecond()
|
||||
fmt.Println(t10 - t9)
|
||||
}
|
||||
|
||||
func main() {
|
||||
testViolenceCheck()
|
||||
}
|
||||
testViolenceCheck()
|
||||
}
|
||||
|
||||
@ -1,14 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
"github.com/gogf/gf/g/encoding/gparser"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/encoding/gparser"
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
)
|
||||
|
||||
func getWithPattern1() {
|
||||
data :=
|
||||
`{
|
||||
data :=
|
||||
`{
|
||||
"users" : {
|
||||
"count" : 100,
|
||||
"list" : [
|
||||
@ -18,16 +18,16 @@ func getWithPattern1() {
|
||||
}
|
||||
}`
|
||||
|
||||
if p, e := gparser.LoadContent([]byte(data), "json"); e != nil {
|
||||
glog.Error(e)
|
||||
} else {
|
||||
fmt.Println("John Score:", p.GetFloat32("users.list.1.score"))
|
||||
}
|
||||
if p, e := gparser.LoadContent([]byte(data), "json"); e != nil {
|
||||
glog.Error(e)
|
||||
} else {
|
||||
fmt.Println("John Score:", p.GetFloat32("users.list.1.score"))
|
||||
}
|
||||
}
|
||||
|
||||
func getWithPattern2() {
|
||||
data :=
|
||||
`<?xml version="1.0" encoding="UTF-8"?>
|
||||
data :=
|
||||
`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<note>
|
||||
<to>Tove</to>
|
||||
<from>Jani</from>
|
||||
@ -35,32 +35,32 @@ func getWithPattern2() {
|
||||
<body>Don't forget me this weekend!</body>
|
||||
</note>`
|
||||
|
||||
if p, e := gparser.LoadContent([]byte(data), "xml"); e != nil {
|
||||
glog.Error(e)
|
||||
} else {
|
||||
fmt.Println("Heading:", p.GetString("note.heading"))
|
||||
}
|
||||
if p, e := gparser.LoadContent([]byte(data), "xml"); e != nil {
|
||||
glog.Error(e)
|
||||
} else {
|
||||
fmt.Println("Heading:", p.GetString("note.heading"))
|
||||
}
|
||||
}
|
||||
|
||||
// 当键名存在"."号时,检索优先级:键名->层级,因此不会引起歧义
|
||||
func multiDots1() {
|
||||
data :=
|
||||
`{
|
||||
data :=
|
||||
`{
|
||||
"users" : {
|
||||
"count" : 100
|
||||
},
|
||||
"users.count" : 101
|
||||
}`
|
||||
if p, e := gparser.LoadContent([]byte(data), "json"); e != nil {
|
||||
glog.Error(e)
|
||||
} else {
|
||||
fmt.Println("Users Count:", p.Get("users.count"))
|
||||
}
|
||||
if p, e := gparser.LoadContent([]byte(data), "json"); e != nil {
|
||||
glog.Error(e)
|
||||
} else {
|
||||
fmt.Println("Users Count:", p.Get("users.count"))
|
||||
}
|
||||
}
|
||||
|
||||
func multiDots2() {
|
||||
data :=
|
||||
`{
|
||||
data :=
|
||||
`{
|
||||
"users" : {
|
||||
"count" : {
|
||||
"type1" : 1,
|
||||
@ -69,150 +69,148 @@ func multiDots2() {
|
||||
"count.type1" : 100
|
||||
}
|
||||
}`
|
||||
if p, e := gparser.LoadContent([]byte(data), "json"); e != nil {
|
||||
glog.Error(e)
|
||||
} else {
|
||||
fmt.Println("Users Count:", p.Get("users.count.type1"))
|
||||
fmt.Println("Users Count:", p.Get("users.count.type2"))
|
||||
}
|
||||
if p, e := gparser.LoadContent([]byte(data), "json"); e != nil {
|
||||
glog.Error(e)
|
||||
} else {
|
||||
fmt.Println("Users Count:", p.Get("users.count.type1"))
|
||||
fmt.Println("Users Count:", p.Get("users.count.type2"))
|
||||
}
|
||||
}
|
||||
|
||||
// 设置数据
|
||||
func set1() {
|
||||
data :=
|
||||
`<?xml version="1.0" encoding="UTF-8"?>
|
||||
data :=
|
||||
`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<article>
|
||||
<count>10</count>
|
||||
<list><title>gf article1</title><content>gf content1</content></list>
|
||||
<list><title>gf article2</title><content>gf content2</content></list>
|
||||
<list><title>gf article3</title><content>gf content3</content></list>
|
||||
</article>`
|
||||
if p, e := gparser.LoadContent([]byte(data), "xml"); e != nil {
|
||||
glog.Error(e)
|
||||
} else {
|
||||
p.Set("article.list.0", nil)
|
||||
c, _ := p.ToJson()
|
||||
fmt.Println(string(c))
|
||||
// {"article":{"count":"10","list":[{"content":"gf content2","title":"gf article2"},{"content":"gf content3","title":"gf article3"}]}}
|
||||
}
|
||||
if p, e := gparser.LoadContent([]byte(data), "xml"); e != nil {
|
||||
glog.Error(e)
|
||||
} else {
|
||||
p.Set("article.list.0", nil)
|
||||
c, _ := p.ToJson()
|
||||
fmt.Println(string(c))
|
||||
// {"article":{"count":"10","list":[{"content":"gf content2","title":"gf article2"},{"content":"gf content3","title":"gf article3"}]}}
|
||||
}
|
||||
}
|
||||
|
||||
func set2() {
|
||||
data :=
|
||||
`{
|
||||
data :=
|
||||
`{
|
||||
"users" : {
|
||||
"count" : 100
|
||||
}
|
||||
}`
|
||||
if p, e := gparser.LoadContent([]byte(data), "json"); e != nil {
|
||||
glog.Error(e)
|
||||
} else {
|
||||
p.Set("users.count", 1)
|
||||
p.Set("users.list", []string{"John", "小明"})
|
||||
c, _ := p.ToJson()
|
||||
fmt.Println(string(c))
|
||||
}
|
||||
if p, e := gparser.LoadContent([]byte(data), "json"); e != nil {
|
||||
glog.Error(e)
|
||||
} else {
|
||||
p.Set("users.count", 1)
|
||||
p.Set("users.list", []string{"John", "小明"})
|
||||
c, _ := p.ToJson()
|
||||
fmt.Println(string(c))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func makeXml1() {
|
||||
p := gparser.New()
|
||||
p.Set("name", "john")
|
||||
p.Set("age", 18)
|
||||
p.Set("scores", map[string]int{
|
||||
"语文" : 100,
|
||||
"数学" : 100,
|
||||
"英语" : 100,
|
||||
})
|
||||
c, _ := p.ToXmlIndent("simple-xml")
|
||||
fmt.Println(string(c))
|
||||
p := gparser.New()
|
||||
p.Set("name", "john")
|
||||
p.Set("age", 18)
|
||||
p.Set("scores", map[string]int{
|
||||
"语文": 100,
|
||||
"数学": 100,
|
||||
"英语": 100,
|
||||
})
|
||||
c, _ := p.ToXmlIndent("simple-xml")
|
||||
fmt.Println(string(c))
|
||||
}
|
||||
|
||||
func makeJson1() {
|
||||
type Order struct {
|
||||
Id int `json:"id"`
|
||||
Price float32 `json:"price"`
|
||||
}
|
||||
p := gparser.New()
|
||||
p.Set("orders.list.0", Order{1, 100})
|
||||
p.Set("orders.list.1", Order{2, 666})
|
||||
p.Set("orders.list.2", Order{3, 999.99})
|
||||
fmt.Println("Order 1 Price:", p.Get("orders.list.1.price"))
|
||||
c, _ := p.ToJson()
|
||||
fmt.Println(string(c))
|
||||
type Order struct {
|
||||
Id int `json:"id"`
|
||||
Price float32 `json:"price"`
|
||||
}
|
||||
p := gparser.New()
|
||||
p.Set("orders.list.0", Order{1, 100})
|
||||
p.Set("orders.list.1", Order{2, 666})
|
||||
p.Set("orders.list.2", Order{3, 999.99})
|
||||
fmt.Println("Order 1 Price:", p.Get("orders.list.1.price"))
|
||||
c, _ := p.ToJson()
|
||||
fmt.Println(string(c))
|
||||
}
|
||||
|
||||
func makeJson2() {
|
||||
p := gparser.New(map[string]string{
|
||||
"k1" : "v1",
|
||||
"k2" : "v2",
|
||||
})
|
||||
p.Set("k1.1", []int{1,2,3})
|
||||
//p.Set("0.0.1", []int{1,2,3})
|
||||
c, _ := p.ToJson()
|
||||
fmt.Println(string(c))
|
||||
p := gparser.New(map[string]string{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
})
|
||||
p.Set("k1.1", []int{1, 2, 3})
|
||||
//p.Set("0.0.1", []int{1,2,3})
|
||||
c, _ := p.ToJson()
|
||||
fmt.Println(string(c))
|
||||
}
|
||||
|
||||
func makeJson3() {
|
||||
p := gparser.New([]string{"a"})
|
||||
p.Set("0.0.0", []int{1,2,3})
|
||||
c, _ := p.ToJson()
|
||||
fmt.Println(string(c))
|
||||
p := gparser.New([]string{"a"})
|
||||
p.Set("0.0.0", []int{1, 2, 3})
|
||||
c, _ := p.ToJson()
|
||||
fmt.Println(string(c))
|
||||
}
|
||||
|
||||
func toStruct1() {
|
||||
type Info struct {
|
||||
Name string
|
||||
Url string
|
||||
}
|
||||
o := Info{}
|
||||
p := gparser.New(map[string]string{
|
||||
"Name" : "gf",
|
||||
"Url" : "https://gitee.com/johng",
|
||||
})
|
||||
p.ToStruct(&o)
|
||||
fmt.Println("Name:", o.Name)
|
||||
fmt.Println("Url :", o.Url)
|
||||
type Info struct {
|
||||
Name string
|
||||
Url string
|
||||
}
|
||||
o := Info{}
|
||||
p := gparser.New(map[string]string{
|
||||
"Name": "gf",
|
||||
"Url": "https://gitee.com/johng",
|
||||
})
|
||||
p.ToStruct(&o)
|
||||
fmt.Println("Name:", o.Name)
|
||||
fmt.Println("Url :", o.Url)
|
||||
}
|
||||
|
||||
func convert() {
|
||||
p := gparser.New(map[string]string{
|
||||
"name" : "gf",
|
||||
"site" : "https://gitee.com/johng",
|
||||
})
|
||||
c, _ := p.ToJson()
|
||||
fmt.Println("JSON:")
|
||||
fmt.Println(string(c))
|
||||
fmt.Println("======================")
|
||||
p := gparser.New(map[string]string{
|
||||
"name": "gf",
|
||||
"site": "https://gitee.com/johng",
|
||||
})
|
||||
c, _ := p.ToJson()
|
||||
fmt.Println("JSON:")
|
||||
fmt.Println(string(c))
|
||||
fmt.Println("======================")
|
||||
|
||||
fmt.Println("XML:")
|
||||
c, _ = p.ToXmlIndent()
|
||||
fmt.Println(string(c))
|
||||
fmt.Println("======================")
|
||||
fmt.Println("XML:")
|
||||
c, _ = p.ToXmlIndent()
|
||||
fmt.Println(string(c))
|
||||
fmt.Println("======================")
|
||||
|
||||
fmt.Println("YAML:")
|
||||
c, _ = p.ToYaml()
|
||||
fmt.Println(string(c))
|
||||
fmt.Println("======================")
|
||||
fmt.Println("YAML:")
|
||||
c, _ = p.ToYaml()
|
||||
fmt.Println(string(c))
|
||||
fmt.Println("======================")
|
||||
|
||||
fmt.Println("TOML:")
|
||||
c, _ = p.ToToml()
|
||||
fmt.Println(string(c))
|
||||
fmt.Println("TOML:")
|
||||
c, _ = p.ToToml()
|
||||
fmt.Println(string(c))
|
||||
|
||||
}
|
||||
|
||||
func remove1() {
|
||||
p := gparser.New(map[string]string{
|
||||
"k1" : "v1",
|
||||
"k2" : "v2",
|
||||
})
|
||||
p.Set("0.0.0.0.0.0.0.0", []int{1,2,3})
|
||||
p.Remove("0.0")
|
||||
c, _ := p.ToJson()
|
||||
fmt.Println(string(c))
|
||||
p := gparser.New(map[string]string{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
})
|
||||
p.Set("0.0.0.0.0.0.0.0", []int{1, 2, 3})
|
||||
p.Remove("0.0")
|
||||
c, _ := p.ToJson()
|
||||
fmt.Println(string(c))
|
||||
}
|
||||
|
||||
|
||||
func main() {
|
||||
toStruct1()
|
||||
}
|
||||
toStruct1()
|
||||
}
|
||||
|
||||
@ -1,25 +1,25 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
func init() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/apple", Apple)
|
||||
s.BindHandler("/pen", Pen)
|
||||
s.BindHandler("/apple-pen", ApplePen)
|
||||
s := g.Server()
|
||||
s.BindHandler("/apple", Apple)
|
||||
s.BindHandler("/pen", Pen)
|
||||
s.BindHandler("/apple-pen", ApplePen)
|
||||
}
|
||||
|
||||
func Apple(r *ghttp.Request) {
|
||||
r.Response.Write("Apple")
|
||||
r.Response.Write("Apple")
|
||||
}
|
||||
|
||||
func Pen(r *ghttp.Request) {
|
||||
r.Response.Write("Pen")
|
||||
r.Response.Write("Pen")
|
||||
}
|
||||
|
||||
func ApplePen(r *ghttp.Request) {
|
||||
r.Response.Write("Apple-Pen")
|
||||
}
|
||||
r.Response.Write("Apple-Pen")
|
||||
}
|
||||
|
||||
@ -1,16 +1,16 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
type Order struct { }
|
||||
type Order struct{}
|
||||
|
||||
func init() {
|
||||
g.Server().BindObject("/{.struct}-{.method}", new(Order))
|
||||
g.Server().BindObject("/{.struct}-{.method}", new(Order))
|
||||
}
|
||||
|
||||
func (o *Order) List(r *ghttp.Request) {
|
||||
r.Response.Write("List")
|
||||
r.Response.Write("List")
|
||||
}
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/frame/gins"
|
||||
"github.com/gogf/gf/g/frame/gins"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
func init() {
|
||||
ghttp.GetServer().BindHandler("/config", func (r *ghttp.Request) {
|
||||
r.Response.Write(gins.Config().GetString("database.default.0.host"))
|
||||
})
|
||||
ghttp.GetServer().BindHandler("/config", func(r *ghttp.Request) {
|
||||
r.Response.Write(gins.Config().GetString("database.default.0.host"))
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,16 +1,16 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/os/gtime"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/os/gtime"
|
||||
)
|
||||
|
||||
func init() {
|
||||
ghttp.GetServer().BindHandler("/cookie", Cookie)
|
||||
ghttp.GetServer().BindHandler("/cookie", Cookie)
|
||||
}
|
||||
|
||||
func Cookie(r *ghttp.Request) {
|
||||
datetime := r.Cookie.Get("datetime")
|
||||
r.Cookie.Set("datetime", gtime.Datetime())
|
||||
r.Response.Write("datetime:" + datetime)
|
||||
}
|
||||
datetime := r.Cookie.Get("datetime")
|
||||
r.Cookie.Set("datetime", gtime.Datetime())
|
||||
r.Response.Write("datetime:" + datetime)
|
||||
}
|
||||
|
||||
@ -1,21 +1,20 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
type ControllerDomain struct {}
|
||||
type ControllerDomain struct{}
|
||||
|
||||
// 初始化控制器对象,并绑定操作到Web Server
|
||||
func init() {
|
||||
// 只有localhost域名下才能访问该对象,
|
||||
// 对应URL为:http://localhost:8199/test/show
|
||||
// 通过该地址将无法访问到内容:http://127.0.0.1:8199/test/show
|
||||
ghttp.GetServer().Domain("localhost").BindObject("/domain", &ControllerDomain{})
|
||||
// 只有localhost域名下才能访问该对象,
|
||||
// 对应URL为:http://localhost:8199/test/show
|
||||
// 通过该地址将无法访问到内容:http://127.0.0.1:8199/test/show
|
||||
ghttp.GetServer().Domain("localhost").BindObject("/domain", &ControllerDomain{})
|
||||
}
|
||||
|
||||
// 用于对象映射
|
||||
func (d *ControllerDomain) Show(r *ghttp.Request) {
|
||||
r.Response.Write("It's show time bibi!")
|
||||
r.Response.Write("It's show time bibi!")
|
||||
}
|
||||
|
||||
|
||||
@ -1,24 +1,24 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/frame/gmvc"
|
||||
"github.com/gogf/gf/g/frame/gmvc"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
type ControllerExit struct {
|
||||
gmvc.Controller
|
||||
gmvc.Controller
|
||||
}
|
||||
|
||||
func (c *ControllerExit) Init(r *ghttp.Request) {
|
||||
c.Controller.Init(r)
|
||||
c.Response.Write("exit, it will not print \"show\"")
|
||||
c.Request.Exit()
|
||||
c.Controller.Init(r)
|
||||
c.Response.Write("exit, it will not print \"show\"")
|
||||
c.Request.Exit()
|
||||
}
|
||||
|
||||
func (c *ControllerExit) Show() {
|
||||
c.Response.Write("show")
|
||||
c.Response.Write("show")
|
||||
}
|
||||
|
||||
func init() {
|
||||
ghttp.GetServer().BindController("/exit", &ControllerExit{})
|
||||
ghttp.GetServer().BindController("/exit", &ControllerExit{})
|
||||
}
|
||||
|
||||
@ -1,19 +1,19 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"fmt"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
func Form(r *ghttp.Request) {
|
||||
fmt.Println(r.GetPostMap())
|
||||
fmt.Println(r.GetPostString("name"))
|
||||
fmt.Println(r.GetPostString("age"))
|
||||
fmt.Println(r.GetPostMap())
|
||||
fmt.Println(r.GetPostString("name"))
|
||||
fmt.Println(r.GetPostString("age"))
|
||||
|
||||
}
|
||||
|
||||
func FormShow(r *ghttp.Request) {
|
||||
r.Response.Write(`
|
||||
r.Response.Write(`
|
||||
<html>
|
||||
<head>
|
||||
<title>表单提交</title>
|
||||
@ -30,6 +30,6 @@ func FormShow(r *ghttp.Request) {
|
||||
}
|
||||
|
||||
func init() {
|
||||
ghttp.GetServer().BindHandler("/form", Form)
|
||||
ghttp.GetServer().BindHandler("/form/show", FormShow)
|
||||
}
|
||||
ghttp.GetServer().BindHandler("/form", Form)
|
||||
ghttp.GetServer().BindHandler("/form/show", FormShow)
|
||||
}
|
||||
|
||||
@ -3,7 +3,7 @@ package demo
|
||||
import "github.com/gogf/gf/g/net/ghttp"
|
||||
|
||||
func init() {
|
||||
ghttp.GetServer().BindHandler("/", func(r *ghttp.Request){
|
||||
r.Response.Write("Hello World!")
|
||||
})
|
||||
}
|
||||
ghttp.GetServer().BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Write("Hello World!")
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,33 +1,30 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/frame/gmvc"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/frame/gmvc"
|
||||
)
|
||||
|
||||
type Method struct {
|
||||
gmvc.Controller
|
||||
gmvc.Controller
|
||||
}
|
||||
|
||||
func init() {
|
||||
// 第三个参数指定主要注册的方法,其他方法不注册,方法名称会自动追加到给定路由后面,构成新路由
|
||||
// 以下注册会中注册两个新路由: /method/name, /method/age
|
||||
g.Server().BindController("/method", new(Method), "Name, Age")
|
||||
// 绑定路由到指定的方法执行,以下注册只会注册一个路由: /method-name
|
||||
g.Server().BindControllerMethod("/method-name", new(Method), "Name")
|
||||
// 第三个参数指定主要注册的方法,其他方法不注册,方法名称会自动追加到给定路由后面,构成新路由
|
||||
// 以下注册会中注册两个新路由: /method/name, /method/age
|
||||
g.Server().BindController("/method", new(Method), "Name, Age")
|
||||
// 绑定路由到指定的方法执行,以下注册只会注册一个路由: /method-name
|
||||
g.Server().BindControllerMethod("/method-name", new(Method), "Name")
|
||||
}
|
||||
|
||||
func (c *Method) Name() {
|
||||
c.Response.Write("John")
|
||||
c.Response.Write("John")
|
||||
}
|
||||
|
||||
func (c *Method) Age() {
|
||||
c.Response.Write("18")
|
||||
c.Response.Write("18")
|
||||
}
|
||||
|
||||
func (c *Method) Info() {
|
||||
c.Response.Write("Info")
|
||||
c.Response.Write("Info")
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,21 +1,20 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
type Object struct {}
|
||||
type Object struct{}
|
||||
|
||||
func init() {
|
||||
g.Server().BindObject("/object", new(Object))
|
||||
g.Server().BindObject("/object", new(Object))
|
||||
}
|
||||
|
||||
func (o *Object) Index(r *ghttp.Request) {
|
||||
r.Response.Write("object index")
|
||||
r.Response.Write("object index")
|
||||
}
|
||||
|
||||
func (o *Object) Show(r *ghttp.Request) {
|
||||
r.Response.Write("object show")
|
||||
r.Response.Write("object show")
|
||||
}
|
||||
|
||||
|
||||
@ -1,33 +1,31 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
type ObjectMethod struct {}
|
||||
type ObjectMethod struct{}
|
||||
|
||||
func init() {
|
||||
obj := &ObjectMethod{}
|
||||
g.Server().BindObject("/object-method", obj, "Show1, Show2, Show3")
|
||||
g.Server().BindObjectMethod("/object-method-show1", obj, "Show1")
|
||||
g.Server().Domain("localhost").BindObject("/object-method", obj, "Show4")
|
||||
obj := &ObjectMethod{}
|
||||
g.Server().BindObject("/object-method", obj, "Show1, Show2, Show3")
|
||||
g.Server().BindObjectMethod("/object-method-show1", obj, "Show1")
|
||||
g.Server().Domain("localhost").BindObject("/object-method", obj, "Show4")
|
||||
}
|
||||
|
||||
func (o *ObjectMethod) Show1(r *ghttp.Request) {
|
||||
r.Response.Write("show 1")
|
||||
r.Response.Write("show 1")
|
||||
}
|
||||
|
||||
func (o *ObjectMethod) Show2(r *ghttp.Request) {
|
||||
r.Response.Write("show 2")
|
||||
r.Response.Write("show 2")
|
||||
}
|
||||
|
||||
func (o *ObjectMethod) Show3(r *ghttp.Request) {
|
||||
r.Response.Write("show 3")
|
||||
r.Response.Write("show 3")
|
||||
}
|
||||
|
||||
func (o *ObjectMethod) Show4(r *ghttp.Request) {
|
||||
r.Response.Write("show 4")
|
||||
r.Response.Write("show 4")
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -3,28 +3,28 @@ package demo
|
||||
import "github.com/gogf/gf/g/net/ghttp"
|
||||
|
||||
// 测试绑定对象
|
||||
type ObjectRest struct {}
|
||||
type ObjectRest struct{}
|
||||
|
||||
func init() {
|
||||
ghttp.GetServer().BindObjectRest("/object-rest", &ObjectRest{})
|
||||
ghttp.GetServer().BindObjectRest("/object-rest", &ObjectRest{})
|
||||
}
|
||||
|
||||
// RESTFul - GET
|
||||
func (o *ObjectRest) Get(r *ghttp.Request) {
|
||||
r.Response.Write("RESTFul HTTP Method GET")
|
||||
r.Response.Write("RESTFul HTTP Method GET")
|
||||
}
|
||||
|
||||
// RESTFul - POST
|
||||
func (c *ObjectRest) Post(r *ghttp.Request) {
|
||||
r.Response.Write("RESTFul HTTP Method POST")
|
||||
r.Response.Write("RESTFul HTTP Method POST")
|
||||
}
|
||||
|
||||
// RESTFul - DELETE
|
||||
func (c *ObjectRest) Delete(r *ghttp.Request) {
|
||||
r.Response.Write("RESTFul HTTP Method DELETE")
|
||||
r.Response.Write("RESTFul HTTP Method DELETE")
|
||||
}
|
||||
|
||||
// 该方法无法映射,将会无法访问到
|
||||
func (c *ObjectRest) Hello(r *ghttp.Request) {
|
||||
r.Response.Write("Hello")
|
||||
}
|
||||
r.Response.Write("Hello")
|
||||
}
|
||||
|
||||
@ -1,25 +1,25 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
type Product struct {
|
||||
total int
|
||||
total int
|
||||
}
|
||||
|
||||
func init() {
|
||||
p := &Product{}
|
||||
g.Server().BindHandler("/product/total", p.Total)
|
||||
g.Server().BindHandler("/product/list/{page}.html", p.List)
|
||||
p := &Product{}
|
||||
g.Server().BindHandler("/product/total", p.Total)
|
||||
g.Server().BindHandler("/product/list/{page}.html", p.List)
|
||||
}
|
||||
|
||||
func (p *Product) Total(r *ghttp.Request) {
|
||||
p.total++
|
||||
r.Response.Write("total: ", p.total)
|
||||
p.total++
|
||||
r.Response.Write("total: ", p.total)
|
||||
}
|
||||
|
||||
func (p *Product) List(r *ghttp.Request) {
|
||||
r.Response.Write("page: ", r.Get("page"))
|
||||
r.Response.Write("page: ", r.Get("page"))
|
||||
}
|
||||
|
||||
@ -1,37 +1,34 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/frame/gmvc"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/frame/gmvc"
|
||||
)
|
||||
|
||||
type Rest struct {
|
||||
gmvc.Controller
|
||||
gmvc.Controller
|
||||
}
|
||||
|
||||
func init() {
|
||||
g.Server().BindControllerRest("/rest", &Rest{})
|
||||
g.Server().BindControllerRest("/rest", &Rest{})
|
||||
}
|
||||
|
||||
// RESTFul - GET
|
||||
func (c *Rest) Get() {
|
||||
c.Response.Write("RESTFul HTTP Method GET")
|
||||
c.Response.Write("RESTFul HTTP Method GET")
|
||||
}
|
||||
|
||||
// RESTFul - POST
|
||||
func (c *Rest) Post() {
|
||||
c.Response.Write("RESTFul HTTP Method POST")
|
||||
c.Response.Write("RESTFul HTTP Method POST")
|
||||
}
|
||||
|
||||
// RESTFul - DELETE
|
||||
func (c *Rest) Delete() {
|
||||
c.Response.Write("RESTFul HTTP Method DELETE")
|
||||
c.Response.Write("RESTFul HTTP Method DELETE")
|
||||
}
|
||||
|
||||
// 该方法无法映射,将会无法访问到
|
||||
func (c *Rest) Hello() {
|
||||
c.Response.Write("Hello")
|
||||
c.Response.Write("Hello")
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,19 +1,18 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/frame/gmvc"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/frame/gmvc"
|
||||
)
|
||||
|
||||
type ControllerRule struct {
|
||||
gmvc.Controller
|
||||
gmvc.Controller
|
||||
}
|
||||
|
||||
func init() {
|
||||
g.Server().BindController("/rule/{method}/:name", &ControllerRule{})
|
||||
g.Server().BindController("/rule/{method}/:name", &ControllerRule{})
|
||||
}
|
||||
|
||||
func (c *ControllerRule) Show() {
|
||||
c.Response.Write(c.Request.Get("name"))
|
||||
c.Response.Write(c.Request.Get("name"))
|
||||
}
|
||||
|
||||
|
||||
@ -1,16 +1,16 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func init() {
|
||||
ghttp.GetServer().BindHandler("/session", Session)
|
||||
ghttp.GetServer().BindHandler("/session", Session)
|
||||
}
|
||||
|
||||
func Session(r *ghttp.Request) {
|
||||
id := r.Session.GetInt("id")
|
||||
r.Session.Set("id", id + 1)
|
||||
r.Response.Write("id:" + strconv.Itoa(id))
|
||||
}
|
||||
id := r.Session.GetInt("id")
|
||||
r.Session.Set("id", id+1)
|
||||
r.Response.Write("id:" + strconv.Itoa(id))
|
||||
}
|
||||
|
||||
@ -1,28 +1,23 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/frame/gmvc"
|
||||
"github.com/gogf/gf/g/frame/gmvc"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
type ControllerTemplate struct {
|
||||
gmvc.Controller
|
||||
gmvc.Controller
|
||||
}
|
||||
|
||||
func (c *ControllerTemplate) Info() {
|
||||
c.View.Assign("name", "john")
|
||||
c.View.Assigns(map[string]interface{}{
|
||||
"age" : 18,
|
||||
"score" : 100,
|
||||
})
|
||||
c.View.Display("view/user/index.tpl")
|
||||
c.View.Assign("name", "john")
|
||||
c.View.Assigns(map[string]interface{}{
|
||||
"age": 18,
|
||||
"score": 100,
|
||||
})
|
||||
c.View.Display("view/user/index.tpl")
|
||||
}
|
||||
|
||||
func init() {
|
||||
ghttp.GetServer().BindController("/template", &ControllerTemplate{})
|
||||
ghttp.GetServer().BindController("/template", &ControllerTemplate{})
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,16 +1,16 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
func init() {
|
||||
ghttp.GetServer().BindHandler("/template2", func(r *ghttp.Request){
|
||||
content, _ := g.View().Parse("index.tpl", map[string]interface{}{
|
||||
"id" : 123,
|
||||
"name" : "john",
|
||||
})
|
||||
r.Response.Write(content)
|
||||
})
|
||||
}
|
||||
ghttp.GetServer().BindHandler("/template2", func(r *ghttp.Request) {
|
||||
content, _ := g.View().Parse("index.tpl", map[string]interface{}{
|
||||
"id": 123,
|
||||
"name": "john",
|
||||
})
|
||||
r.Response.Write(content)
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,17 +1,17 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/frame/gins"
|
||||
"github.com/gogf/gf/g/frame/gins"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
func init() {
|
||||
gins.View().SetPath("/home/www/template/")
|
||||
ghttp.GetServer().BindHandler("/template3", func(r *ghttp.Request){
|
||||
content, _ := gins.View().Parse("index.tpl", map[string]interface{}{
|
||||
"id" : 123,
|
||||
"name" : "john",
|
||||
})
|
||||
r.Response.Write(content)
|
||||
})
|
||||
}
|
||||
gins.View().SetPath("/home/www/template/")
|
||||
ghttp.GetServer().BindHandler("/template3", func(r *ghttp.Request) {
|
||||
content, _ := gins.View().Parse("index.tpl", map[string]interface{}{
|
||||
"id": 123,
|
||||
"name": "john",
|
||||
})
|
||||
r.Response.Write(content)
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,25 +1,25 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
)
|
||||
|
||||
func Upload(r *ghttp.Request) {
|
||||
if f, h, e := r.FormFile("upload-file"); e == nil {
|
||||
defer f.Close()
|
||||
fname := gfile.Basename(h.Filename)
|
||||
buffer := make([]byte, h.Size)
|
||||
f.Read(buffer)
|
||||
gfile.PutBinContents("/tmp/" + fname, buffer)
|
||||
r.Response.Write(fname + " uploaded successly")
|
||||
} else {
|
||||
r.Response.Write(e.Error())
|
||||
}
|
||||
if f, h, e := r.FormFile("upload-file"); e == nil {
|
||||
defer f.Close()
|
||||
fname := gfile.Basename(h.Filename)
|
||||
buffer := make([]byte, h.Size)
|
||||
f.Read(buffer)
|
||||
gfile.PutBinContents("/tmp/"+fname, buffer)
|
||||
r.Response.Write(fname + " uploaded successly")
|
||||
} else {
|
||||
r.Response.Write(e.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func UploadShow(r *ghttp.Request) {
|
||||
r.Response.Write(`
|
||||
r.Response.Write(`
|
||||
<html>
|
||||
<head>
|
||||
<title>上传文件</title>
|
||||
@ -35,6 +35,6 @@ func UploadShow(r *ghttp.Request) {
|
||||
}
|
||||
|
||||
func init() {
|
||||
ghttp.GetServer().BindHandler("/upload", Upload)
|
||||
ghttp.GetServer().BindHandler("/upload/show", UploadShow)
|
||||
}
|
||||
ghttp.GetServer().BindHandler("/upload", Upload)
|
||||
ghttp.GetServer().BindHandler("/upload/show", UploadShow)
|
||||
}
|
||||
|
||||
@ -1,33 +1,29 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/frame/gmvc"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/frame/gmvc"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
gmvc.Controller
|
||||
gmvc.Controller
|
||||
}
|
||||
|
||||
func init() {
|
||||
s := g.Server()
|
||||
s.BindController("/user", new(User))
|
||||
s.BindController("/user/{.method}/{uid}", new(User), "Info")
|
||||
s.BindController("/user/{.method}/{page}.html", new(User), "List")
|
||||
s := g.Server()
|
||||
s.BindController("/user", new(User))
|
||||
s.BindController("/user/{.method}/{uid}", new(User), "Info")
|
||||
s.BindController("/user/{.method}/{page}.html", new(User), "List")
|
||||
}
|
||||
|
||||
func (u *User) Index() {
|
||||
u.Response.Write("User")
|
||||
u.Response.Write("User")
|
||||
}
|
||||
|
||||
func (u *User) Info() {
|
||||
u.Response.Write("Info - Uid: ", u.Request.Get("uid"))
|
||||
u.Response.Write("Info - Uid: ", u.Request.Get("uid"))
|
||||
}
|
||||
|
||||
func (u *User) List() {
|
||||
u.Response.Write("List - Page: ", u.Request.Get("page"))
|
||||
u.Response.Write("List - Page: ", u.Request.Get("page"))
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,19 +1,19 @@
|
||||
package stats
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
var (
|
||||
total int
|
||||
total int
|
||||
)
|
||||
|
||||
func init() {
|
||||
g.Server().BindHandler("/stats/total", showTotal)
|
||||
g.Server().BindHandler("/stats/total", showTotal)
|
||||
}
|
||||
|
||||
func showTotal(r *ghttp.Request) {
|
||||
total++
|
||||
r.Response.Write("total:", total)
|
||||
total++
|
||||
r.Response.Write("total:", total)
|
||||
}
|
||||
|
||||
@ -1,15 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
_ "github.com/gogf/gf/geg/frame/mvc/controller/demo"
|
||||
_ "github.com/gogf/gf/geg/frame/mvc/controller/stats"
|
||||
"github.com/gogf/gf/g"
|
||||
_ "github.com/gogf/gf/geg/frame/mvc/controller/demo"
|
||||
_ "github.com/gogf/gf/geg/frame/mvc/controller/stats"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
//g.Server().SetDumpRouteMap(false)
|
||||
g.Server().SetPort(8199)
|
||||
g.Server().Run()
|
||||
//g.Server().SetDumpRouteMap(false)
|
||||
g.Server().SetPort(8199)
|
||||
g.Server().Run()
|
||||
|
||||
}
|
||||
|
||||
@ -1,17 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
)
|
||||
|
||||
func main() {
|
||||
c := ghttp.NewClient()
|
||||
c.SetHeader("Cookie", "name=john; score=100")
|
||||
if r, e := c.Get("http://127.0.0.1:8199/"); e != nil {
|
||||
glog.Error(e)
|
||||
} else {
|
||||
fmt.Println(string(r.ReadAll()))
|
||||
}
|
||||
c := ghttp.NewClient()
|
||||
c.SetHeader("Cookie", "name=john; score=100")
|
||||
if r, e := c.Get("http://127.0.0.1:8199/"); e != nil {
|
||||
glog.Error(e)
|
||||
} else {
|
||||
fmt.Println(string(r.ReadAll()))
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,15 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/", func(r *ghttp.Request){
|
||||
r.Response.Writeln(r.Cookie.Map())
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
s := g.Server()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Writeln(r.Cookie.Map())
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
c := ghttp.NewClient()
|
||||
r, _ := c.Get("http://baidu.com")
|
||||
fmt.Println(r.StatusCode)
|
||||
c := ghttp.NewClient()
|
||||
r, _ := c.Get("http://baidu.com")
|
||||
fmt.Println(r.StatusCode)
|
||||
}
|
||||
|
||||
@ -1,18 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
)
|
||||
|
||||
func main() {
|
||||
path := "/home/john/Workspace/Go/github.com/gogf/gf/version.go"
|
||||
r, e := ghttp.Post("http://127.0.0.1:8199/upload", "name=john&age=18&upload-file=@file:" + path)
|
||||
if e != nil {
|
||||
glog.Error(e)
|
||||
} else {
|
||||
fmt.Println(string(r.ReadAll()))
|
||||
r.Close()
|
||||
}
|
||||
}
|
||||
path := "/home/john/Workspace/Go/github.com/gogf/gf/version.go"
|
||||
r, e := ghttp.Post("http://127.0.0.1:8199/upload", "name=john&age=18&upload-file=@file:"+path)
|
||||
if e != nil {
|
||||
glog.Error(e)
|
||||
} else {
|
||||
fmt.Println(string(r.ReadAll()))
|
||||
r.Close()
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,28 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
)
|
||||
|
||||
// 执行文件上传处理,上传到系统临时目录 /tmp
|
||||
func Upload(r *ghttp.Request) {
|
||||
if f, h, e := r.FormFile("upload-file"); e == nil {
|
||||
defer f.Close()
|
||||
name := gfile.Basename(h.Filename)
|
||||
buffer := make([]byte, h.Size)
|
||||
f.Read(buffer)
|
||||
gfile.PutBinContents("/tmp/" + name, buffer)
|
||||
r.Response.Write(name + " uploaded successly")
|
||||
} else {
|
||||
r.Response.Write(e.Error())
|
||||
}
|
||||
if f, h, e := r.FormFile("upload-file"); e == nil {
|
||||
defer f.Close()
|
||||
name := gfile.Basename(h.Filename)
|
||||
buffer := make([]byte, h.Size)
|
||||
f.Read(buffer)
|
||||
gfile.PutBinContents("/tmp/"+name, buffer)
|
||||
r.Response.Write(name + " uploaded successly")
|
||||
} else {
|
||||
r.Response.Write(e.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// 展示文件上传页面
|
||||
func UploadShow(r *ghttp.Request) {
|
||||
r.Response.Write(`
|
||||
r.Response.Write(`
|
||||
<html>
|
||||
<head>
|
||||
<title>上传文件</title>
|
||||
@ -38,9 +38,9 @@ func UploadShow(r *ghttp.Request) {
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/upload", Upload)
|
||||
s.BindHandler("/upload/show", UploadShow)
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
s := g.Server()
|
||||
s.BindHandler("/upload", Upload)
|
||||
s.BindHandler("/upload/show", UploadShow)
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -1,32 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/frame/gmvc"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/frame/gmvc"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
gmvc.Controller
|
||||
gmvc.Controller
|
||||
}
|
||||
|
||||
func (c *User) Index() {
|
||||
c.View.Display("index.html")
|
||||
c.View.Display("index.html")
|
||||
}
|
||||
|
||||
// 不符合规范,不会被自动注册
|
||||
func (c *User) Test(value interface{}) {
|
||||
c.View.Display("index.html")
|
||||
c.View.Display("index.html")
|
||||
}
|
||||
|
||||
func main() {
|
||||
//g.View().SetPath("C:/www/static")
|
||||
s := g.Server()
|
||||
s.BindController("/user", new(User))
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
//g.View().SetPath("C:/www/static")
|
||||
s := g.Server()
|
||||
s.BindController("/user", new(User))
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,18 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/os/gtime"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/os/gtime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/cookie", func(r *ghttp.Request) {
|
||||
datetime := r.Cookie.Get("datetime")
|
||||
r.Cookie.Set("datetime", gtime.Datetime())
|
||||
r.Response.Write("datetime:", datetime)
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
s := g.Server()
|
||||
s.BindHandler("/cookie", func(r *ghttp.Request) {
|
||||
datetime := r.Cookie.Get("datetime")
|
||||
r.Cookie.Set("datetime", gtime.Datetime())
|
||||
r.Response.Write("datetime:", datetime)
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -3,10 +3,10 @@ package main
|
||||
import "github.com/gogf/gf/g"
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.SetDenyRoutes([]string{
|
||||
"/config*",
|
||||
})
|
||||
s.SetPort(8299)
|
||||
s.Run()
|
||||
s := g.Server()
|
||||
s.SetDenyRoutes([]string{
|
||||
"/config*",
|
||||
})
|
||||
s.SetPort(8299)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -3,17 +3,17 @@ package main
|
||||
import "github.com/gogf/gf/g/net/ghttp"
|
||||
|
||||
func Hello1(r *ghttp.Request) {
|
||||
r.Response.Write("127.0.0.1: Hello World1!")
|
||||
r.Response.Write("127.0.0.1: Hello World1!")
|
||||
}
|
||||
|
||||
func Hello2(r *ghttp.Request) {
|
||||
r.Response.Write("localhost: Hello World2!")
|
||||
r.Response.Write("localhost: Hello World2!")
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := ghttp.GetServer()
|
||||
s.Domain("127.0.0.1").BindHandler("/", Hello1)
|
||||
s.Domain("localhost, local").BindHandler("/", Hello2)
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
s := ghttp.GetServer()
|
||||
s.Domain("127.0.0.1").BindHandler("/", Hello1)
|
||||
s.Domain("localhost, local").BindHandler("/", Hello2)
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -1,20 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/download", func(r *ghttp.Request){
|
||||
r.Response.Header().Set("Content-Type", "text/html;charset=utf-8");
|
||||
r.Response.Header().Set("Content-type", "application/force-download");
|
||||
r.Response.Header().Set("Content-Type", "application/octet-stream");
|
||||
r.Response.Header().Set("Accept-Ranges", "bytes");
|
||||
r.Response.Header().Set("Content-Disposition", "attachment;filename=\"下载文件名称.txt\"");
|
||||
r.Response.ServeFile("text.txt")
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
s := g.Server()
|
||||
s.BindHandler("/download", func(r *ghttp.Request) {
|
||||
r.Response.Header().Set("Content-Type", "text/html;charset=utf-8")
|
||||
r.Response.Header().Set("Content-type", "application/force-download")
|
||||
r.Response.Header().Set("Content-Type", "application/octet-stream")
|
||||
r.Response.Header().Set("Accept-Ranges", "bytes")
|
||||
r.Response.Header().Set("Content-Disposition", "attachment;filename=\"下载文件名称.txt\"")
|
||||
r.Response.ServeFile("text.txt")
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -2,18 +2,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/", func(r *ghttp.Request){
|
||||
r.Response.Writeln("哈喽世界!")
|
||||
})
|
||||
s.BindHandler("/", func(r *ghttp.Request){
|
||||
r.Response.Writeln("哈喽世界!")
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
s := g.Server()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Writeln("哈喽世界!")
|
||||
})
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Writeln("哈喽世界!")
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -2,32 +2,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/frame/gmvc"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/frame/gmvc"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
gmvc.Controller
|
||||
gmvc.Controller
|
||||
}
|
||||
|
||||
func (u *User) Index() {
|
||||
u.Response.Write("User")
|
||||
u.Response.Write("User")
|
||||
}
|
||||
|
||||
func (u *User) Info() {
|
||||
u.Response.Write("Info - Uid: ", u.Request.Get("uid"))
|
||||
u.Response.Write("Info - Uid: ", u.Request.Get("uid"))
|
||||
}
|
||||
|
||||
func (u *User) List() {
|
||||
u.Response.Write("List - Page: ", u.Request.Get("page"))
|
||||
u.Response.Write("List - Page: ", u.Request.Get("page"))
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindController("/user", new(User))
|
||||
s.BindController("/user/{.method}/{uid}", new(User), "Info")
|
||||
s.BindController("/user/{.method}/{page}.html", new(User), "List")
|
||||
s.BindController("/user/{.method}/{page}.html", new(User), "List")
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
s := g.Server()
|
||||
s.BindController("/user", new(User))
|
||||
s.BindController("/user/{.method}/{uid}", new(User), "Info")
|
||||
s.BindController("/user/{.method}/{page}.html", new(User), "List")
|
||||
s.BindController("/user/{.method}/{page}.html", new(User), "List")
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -2,24 +2,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
type Object struct {}
|
||||
type Object struct{}
|
||||
|
||||
func (o *Object) Index(r *ghttp.Request) {
|
||||
r.Response.Write("object index")
|
||||
r.Response.Write("object index")
|
||||
}
|
||||
|
||||
func (o *Object) Show(r *ghttp.Request) {
|
||||
r.Response.Write("object show")
|
||||
r.Response.Write("object show")
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
g.Server().BindObject("/object", new(Object))
|
||||
g.Server().BindObject("/object", new(Object))
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
s := g.Server()
|
||||
g.Server().BindObject("/object", new(Object))
|
||||
g.Server().BindObject("/object", new(Object))
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -1,27 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
)
|
||||
|
||||
func main() {
|
||||
p := "/"
|
||||
s := g.Server()
|
||||
s.BindHandler(p, func(r *ghttp.Request) {
|
||||
r.Response.Writeln("start")
|
||||
r.Exit()
|
||||
r.Response.Writeln("end")
|
||||
})
|
||||
s.BindHookHandlerByMap(p, map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_SERVE : func(r *ghttp.Request){
|
||||
glog.To(r.Response.Writer).Println("BeforeServe")
|
||||
},
|
||||
ghttp.HOOK_AFTER_SERVE : func(r *ghttp.Request){
|
||||
glog.To(r.Response.Writer).Println("AfterServe")
|
||||
},
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
p := "/"
|
||||
s := g.Server()
|
||||
s.BindHandler(p, func(r *ghttp.Request) {
|
||||
r.Response.Writeln("start")
|
||||
r.Exit()
|
||||
r.Response.Writeln("end")
|
||||
})
|
||||
s.BindHookHandlerByMap(p, map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
|
||||
glog.To(r.Response.Writer).Println("BeforeServe")
|
||||
},
|
||||
ghttp.HOOK_AFTER_SERVE: func(r *ghttp.Request) {
|
||||
glog.To(r.Response.Writer).Println("AfterServe")
|
||||
},
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -1,17 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/os/gproc"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/os/gproc"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.SetIndexFolder(true)
|
||||
s.BindHandler("/", func(r *ghttp.Request){
|
||||
r.Response.Write("pid:", gproc.Pid())
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
s := g.Server()
|
||||
s.SetIndexFolder(true)
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Write("pid:", gproc.Pid())
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -1,27 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/frame/gmvc"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/frame/gmvc"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
type Order struct {
|
||||
gmvc.Controller
|
||||
gmvc.Controller
|
||||
}
|
||||
|
||||
func (o *Order) Get() {
|
||||
o.Response.Write("GET")
|
||||
o.Response.Write("GET")
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHookHandlerByMap("/api.v1/*any", map[string]ghttp.HandlerFunc {
|
||||
"BeforeServe" : func(r *ghttp.Request) {
|
||||
r.Response.SetAllowCrossDomainRequest("*", "PUT,GET,POST,DELETE,OPTIONS")
|
||||
},
|
||||
})
|
||||
s.BindControllerRest("/api.v1/{.struct}", new(Order))
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
s := g.Server()
|
||||
s.BindHookHandlerByMap("/api.v1/*any", map[string]ghttp.HandlerFunc{
|
||||
"BeforeServe": func(r *ghttp.Request) {
|
||||
r.Response.SetAllowCrossDomainRequest("*", "PUT,GET,POST,DELETE,OPTIONS")
|
||||
},
|
||||
})
|
||||
s.BindControllerRest("/api.v1/{.struct}", new(Order))
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -1,27 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/frame/gmvc"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/frame/gmvc"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
type Order2 struct {
|
||||
gmvc.Controller
|
||||
gmvc.Controller
|
||||
}
|
||||
|
||||
func (o *Order2) Get() {
|
||||
o.Response.Write("GET")
|
||||
o.Response.Write("GET")
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHookHandlerByMap("/api.v2/*any", map[string]ghttp.HandlerFunc {
|
||||
"BeforeServe" : func(r *ghttp.Request) {
|
||||
r.Response.CORSDefault()
|
||||
},
|
||||
})
|
||||
s.BindControllerRest("/api.v2/{.struct}", new(Order2))
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
s := g.Server()
|
||||
s.BindHookHandlerByMap("/api.v2/*any", map[string]ghttp.HandlerFunc{
|
||||
"BeforeServe": func(r *ghttp.Request) {
|
||||
r.Response.CORSDefault()
|
||||
},
|
||||
})
|
||||
s.BindControllerRest("/api.v2/{.struct}", new(Order2))
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -1,26 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 基本事件回调使用
|
||||
p := "/:name/info/{uid}"
|
||||
s := g.Server()
|
||||
s.BindHookHandlerByMap(p, map[string]ghttp.HandlerFunc{
|
||||
"BeforeServe" : func(r *ghttp.Request){ glog.Println("BeforeServe") },
|
||||
"AfterServe" : func(r *ghttp.Request){ glog.Println("AfterServe") },
|
||||
"BeforeOutput" : func(r *ghttp.Request){ glog.Println("BeforeOutput") },
|
||||
"AfterOutput" : func(r *ghttp.Request){ glog.Println("AfterOutput") },
|
||||
"BeforeClose" : func(r *ghttp.Request){ glog.Println("BeforeClose") },
|
||||
"AfterClose" : func(r *ghttp.Request){ glog.Println("AfterClose") },
|
||||
})
|
||||
s.BindHandler(p, func(r *ghttp.Request) {
|
||||
r.Response.Write("用户:", r.Get("name"), ", uid:", r.Get("uid"))
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
// 基本事件回调使用
|
||||
p := "/:name/info/{uid}"
|
||||
s := g.Server()
|
||||
s.BindHookHandlerByMap(p, map[string]ghttp.HandlerFunc{
|
||||
"BeforeServe": func(r *ghttp.Request) { glog.Println("BeforeServe") },
|
||||
"AfterServe": func(r *ghttp.Request) { glog.Println("AfterServe") },
|
||||
"BeforeOutput": func(r *ghttp.Request) { glog.Println("BeforeOutput") },
|
||||
"AfterOutput": func(r *ghttp.Request) { glog.Println("AfterOutput") },
|
||||
"BeforeClose": func(r *ghttp.Request) { glog.Println("BeforeClose") },
|
||||
"AfterClose": func(r *ghttp.Request) { glog.Println("AfterClose") },
|
||||
})
|
||||
s.BindHandler(p, func(r *ghttp.Request) {
|
||||
r.Response.Write("用户:", r.Get("name"), ", uid:", r.Get("uid"))
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -1,37 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s := g.Server()
|
||||
|
||||
// 多事件回调示例,事件1
|
||||
pattern1 := "/:name/info"
|
||||
s.BindHookHandlerByMap(pattern1, map[string]ghttp.HandlerFunc {
|
||||
"BeforeServe" : func(r *ghttp.Request) {
|
||||
r.SetQuery("uid", "1000")
|
||||
},
|
||||
})
|
||||
s.BindHandler(pattern1, func(r *ghttp.Request) {
|
||||
r.Response.Write("用户:", r.Get("name"), ", uid:", r.GetQueryString("uid"))
|
||||
})
|
||||
// 多事件回调示例,事件1
|
||||
pattern1 := "/:name/info"
|
||||
s.BindHookHandlerByMap(pattern1, map[string]ghttp.HandlerFunc{
|
||||
"BeforeServe": func(r *ghttp.Request) {
|
||||
r.SetQuery("uid", "1000")
|
||||
},
|
||||
})
|
||||
s.BindHandler(pattern1, func(r *ghttp.Request) {
|
||||
r.Response.Write("用户:", r.Get("name"), ", uid:", r.GetQueryString("uid"))
|
||||
})
|
||||
|
||||
// 多事件回调示例,事件2
|
||||
pattern2 := "/{object}/list/{page}.java"
|
||||
s.BindHookHandlerByMap(pattern2, map[string]ghttp.HandlerFunc {
|
||||
"BeforeOutput" : func(r *ghttp.Request){
|
||||
r.Response.SetBuffer([]byte(
|
||||
fmt.Sprintf("通过事件修改输出内容, object:%s, page:%s", r.Get("object"), r.GetRouterString("page"))),
|
||||
)
|
||||
},
|
||||
})
|
||||
s.BindHandler(pattern2, func(r *ghttp.Request) {
|
||||
r.Response.Write(r.Router.Uri)
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
// 多事件回调示例,事件2
|
||||
pattern2 := "/{object}/list/{page}.java"
|
||||
s.BindHookHandlerByMap(pattern2, map[string]ghttp.HandlerFunc{
|
||||
"BeforeOutput": func(r *ghttp.Request) {
|
||||
r.Response.SetBuffer([]byte(
|
||||
fmt.Sprintf("通过事件修改输出内容, object:%s, page:%s", r.Get("object"), r.GetRouterString("page"))),
|
||||
)
|
||||
},
|
||||
})
|
||||
s.BindHandler(pattern2, func(r *ghttp.Request) {
|
||||
r.Response.Write(r.Router.Uri)
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -1,32 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/priority/show", func(r *ghttp.Request) {
|
||||
r.Response.Write("priority test")
|
||||
})
|
||||
s := g.Server()
|
||||
s.BindHandler("/priority/show", func(r *ghttp.Request) {
|
||||
r.Response.Write("priority test")
|
||||
})
|
||||
|
||||
s.BindHookHandlerByMap("/priority/:name", map[string]ghttp.HandlerFunc {
|
||||
"BeforeServe" : func(r *ghttp.Request) {
|
||||
glog.Println(r.Router.Uri)
|
||||
},
|
||||
})
|
||||
s.BindHookHandlerByMap("/priority/*any", map[string]ghttp.HandlerFunc {
|
||||
"BeforeServe" : func(r *ghttp.Request) {
|
||||
glog.Println(r.Router.Uri)
|
||||
},
|
||||
})
|
||||
s.BindHookHandlerByMap("/priority/show", map[string]ghttp.HandlerFunc {
|
||||
"BeforeServe" : func(r *ghttp.Request) {
|
||||
glog.Println(r.Router.Uri)
|
||||
},
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
s.BindHookHandlerByMap("/priority/:name", map[string]ghttp.HandlerFunc{
|
||||
"BeforeServe": func(r *ghttp.Request) {
|
||||
glog.Println(r.Router.Uri)
|
||||
},
|
||||
})
|
||||
s.BindHookHandlerByMap("/priority/*any", map[string]ghttp.HandlerFunc{
|
||||
"BeforeServe": func(r *ghttp.Request) {
|
||||
glog.Println(r.Router.Uri)
|
||||
},
|
||||
})
|
||||
s.BindHookHandlerByMap("/priority/show", map[string]ghttp.HandlerFunc{
|
||||
"BeforeServe": func(r *ghttp.Request) {
|
||||
glog.Println(r.Router.Uri)
|
||||
},
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -1,19 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHookHandler("/*any", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
r.Response.Writeln("/*any")
|
||||
})
|
||||
s.BindHookHandler("/v1/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
r.Response.Writeln("/v1/*")
|
||||
r.ExitHook()
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
s := g.Server()
|
||||
s.BindHookHandler("/*any", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
r.Response.Writeln("/*any")
|
||||
})
|
||||
s.BindHookHandler("/v1/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
r.Response.Writeln("/v1/*")
|
||||
r.ExitHook()
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -1,20 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Writeln(r.GetParam("name").String())
|
||||
})
|
||||
s.BindHookHandlerByMap("/", map[string]ghttp.HandlerFunc {
|
||||
ghttp.HOOK_BEFORE_SERVE : func(r *ghttp.Request) {
|
||||
r.SetParam("name", "john")
|
||||
},
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
s := g.Server()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Writeln(r.GetParam("name").String())
|
||||
})
|
||||
s.BindHookHandlerByMap("/", map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
|
||||
r.SetParam("name", "john")
|
||||
},
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -1,32 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
// 优先调用的HOOK
|
||||
func beforeServeHook1(r *ghttp.Request) {
|
||||
r.SetParam("name", "GoFrame")
|
||||
r.Response.Writeln("set name")
|
||||
r.SetParam("name", "GoFrame")
|
||||
r.Response.Writeln("set name")
|
||||
}
|
||||
|
||||
// 随后调用的HOOK
|
||||
func beforeServeHook2(r *ghttp.Request) {
|
||||
r.SetParam("site", "https://goframe.org")
|
||||
r.Response.Writeln("set site")
|
||||
r.SetParam("site", "https://goframe.org")
|
||||
r.Response.Writeln("set site")
|
||||
}
|
||||
|
||||
// 允许对同一个路由同一个事件注册多个回调函数,按照注册顺序进行优先级调用。
|
||||
// 为便于在路由表中对比查看优先级,这里讲HOOK回调函数单独定义为了两个函数。
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Writeln(r.GetParam("name").String())
|
||||
r.Response.Writeln(r.GetParam("site").String())
|
||||
})
|
||||
s.BindHookHandler("/", ghttp.HOOK_BEFORE_SERVE, beforeServeHook1)
|
||||
s.BindHookHandler("/", ghttp.HOOK_BEFORE_SERVE, beforeServeHook2)
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
s := g.Server()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Writeln(r.GetParam("name").String())
|
||||
r.Response.Writeln(r.GetParam("site").String())
|
||||
})
|
||||
s.BindHookHandler("/", ghttp.HOOK_BEFORE_SERVE, beforeServeHook1)
|
||||
s.BindHookHandler("/", ghttp.HOOK_BEFORE_SERVE, beforeServeHook2)
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -1,15 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := ghttp.GetServer()
|
||||
s.BindHandler("/", func(r *ghttp.Request){
|
||||
r.Response.Writeln("来自于HTTPS的:哈喽世界!")
|
||||
})
|
||||
s.EnableHTTPS("./server.crt", "./server.key")
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
s := ghttp.GetServer()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Writeln("来自于HTTPS的:哈喽世界!")
|
||||
})
|
||||
s.EnableHTTPS("./server.crt", "./server.key")
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -1,18 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := ghttp.GetServer()
|
||||
s.EnableAdmin()
|
||||
s.BindHandler("/", func(r *ghttp.Request){
|
||||
r.Response.Writeln("您可以同时通过HTTP和HTTPS方式看到该内容!")
|
||||
})
|
||||
s.EnableHTTPS("./server.crt", "./server.key")
|
||||
s.SetHTTPSPort(8198, 8199)
|
||||
s.SetPort(8200, 8300)
|
||||
s.EnableAdmin()
|
||||
s.Run()
|
||||
}
|
||||
s := ghttp.GetServer()
|
||||
s.EnableAdmin()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Writeln("您可以同时通过HTTP和HTTPS方式看到该内容!")
|
||||
})
|
||||
s.EnableHTTPS("./server.crt", "./server.key")
|
||||
s.SetHTTPSPort(8198, 8199)
|
||||
s.SetPort(8200, 8300)
|
||||
s.EnableAdmin()
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -1,43 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
type User struct {}
|
||||
type User struct{}
|
||||
|
||||
func (u *User) ShowList(r *ghttp.Request) {
|
||||
r.Response.Write("list")
|
||||
r.Response.Write("list")
|
||||
}
|
||||
|
||||
func main() {
|
||||
s1 := g.Server(1)
|
||||
s2 := g.Server(2)
|
||||
s3 := g.Server(3)
|
||||
s4 := g.Server(4)
|
||||
s1 := g.Server(1)
|
||||
s2 := g.Server(2)
|
||||
s3 := g.Server(3)
|
||||
s4 := g.Server(4)
|
||||
|
||||
s1.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_DEFAULT)
|
||||
s2.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_FULLNAME)
|
||||
s3.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_ALLLOWER)
|
||||
s4.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_CAMEL)
|
||||
s1.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_DEFAULT)
|
||||
s2.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_FULLNAME)
|
||||
s3.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_ALLLOWER)
|
||||
s4.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_CAMEL)
|
||||
|
||||
s1.BindObject("/{.struct}/{.method}", new(User))
|
||||
s2.BindObject("/{.struct}/{.method}", new(User))
|
||||
s3.BindObject("/{.struct}/{.method}", new(User))
|
||||
s4.BindObject("/{.struct}/{.method}", new(User))
|
||||
s1.BindObject("/{.struct}/{.method}", new(User))
|
||||
s2.BindObject("/{.struct}/{.method}", new(User))
|
||||
s3.BindObject("/{.struct}/{.method}", new(User))
|
||||
s4.BindObject("/{.struct}/{.method}", new(User))
|
||||
|
||||
s1.SetPort(8100)
|
||||
s2.SetPort(8200)
|
||||
s3.SetPort(8300)
|
||||
s4.SetPort(8400)
|
||||
s1.SetPort(8100)
|
||||
s2.SetPort(8200)
|
||||
s3.SetPort(8300)
|
||||
s4.SetPort(8400)
|
||||
|
||||
s1.Start()
|
||||
s2.Start()
|
||||
s3.Start()
|
||||
s4.Start()
|
||||
s1.Start()
|
||||
s2.Start()
|
||||
s3.Start()
|
||||
s4.Start()
|
||||
|
||||
g.Wait()
|
||||
g.Wait()
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -1,19 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := &http.Server{
|
||||
Addr: ":8199",
|
||||
ReadTimeout: 2 * time.Second,
|
||||
WriteTimeout: 2 * time.Second,
|
||||
}
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("hi"))
|
||||
time.Sleep(3*time.Second)
|
||||
})
|
||||
s.ListenAndServe()
|
||||
}
|
||||
s := &http.Server{
|
||||
Addr: ":8199",
|
||||
ReadTimeout: 2 * time.Second,
|
||||
WriteTimeout: 2 * time.Second,
|
||||
}
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("hi"))
|
||||
time.Sleep(3 * time.Second)
|
||||
})
|
||||
s.ListenAndServe()
|
||||
}
|
||||
|
||||
@ -1,31 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
|
||||
}
|
||||
|
||||
func (c *User) Index(r *ghttp.Request) {
|
||||
r.Response.Write("Index")
|
||||
r.Response.Write("Index")
|
||||
}
|
||||
|
||||
// 不符合规范,不会被注册
|
||||
func (c *User) Test(r *ghttp.Request, value interface{}) {
|
||||
r.Response.Write("Test")
|
||||
r.Response.Write("Test")
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindObject("/user", new(User))
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
s := g.Server()
|
||||
s.BindObject("/user", new(User))
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,14 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := ghttp.GetServer()
|
||||
s.BindHandler("/", func(r *ghttp.Request){
|
||||
r.Response.Writeln("go frame!")
|
||||
})
|
||||
s.SetPort(8100, 8200, 8300)
|
||||
s.Run()
|
||||
}
|
||||
s := ghttp.GetServer()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Writeln("go frame!")
|
||||
})
|
||||
s.SetPort(8100, 8200, 8300)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -1,16 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := ghttp.GetServer()
|
||||
s.EnablePprof()
|
||||
s.BindHandler("/", func(r *ghttp.Request){
|
||||
r.Response.Writeln("哈喽世界!")
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
s := ghttp.GetServer()
|
||||
s.EnablePprof()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Writeln("哈喽世界!")
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
|
||||
@ -1,18 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/page", func(r *ghttp.Request){
|
||||
r.Response.Writeln(`<a href="/back">back</a>`)
|
||||
})
|
||||
s.BindHandler("/back", func(r *ghttp.Request){
|
||||
r.Response.RedirectBack()
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
s := g.Server()
|
||||
s.BindHandler("/page", func(r *ghttp.Request) {
|
||||
r.Response.Writeln(`<a href="/back">back</a>`)
|
||||
})
|
||||
s.BindHandler("/back", func(r *ghttp.Request) {
|
||||
r.Response.RedirectBack()
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -1,17 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
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()
|
||||
}
|
||||
s := g.Server()
|
||||
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,17 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/", func(r *ghttp.Request){
|
||||
r.Response.Writeln("哈罗!")
|
||||
})
|
||||
s.EnableHTTPS("/home/john/temp/server.crt", "/home/john/temp/server.key")
|
||||
s.EnableAdmin()
|
||||
s.SetPort(8200)
|
||||
s.Run()
|
||||
}
|
||||
s := g.Server()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Writeln("哈罗!")
|
||||
})
|
||||
s.EnableHTTPS("/home/john/temp/server.crt", "/home/john/temp/server.key")
|
||||
s.EnableAdmin()
|
||||
s.SetPort(8200)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -1,18 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := ghttp.GetServer()
|
||||
s.EnableAdmin()
|
||||
s.BindHandler("/", func(r *ghttp.Request){
|
||||
r.Response.Writeln("您可以同时通过HTTP和HTTPS方式看到该内容!")
|
||||
})
|
||||
s.EnableHTTPS("/home/john/temp/server.crt", "/home/john/temp/server.key")
|
||||
s.SetHTTPSPort(8198, 8199)
|
||||
s.SetPort(8200, 8300)
|
||||
s.EnableAdmin()
|
||||
s.Run()
|
||||
}
|
||||
s := ghttp.GetServer()
|
||||
s.EnableAdmin()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Writeln("您可以同时通过HTTP和HTTPS方式看到该内容!")
|
||||
})
|
||||
s.EnableHTTPS("/home/john/temp/server.crt", "/home/john/temp/server.key")
|
||||
s.SetHTTPSPort(8198, 8199)
|
||||
s.SetPort(8200, 8300)
|
||||
s.EnableAdmin()
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -1,19 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s1 := g.Server("s1")
|
||||
s1.EnableAdmin()
|
||||
s1.SetPort(8100, 8200)
|
||||
s1.Start()
|
||||
s1 := g.Server("s1")
|
||||
s1.EnableAdmin()
|
||||
s1.SetPort(8100, 8200)
|
||||
s1.Start()
|
||||
|
||||
s2 := g.Server("s2")
|
||||
s2.EnableAdmin()
|
||||
s2.SetPort(8300, 8400)
|
||||
s2.Start()
|
||||
s2 := g.Server("s2")
|
||||
s2.EnableAdmin()
|
||||
s2.SetPort(8300, 8400)
|
||||
s2.Start()
|
||||
|
||||
g.Wait()
|
||||
}
|
||||
g.Wait()
|
||||
}
|
||||
|
||||
@ -1,26 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/os/gproc"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/os/gproc"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/", func(r *ghttp.Request){
|
||||
r.Response.Writeln("哈喽!")
|
||||
})
|
||||
s.BindHandler("/pid", func(r *ghttp.Request){
|
||||
r.Response.Writeln(gproc.Pid())
|
||||
})
|
||||
s.BindHandler("/sleep", func(r *ghttp.Request){
|
||||
r.Response.Writeln(gproc.Pid())
|
||||
time.Sleep(10*time.Second)
|
||||
r.Response.Writeln(gproc.Pid())
|
||||
})
|
||||
s.EnableAdmin()
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
s := g.Server()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Writeln("哈喽!")
|
||||
})
|
||||
s.BindHandler("/pid", func(r *ghttp.Request) {
|
||||
r.Response.Writeln(gproc.Pid())
|
||||
})
|
||||
s.BindHandler("/sleep", func(r *ghttp.Request) {
|
||||
r.Response.Writeln(gproc.Pid())
|
||||
time.Sleep(10 * time.Second)
|
||||
r.Response.Writeln(gproc.Pid())
|
||||
})
|
||||
s.EnableAdmin()
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user