今天小編給大家推薦一個 Go 輕量級開發通用庫,簡單易上手,功能滿足大部分日常開發
Features支援 MySQL支援 PostgreSQL支援 MongoDB支援 Redis支援 Apollo郵件使用 gomail配置使用 tomlSQL使用 sqlxORM使用 gorm日誌使用 zap包含一些實用的幫助方法,如:http、cypto、date、IP 等RequirementsGo1.11+
Installation
go get github.com/shenghui0779/yiigo
UsageConfigyiigo.toml
[app]env = "dev" # dev | beta | proddebug = 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=localhost port=5432 user=root password=secret dbname=test connect_timeout=10 sslmode=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 = ""# apollo namespace[apollo_test]name = "yiigo"
usageyiigo.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 中獲取;
// default dbyiigo.DB().Get(&User{}, "SELECT * FROM `user` WHERE `id` = ?", 1)yiigo.Orm().First(&User{}, 1)// other dbyiigo.DB("foo").Get(&User{}, "SELECT * FROM `user` WHERE `id` = ?", 1)yiigo.Orm("foo").First(&User{}, 1)
MongoDB// default mongodbctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)defer cancel()yiigo.Mongo().Database("test").Collection("numbers").InsertOne(ctx, bson.M{"name": "pi", "value": 3.14159})// other mongodbctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)defer cancel()yiigo.Mongo("foo").Database("test").Collection("numbers").InsertOne(ctx, bson.M{"name": "pi", "value": 3.14159})
Redis// default redisconn, err := yiigo.Redis().Get()if err != nil { log.Fatal(err)}defer yiigo.Redis().Put(conn)conn.Do("SET", "test_key", "hello world")// other redisconn, err := yiigo.Redis("foo").Get()if err != nil { log.Fatal(err)}defer yiigo.Redis("foo").Put(conn)conn.Do("SET", "test_key", "hello world")
HTTPclient, err := yiigo.NewHTTPClient( yiigo.WithHTTPMaxIdleConnsPerHost(1000), yiigo.WithHTTPMaxConnsPerHost(1000), yiigo.WithHTTPDefaultTimeout(time.Second*10),)if err != nil { log.Fatal(err)}b, err := client.Get("url...", yiigo.WithRequestTimeout(5*time.Second))if err != nil { log.Fatal(err)}fmt.Println(string(b))
Logger
// default loggeryiigo.Logger().Info("hello world")// other loggeryiigo.Logger("foo").Info("hello world")