Yiigo超好用的go开发辅助库

Yiigo超好用的go开发辅助库

首页模拟经营Idle Port更新时间:2024-06-30

今天小编给大家推荐一个 Go 轻量级开发通用库,简单易上手,功能满足大部分日常开发。这个库的作用是封装了常用且必要的优秀库,让初学者可以很快上手Go用来开发项目,且轻量,只有很必要的功能封装

FeaturesRequirements

Go1.11 Installation

gogetgithub.com/shenghui0779/yiigoUsageConfig

[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"

yiigo.Env("app.env").String("dev") yiigo.Env("app.debug").Bool(true) yiigo.Env("apollo_test.name").String("foo")

⚠️注意!

如果配置了 apollo,则:

namespace 默认包含 application;

namespace 中的配置项优先从 apollo 读取,若不存在,则从 yiigo.toml 中读取;

若 namespace 不在 apollo 配置中,则其配置项从 yiigo.toml 中获取;

MySQL

//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"),

大家还看了
也许喜欢
更多游戏

Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved