简单易用的 Golang 辅助库,让 Golang 开发更简单

2019-02-28 16:55:53 +08:00
 IIInsomnia

yiigo

简单易用的 Golang 辅助库,让 Golang 开发更简单

特点

获取

go get github.com/iiinsomnia/yiigo

使用

MySQL

// default db
yiigo.RegisterDB("default", yiigo.MySQL, "root:root@tcp(localhost:3306)/test")

yiigo.DB.Get(&User{}, "SELECT * FROM `user` WHERE `id` = ?", 1)

// other db
yiigo.RegisterDB("foo", yiigo.MySQL, "root:root@tcp(localhost:3306)/foo")

yiigo.UseDB("foo").Get(&User{}, "SELECT * FROM `user` WHERE `id` = ?", 1)

MongoDB

// default mongodb
yiigo.RegisterMongoDB("default", "mongodb://localhost:27017")

ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
yiigo.Mongo.Database("test").Collection("numbers").InsertOne(ctx, bson.M{"name": "pi", "value": 3.14159})

// other mongodb
yiigo.RegisterMongoDB("foo", "mongodb://localhost:27017")

ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
yiigo.UseMongo("foo").Database("test").Collection("numbers").InsertOne(ctx, bson.M{"name": "pi", "value": 3.14159})

Redis

// default redis
yiigo.RegisterRedis("default", "localhost:6379")

conn, err := yiigo.Redis.Get()

if err != nil {
	log.Fatal(err)
}

defer yiigo.Redis.Put(conn)

conn.Do("SET", "test_key", "hello world")

// other redis
yiigo.RegisterRedis("foo", "localhost:6379")

foo := yiigo.UseRedis("foo")
conn, err := foo.Get()

if err != nil {
	log.Fatal(err)
}

defer foo.Put(conn)

conn.Do("SET", "test_key", "hello world")

Config

// env.toml
//
// [app]
// env = "dev"
// debug = true
// port = 50001

yiigo.UseEnv("env.toml")

yiigo.Env.GetBool("app.debug", true)
yiigo.Env.GetInt("app.port", 12345)
yiigo.Env.GetString("app.env", "dev")

Logger

// default logger
yiigo.RegisterLogger("default", "app.log")
yiigo.Logger.Info("hello world")

// other logger
yiigo.RegisterLogger("foo", "foo.log")
yiigo.UseLogger("foo").Info("hello world")

文档

说明

Enjoy 😊

3262 次点击
所在节点    Go 编程语言
3 条回复
Rehtt
2019-03-01 09:20:56 +08:00
感谢分享
Raphael96
2019-03-01 10:47:37 +08:00
谢谢分享,star 收藏备用
meteor957
2019-03-01 12:11:13 +08:00
支持下

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/539694

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX