今天小编给大家推荐一个 Go 轻量级开发通用库,简单易上手,功能满足大部分日常开发。这个库的作用是封装了常用且必要的优秀库,让初学者可以很快上手Go用来开发项目,且轻量,只有很必要的功能封装
Features- 支持 MySQL
- 支持 PostgreSQL
- 支持 MongoDB
- 支持 Redis
- 支持 Apollo
- 邮件使用 gomail
- 配置使用 toml
- SQL使用 sqlx
- ORM使用 gorm
- 日志使用 zap
- 包含一些实用的帮助方法,如:http、cypto、date、IP 等
Go1.11
Installation
gogetgithub.com/shenghui0779/yiigo
UsageConfig
- yiigo.toml
[app]
env="dev"#dev|beta|prod
debug=true
[apollo]
app_id="test"
cluster="default"
address="127.0.0.1:8080"
namespace=["apollo_test"]
cache_dir="./"
accesskey_secret=""
insecure_skip_verify=true
[db]
[db.default]
driver="mysql"
dsn="username:password@tcp(localhost:3306)/dbname?timeout=10s&charset=utf8mb4&collation=utf8mb4_general_ci&parseTime=True&loc=Local"
#dsn="host=localhostport=5432user=rootpassword=secretdbname=testconnect_timeout=10sslmode=disable"#pgsql
max_open_conns=20
max_idle_conns=10
conn_max_lifetime=60#秒
[mongo]
[mongo.default]
dsn="mongodb://username:password@localhost:27017"
connect_timeout=10#秒
pool_size=10
max_conn_idle_time=60#秒
mode="primary"#primary|primary_preferred|secondary|secondary_preferred|nearest
[redis]
[redis.default]
address="127.0.0.1:6379"
password=""
database=0
connect_timeout=10#秒
read_timeout=10#秒
write_timeout=10#秒
pool_size=10
pool_limit=20
idle_timeout=60#秒
wait_timeout=10#秒
prefill_parallelism=0#预填充连接数
[log]
[log.default]
path="app.log"
max_size=500
max_age=0
max_backups=0
compress=true
[email]
[email.default]
host="smtp.exmail.qq.com"
port=25
username=""
password=""
#apollonamespace
[apollo_test]
name="yiigo"
- usage
yiigo.Env("app.env").String("dev")
yiigo.Env("app.debug").Bool(true)
yiigo.Env("apollo_test.name").String("foo")
MySQL⚠️注意!
如果配置了 apollo,则:
namespace 默认包含 application;
namespace 中的配置项优先从 apollo 读取,若不存在,则从 yiigo.toml 中读取;
若 namespace 不在 apollo 配置中,则其配置项从 yiigo.toml 中获取;
//defaultdb
yiigo.DB().Get(&User{},"SELECT*FROM`user`WHERE`id`=?",1)
yiigo.Orm().First(&User{},1)
//otherdb
yiigo.DB("foo").Get(&User{},"SELECT*FROM`user`WHERE`id`=?",1)
yiigo.Orm("foo").First(&User{},1)
MongoDB
//defaultmongodb
ctx,cancel:=context.WithTimeout(context.Background(),5*time.Second)
defercancel()
yiigo.Mongo().Database("test").Collection("numbers").InsertOne(ctx,bson.M{"name":"pi","value":3.14159})
//othermongodb
ctx,cancel:=context.WithTimeout(context.Background(),5*time.Second)
defercancel()
yiigo.Mongo("foo").Database("test").Collection("numbers").InsertOne(ctx,bson.M{"name":"pi","value":3.14159})
Redis
//defaultredis
conn,err:=yiigo.Redis().Get()
iferr!=nil{
log.Fatal(err)
}
deferyiigo.Redis().Put(conn)
conn.Do("SET","test_key","helloworld")
//otherredis
conn,err:=yiigo.Redis("foo").Get()
iferr!=nil{
log.Fatal(err)
}
deferyiigo.Redis("foo").Put(conn)
conn.Do("SET","test_key","helloworld")
HTTP
client,err:=yiigo.NewHTTPClient(
yiigo.WithHTTPMaxIdleConnsPerHost(1000),
yiigo.WithHTTPMaxConnsPerHost(1000),
yiigo.WithHTTPDefaultTimeout(time.Second*10),
)
iferr!=nil{
log.Fatal(err)
}
b,err:=client.Get("url...",yiigo.WithRequestTimeout(5*time.Second))
iferr!=nil{
log.Fatal(err)
}
fmt.Println(string(b))
Logger
//defaultlogger
yiigo.Logger().Info("helloworld")
//otherlogger
yiigo.Logger("foo").Info("helloworld")
,