1 Star 0 Fork 0

ivanloveluo / goproject

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
test.go 2.97 KB
一键复制 编辑 原始数据 按行查看 历史
package main
import (
"context"
"encoding/json"
"fmt"
"goproject/mongodb"
"goproject/otherstudy/goioc"
"reflect"
"sync"
"time"
)
func main() {
//mongodb.Init() //初始化数据库
//mm := mongodb.NewMgo("youj", "people")
//var model mongodb.Peo
//fmt.Println(mm.CollectionCount())
//插入
//pp:=&mongodb.Peo{
// Id:strings.ReplaceAll(uuid.NewV4().String(),"-",""),
// Name:"郭坚",
// Age:70,
//}
//mm.InserOne(pp)
//更新
/* model = mongodb.Peo{
Name: "李小力",
Id: "fce12c787b7a49c3805baf692e851552",
Age: 25,
}
mm.UpdateOne("Id", model)*/
//查询
//obj := mm.FindOneObj("age", 18, &model)
//peo := obj.(*mongodb.Peo)
//fmt.Println(peo.Name, peo.Age)
/*fmt.Println("---------------------")
sline := mm.FindManyObj("id", "fce12c787b7a49c3805baf692e851552", &model)
for _, d := range sline {
//通过断言实现类型转换
peo := d.(*mongodb.Peo)
fmt.Println(peo.Name, peo.Age)
}*/
//tes(&model)
//model = mongodb.Peo{
// Name: "lxw",
// Id:"a386",
// Age: 20,
//}
//toBson("Id", model)
/* e1 := GetInstance()
fmt.Println(&e1,e1.name)
e2 := GetInstance()
fmt.Println(&e2,e2.name)
*/
container := goioc.NewContainer()
container.SetSingleton("ex", example{name: "lxw"})
singleton := container.GetSingleton("ex")
e := singleton.(example)
e1 := singleton.(example)
fmt.Println(&e, e.name, &e1)
fmt.Println(e == e1)
fmt.Println(context.TODO())
message := make(chan int, 10)
done := make(chan bool)
defer close(message)
go func() {
ticker := time.NewTicker(1 * time.Second)
for _ = range ticker.C {
select {
case <-done:
fmt.Println("child process interrupt....")
return
default:
fmt.Println("send message:%d\n", <-message)
}
}
}()
for i := 0; i < 10; i++ {
message <- i
}
time.Sleep(5 * time.Second)
close(done)
time.Sleep(1 * time.Second)
fmt.Println("main preocess exit!")
}
type example struct {
name string
}
var (
once sync.Once
exam *example
)
func GetInstance() *example {
once.Do(
func() {
exam = new(example)
exam.name = "第一次赋值单例"
},
)
return exam
}
func toBson(key string, val interface{}) {
update := mongodb.GetUpdate(val)
fmt.Println(update)
filter := mongodb.GetFilter(key, val)
fmt.Println(filter)
}
func tes(obj interface{}) {
pe := mongodb.Peo{
Name: "lxw",
Age: 20,
}
fmt.Println(pe)
data, _ := json.Marshal(pe)
//fmt.Println(string(data))
ss := make([]interface{}, 0)
for i := 0; i < 1; i++ {
ptrType := reflect.TypeOf(obj) //获取call的指针的reflect.Type
trueType := ptrType.Elem() //获取type的真实类型
ptrValue := reflect.New(trueType) // 调用反射创建对象 返回对象的指针对应的reflect.Value
el := ptrValue.Interface()
json.Unmarshal(data, el)
ss = append(ss, el)
}
for _, s := range ss {
//golang将interface{}转换为struct 这里需要用到interface assertion
per, ok := s.(*mongodb.Peo) //通过断言实现类型转换
fmt.Println(ok)
fmt.Println(per.Name, per.Age)
}
}
1
https://gitee.com/ivanloveluo/goproject.git
git@gitee.com:ivanloveluo/goproject.git
ivanloveluo
goproject
goproject
master

搜索帮助