From 4aa9a521ade7db01d2a8a1420a2f7445faa13b1e Mon Sep 17 00:00:00 2001 From: miaolz123 Date: Wed, 5 Aug 2020 20:06:38 +0800 Subject: [PATCH] feat: upgrade dependencies --- .golangci.yml | 44 ++++++++++++++++++++++++++++++++++++++++ .gometalinter.json | 14 ------------- Makefile | 9 ++++++-- cache/cache.go | 2 +- cache/memory.go | 1 + cache/redis.go | 3 ++- db/db.go | 38 ++++++++++++++++++++-------------- go.mod | 4 ++-- go.sum | 18 +++++----------- iriser/example/main.go | 10 +++++---- iriser/iriser.go | 1 + json/json.go | 2 +- log/log.go | 9 ++++++++ math/logarithm.go | 2 +- math/math.go | 2 +- mongo/mongo.go | 2 +- mq/rabbitmq.go | 9 ++++---- schedule/example/main.go | 1 + schedule/schedule.go | 1 + sync/example/main.go | 12 +++++------ sync/redis.go | 3 ++- 21 files changed, 120 insertions(+), 67 deletions(-) create mode 100644 .golangci.yml delete mode 100644 .gometalinter.json diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..67627a6 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,44 @@ +# https://golangci-lint.run/usage/configuration/#config-file + +run: + timeout: 2m + + +linters: + disable-all: true + enable: + - bodyclose + - deadcode + - depguard + - dogsled + - errcheck + - gocritic + - gocyclo + - gofmt + - goimports + # - golint + - goprintffuncname + - gosimple + - govet + - ineffassign + - interfacer + - nakedret + - noctx + - nolintlint + - rowserrcheck + - scopelint + - staticcheck + - structcheck + - stylecheck + - typecheck + - unconvert + - unparam + - unused + - varcheck + - whitespace + + +linters-settings: + govet: + disable: + - printf diff --git a/.gometalinter.json b/.gometalinter.json deleted file mode 100644 index 4f45a0d..0000000 --- a/.gometalinter.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "Deadline": "2m", - "Enable": [ - "gofmt", - "goimports", - "gosimple", - "ineffassign", - "interfacer", - "nakedret", - "unconvert", - "unused" - ], - "Sort": ["path", "line", "linter", "severity"] -} diff --git a/Makefile b/Makefile index cf178be..cc1b1bf 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,9 @@ -.PHONY: lint +ALL: lint run +.PHONY: lint lint: - gometalinter ./... + golangci-lint run + +.PHONY: run +run: + go run example/main.go diff --git a/cache/cache.go b/cache/cache.go index 7f3e49c..1b5d408 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -7,7 +7,7 @@ import ( ) var ( - ErrNil = redis.Nil + ErrNil = redis.Nil // error not found DefaultCacher = NewMemoryCacher() ) diff --git a/cache/memory.go b/cache/memory.go index 13f7f7b..3853077 100644 --- a/cache/memory.go +++ b/cache/memory.go @@ -4,6 +4,7 @@ import ( "time" "github.com/gxxgle/go-utils/json" + "github.com/patrickmn/go-cache" ) diff --git a/cache/redis.go b/cache/redis.go index 7431d57..3a89c4c 100644 --- a/cache/redis.go +++ b/cache/redis.go @@ -3,9 +3,10 @@ package cache import ( "time" - "github.com/go-redis/redis/v7" "github.com/gxxgle/go-utils/json" "github.com/gxxgle/go-utils/log" + + "github.com/go-redis/redis/v7" ) // RedisConfig is config struct of redis. diff --git a/db/db.go b/db/db.go index c334de4..1fd6bdd 100644 --- a/db/db.go +++ b/db/db.go @@ -5,43 +5,48 @@ import ( "strings" "time" + "github.com/gxxgle/go-utils/env" + "github.com/gxxgle/go-utils/log" + "github.com/doug-martin/goqu/v9" _ "github.com/doug-martin/goqu/v9/dialect/mysql" _ "github.com/go-sql-driver/mysql" - "github.com/gxxgle/go-utils/env" - "github.com/gxxgle/go-utils/log" - "xorm.io/builder" "xorm.io/xorm" ) +type Driver = string + +const ( + MySQL Driver = "mysql" +) + // default config var ( DefaultRetries = 5 DefaullRetrySleep = time.Second DefaultNeedRetry = isDeadlock - Builder func() *builder.Builder - Goqu goqu.DialectWrapper +) + +var ( + Goqu goqu.DialectWrapper ) // Config for db type Config struct { - Driver string `json:"driver"` - URL string `json:"url"` + Driver Driver `json:"driver"` + URL string `json:"url"` // example: "root:PASSWORD@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&clientFoundRows=true&parseTime=true&loc=Asia%2FShanghai" PoolSize int `json:"pool_size"` Debug bool `json:"debug"` } func OpenDB(cfg *Config) (*xorm.Engine, error) { if cfg.Driver == "" { - cfg.Driver = builder.MYSQL + cfg.Driver = MySQL } switch cfg.Driver { - case builder.MYSQL: - Builder = builder.MySQL - Goqu = goqu.Dialect(builder.MYSQL) - // case builder.POSTGRES: - // Builder = builder.Postgres + case MySQL: + Goqu = goqu.Dialect(MySQL) default: return nil, fmt.Errorf("db driver: %s not support", cfg.Driver) } @@ -53,10 +58,13 @@ func OpenDB(cfg *Config) (*xorm.Engine, error) { db.DatabaseTZ = env.Local db.ShowSQL(cfg.Debug) - db.SetMaxOpenConns(cfg.PoolSize) - db.SetMaxIdleConns(cfg.PoolSize) db.SetConnMaxLifetime(time.Minute * 30) + if cfg.PoolSize > 0 { + db.SetMaxOpenConns(cfg.PoolSize) + db.SetMaxIdleConns(cfg.PoolSize) + } + if err := db.Ping(); err != nil { return nil, err } diff --git a/go.mod b/go.mod index 6c778d7..cbc12c3 100644 --- a/go.mod +++ b/go.mod @@ -20,7 +20,8 @@ require ( github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect github.com/kataras/iris/v12 v12.1.8 github.com/klauspost/compress v1.10.10 // indirect - github.com/kr/pretty v0.2.0 // indirect + github.com/kr/pretty v0.2.1 // indirect + github.com/kr/text v0.2.0 // indirect github.com/lib/pq v1.8.0 // indirect github.com/mattn/go-colorable v0.1.7 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect @@ -45,6 +46,5 @@ require ( golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899 // indirect golang.org/x/net v0.0.0-20200707034311-ab3426394381 gopkg.in/yaml.v2 v2.3.0 // indirect - xorm.io/builder v0.3.7 xorm.io/xorm v1.0.3 ) diff --git a/go.sum b/go.sum index fefd7ae..22177ed 100644 --- a/go.sum +++ b/go.sum @@ -30,10 +30,10 @@ github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/denisenkom/go-mssqldb v0.0.0-20200206145737-bbfc9a55622e h1:LzwWXEScfcTu7vUZNlDDWDARoSGEtvlDKK2BYHowNeE= github.com/denisenkom/go-mssqldb v0.0.0-20200206145737-bbfc9a55622e/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= github.com/denisenkom/go-mssqldb v0.0.0-20200428022330-06a60b6afbbc/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= github.com/dgraph-io/badger v1.6.0/go.mod h1:zwt7syl517jmP8s94KqSxTlM6IMsdhYy6psNgSztDR4= @@ -56,7 +56,6 @@ github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8 h1:DujepqpGd1hyOd7a github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q= github.com/go-check/check v0.0.0-20180628173108-788fd7840127 h1:0gkP6mzaMqkmpcJYCFOLkIBwI7xFExG03bbkOkCvUPI= github.com/go-check/check v0.0.0-20180628173108-788fd7840127/go.mod h1:9ES+weclKsC9YodN5RgxqK/VD9HM9JsCSh7rNhMZE98= -github.com/go-redis/redis/v7 v7.2.0 h1:CrCexy/jYWZjW0AyVoHlcJUeZN19VWlbepTh1Vq6dJs= github.com/go-redis/redis/v7 v7.2.0/go.mod h1:JDNMw23GTyLNC4GZu9njt15ctBQVn7xjRfnwdHj/Dcg= github.com/go-redis/redis/v7 v7.4.0 h1:7obg6wUoj05T0EpY0o8B59S9w5yeMWql7sw2kwNW1x4= github.com/go-redis/redis/v7 v7.4.0/go.mod h1:JDNMw23GTyLNC4GZu9njt15ctBQVn7xjRfnwdHj/Dcg= @@ -66,7 +65,6 @@ github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LB github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo= github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM= -github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY= github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= @@ -111,7 +109,6 @@ github.com/iris-contrib/pongo2 v0.0.1 h1:zGP7pW51oi5eQZMIlGA3I+FHY9/HOQWDB+572yi github.com/iris-contrib/pongo2 v0.0.1/go.mod h1:Ssh+00+3GAZqSQb30AvBRNxBx7rf0GqwkjqxNd0u65g= github.com/iris-contrib/schema v0.0.1 h1:10g/WnoRR+U+XXHWKBHeNy/+tZmM2kcAVGLOsz+yaDA= github.com/iris-contrib/schema v0.0.1/go.mod h1:urYA3uvUNG1TIIjOSCzHr9/LmbQo8LrOcOqfqxa4hXw= -github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68= github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= @@ -128,20 +125,19 @@ github.com/kataras/pio v0.0.2 h1:6NAi+uPJ/Zuid6mrAKlgpbI11/zK/lV4B2rxWaJN98Y= github.com/kataras/pio v0.0.2/go.mod h1:hAoW0t9UmXi4R5Oyq5Z4irTbaTsOemSrDGUtaTl7Dro= github.com/kataras/sitemap v0.0.5 h1:4HCONX5RLgVy6G4RkYOV3vKNcma9p236LdGOipJsaFE= github.com/kataras/sitemap v0.0.5/go.mod h1:KY2eugMKiPwsJgx7+U103YZehfvNGOXURubcGyk0Bz8= -github.com/klauspost/compress v1.9.7 h1:hYW1gP94JUmAhBtJ+LNz5My+gBobDxPR1iVuKug26aA= github.com/klauspost/compress v1.9.7/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= -github.com/klauspost/compress v1.10.7 h1:7rix8v8GpI3ZBb0nSozFRgbtXKv+hOe+qfEpZqybrAg= github.com/klauspost/compress v1.10.7/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= github.com/klauspost/compress v1.10.10 h1:a/y8CglcM7gLGYmlbP/stPE5sR3hbhFRUjCBfd/0B3I= github.com/klauspost/compress v1.10.10/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8= github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs= -github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.7.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lib/pq v1.8.0 h1:9xohqzkUwzR4Ga4ivdTcawVS89YSDVxXMa3xJX3cGzg= @@ -151,7 +147,6 @@ github.com/mattn/go-colorable v0.1.7 h1:bQGKb3vps/j0E9GfJQ03JyhRuxsvdAanXlT9BTw3 github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= -github.com/mattn/go-sqlite3 v1.11.0 h1:LDdKkqtYlom37fkvqs8rMPFKAMe8+SgjbwZ6ex1/A/Q= github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/go-sqlite3 v1.14.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/QA= github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus= @@ -183,7 +178,6 @@ github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1Cpa github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= -github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -250,7 +244,6 @@ github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 h1:BHyfKlQyqbsFN5p3Ifn github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82/go.mod h1:lgjkn3NuSvDfVJdfcVVdX+jpBxNmX4rDAzaS45IcYoM= github.com/yudai/pp v2.0.1+incompatible h1:Q4//iY4pNF6yPLZIigmvcl7k/bPgrcTPIFIcmawg5bI= github.com/yudai/pp v2.0.1+incompatible/go.mod h1:PuxR/8QJ7cyCkFp/aUDS+JY727OFEZkTdatxwunjIkc= -github.com/ziutek/mymysql v1.5.4 h1:GB0qdRGsTwQSBVYuVShFBKaXSnSnYYC2d9knnE1LHFs= github.com/ziutek/mymysql v1.5.4/go.mod h1:LMSpPZ6DbqWFxNCHW77HeMg9I646SAhApZ/wKdgO/C0= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= @@ -289,7 +282,6 @@ golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 h1:OjiUf46hAmXblsZdnoSXsEUSKU8r1UEzcL5RVZ4gO9Y= golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/iriser/example/main.go b/iriser/example/main.go index cdac3bb..54d434f 100644 --- a/iriser/example/main.go +++ b/iriser/example/main.go @@ -4,6 +4,8 @@ import ( "errors" "github.com/gxxgle/go-utils/iriser" + "github.com/gxxgle/go-utils/log" + "github.com/kataras/iris/v12" "github.com/kataras/iris/v12/middleware/recover" ) @@ -11,14 +13,14 @@ import ( type defaultHandler struct{} func (*defaultHandler) HelloPanic(ctx iris.Context) { - ctx.JSON(iris.Map{ + _, _ = ctx.JSON(iris.Map{ "name": "world", }) panic(errors.New("custom panic")) } func (*defaultHandler) Hello(ctx iris.Context) { - ctx.JSON(iris.Map{ + _, _ = ctx.JSON(iris.Map{ "name": "world", }) } @@ -27,9 +29,9 @@ func main() { app := iris.New() app.Logger().SetLevel("debug") app.Use(recover.New()) - app.Use(iriser.NewLogger()) + app.Use(iriser.NewLogger(nil)) iriser.Register(app.Party("/v1"), new(defaultHandler)) - app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed)) + log.LogIfError(app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))) } diff --git a/iriser/iriser.go b/iriser/iriser.go index ea021aa..0c82ab9 100644 --- a/iriser/iriser.go +++ b/iriser/iriser.go @@ -6,6 +6,7 @@ import ( "time" "github.com/gxxgle/go-utils/log" + "github.com/kataras/iris/v12" "github.com/kataras/iris/v12/core/router" "github.com/kataras/iris/v12/middleware/logger" diff --git a/json/json.go b/json/json.go index d561fad..63030ef 100644 --- a/json/json.go +++ b/json/json.go @@ -3,7 +3,7 @@ package json import ( stdjson "encoding/json" - "github.com/json-iterator/go" + jsoniter "github.com/json-iterator/go" "github.com/tidwall/gjson" ) diff --git a/log/log.go b/log/log.go index d7c895d..df245cb 100644 --- a/log/log.go +++ b/log/log.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/gxxgle/go-utils/path" + "github.com/sirupsen/logrus" ) @@ -83,6 +84,14 @@ func Debug() { logrus.SetLevel(logrus.DebugLevel) } +func LogIfError(err error, msg ...interface{}) { + if err == nil { + return + } + + L.WithError(err).Error(msg...) +} + func Close() { if logfile != nil { logfile.Close() diff --git a/math/logarithm.go b/math/logarithm.go index 5b7c203..04b042b 100644 --- a/math/logarithm.go +++ b/math/logarithm.go @@ -13,7 +13,7 @@ func Log(args ...float64) float64 { step = float64(1) ) - if len(args) <= 0 { + if len(args) == 0 { return 0 } diff --git a/math/math.go b/math/math.go index ab05c11..cbcb904 100644 --- a/math/math.go +++ b/math/math.go @@ -53,6 +53,6 @@ func FloatToInt(f float64, precision int32) int { func FloatFromInt(i int, precision int32) float64 { dMulti := decimal.NewFromFloat(math.Pow10(int(precision))) dI := decimal.NewFromFloat(float64(i)) - out, _ := dI.Div(dMulti).Round(int32(precision)).Float64() + out, _ := dI.Div(dMulti).Round(precision).Float64() return out } diff --git a/mongo/mongo.go b/mongo/mongo.go index 82410cf..148d2a2 100644 --- a/mongo/mongo.go +++ b/mongo/mongo.go @@ -66,7 +66,7 @@ func (s *Session) C(table interface{}) *mgo.Collection { // Insert inserts one or more documents in the respective collection func (s *Session) Insert(docs ...interface{}) error { - if len(docs) <= 0 { + if len(docs) == 0 { return nil } diff --git a/mq/rabbitmq.go b/mq/rabbitmq.go index f9cb617..ebc57ea 100644 --- a/mq/rabbitmq.go +++ b/mq/rabbitmq.go @@ -3,8 +3,9 @@ package mq import ( "sync" - "github.com/assembla/cony" "github.com/gxxgle/go-utils/log" + + "github.com/assembla/cony" "github.com/streadway/amqp" ) @@ -34,7 +35,7 @@ func NewRabbitMQ(url string) Client { cony.URL(url), cony.Backoff(cony.DefaultBackoff), ), - exit: make(chan bool, 0), + exit: make(chan bool), } out.run() @@ -81,7 +82,7 @@ func newRabbitmqPublisher(cli *rabbitmq, exchange string) *rabbitmqPublisher { cli: cli, puber: cony.NewPublisher(exchange, ""), exchange: exchange, - msgs: make(chan *Message, 0), + msgs: make(chan *Message), } out.run() @@ -153,7 +154,7 @@ func (s *rabbitmqSubscriber) Subscribe(handler func([]byte) error) { }).Error("go-utils mq rabbitmq subscriber handler message") continue } - msg.Ack(false) + log.LogIfError(msg.Ack(false)) case err := <-s.coner.Errors(): if err != nil { diff --git a/schedule/example/main.go b/schedule/example/main.go index 59a08ef..b1dde76 100644 --- a/schedule/example/main.go +++ b/schedule/example/main.go @@ -1,3 +1,4 @@ +// nolint package main import ( diff --git a/schedule/schedule.go b/schedule/schedule.go index 0ca8583..a73d616 100644 --- a/schedule/schedule.go +++ b/schedule/schedule.go @@ -6,6 +6,7 @@ import ( "time" "github.com/gxxgle/go-utils/env" + "github.com/robfig/cron/v3" ) diff --git a/sync/example/main.go b/sync/example/main.go index b467c33..26f759f 100644 --- a/sync/example/main.go +++ b/sync/example/main.go @@ -1,11 +1,11 @@ package main import ( - "log" sn "sync" "time" "github.com/gxxgle/go-utils/cache" + "github.com/gxxgle/go-utils/log" "github.com/gxxgle/go-utils/sync" ) @@ -14,11 +14,11 @@ var ( ) func init() { - sync.InitRedis(&cache.RedisConfig{ + log.LogIfError(sync.InitRedis(&cache.RedisConfig{ URL: "devhost:6379", Password: "KgqvdOdYV5", Retries: 10, - }) + })) } func main() { @@ -33,11 +33,11 @@ func main() { func print(i int) { begin := time.Now() mu := sync.NewMutex("LOCKER:TEST") - log.Println("lock getting index:", i, ", cost:", time.Since(begin).String()) + log.L.Info("lock getting index:", i, ", cost:", time.Since(begin).String()) mu.Lock() - log.Println("lock got index:", i, ", cost:", time.Since(begin).String()) + log.L.Info("lock got index:", i, ", cost:", time.Since(begin).String()) defer wg.Done() defer mu.Unlock() time.Sleep(time.Second * 5) - log.Println("lock un index:", i, ", cost:", time.Since(begin).String()) + log.L.Info("lock un index:", i, ", cost:", time.Since(begin).String()) } diff --git a/sync/redis.go b/sync/redis.go index 67acde4..591747a 100644 --- a/sync/redis.go +++ b/sync/redis.go @@ -1,9 +1,10 @@ package sync import ( - "github.com/bsm/redislock" "github.com/gxxgle/go-utils/cache" "github.com/gxxgle/go-utils/log" + + "github.com/bsm/redislock" ) type redisMutexer struct {