From d771ed92092d0a2b02a2b2e21b5daed75249e3d6 Mon Sep 17 00:00:00 2001 From: John Date: Wed, 19 Jun 2019 23:28:37 +0800 Subject: [PATCH] improve codes --- g/crypto/gdes/gdes.go | 10 +++--- g/crypto/gdes/gdes_test.go | 8 ++++- g/crypto/gmd5/gmd5.go | 41 ++++++++++++---------- g/crypto/gsha1/gsha1.go | 30 ++++++++++------- g/encoding/gcompress/gcompress.go | 56 ++++++++++++++++++++----------- g/net/gtcp/gtcp_conn.go | 52 ++++++++++++++-------------- g/net/gtcp/gtcp_conn_pkg.go | 24 +++++++++---- g/net/gtcp/gtcp_pool.go | 24 +++++++++---- g/net/gtcp/gtcp_pool_pkg.go | 23 +++++++++---- g/os/gcfg/gcfg.go | 16 +++++---- g/os/gcfg/gcfg_z_unit_test.go | 35 ++++++++++++------- 11 files changed, 198 insertions(+), 121 deletions(-) diff --git a/g/crypto/gdes/gdes.go b/g/crypto/gdes/gdes.go index 8f945db46..7316d28d7 100644 --- a/g/crypto/gdes/gdes.go +++ b/g/crypto/gdes/gdes.go @@ -3,8 +3,6 @@ // This Source Code Form is subject to the terms of the MIT License. // If a copy of the MIT was not distributed with this file, // You can obtain one at https://github.com/gogf/gf. -// -// @author wenzi1 // Package gdes provides useful API for DES encryption/decryption algorithms. package gdes @@ -75,7 +73,7 @@ func TripleDesECBEncrypt(key []byte, clearText []byte, padding int) ([]byte, err return nil, err } - newKey := make([]byte, 0) + var newKey []byte if len(key) == 16 { newKey = append([]byte{}, key...) newKey = append(newKey, key[:8]...) @@ -103,7 +101,7 @@ func TripleDesECBDecrypt(key []byte, cipherText []byte, padding int) ([]byte, er return nil, errors.New("key length error") } - newKey := make([]byte, 0) + var newKey []byte if len(key) == 16 { newKey = append([]byte{}, key...) newKey = append(newKey, key[:8]...) @@ -182,7 +180,7 @@ func TripleDesCBCEncrypt(key []byte, clearText []byte, iv []byte, padding int) ( return nil, errors.New("key length invalid") } - newKey := make([]byte, 0) + var newKey []byte if len(key) == 16 { newKey = append([]byte{}, key...) newKey = append(newKey, key[:8]...) @@ -217,7 +215,7 @@ func TripleDesCBCDecrypt(key []byte, cipherText []byte, iv []byte, padding int) return nil, errors.New("key length invalid") } - newKey := make([]byte, 0) + var newKey []byte if len(key) == 16 { newKey = append([]byte{}, key...) newKey = append(newKey, key[:8]...) diff --git a/g/crypto/gdes/gdes_test.go b/g/crypto/gdes/gdes_test.go index 4236352a6..b1a0e86d2 100644 --- a/g/crypto/gdes/gdes_test.go +++ b/g/crypto/gdes/gdes_test.go @@ -1,3 +1,9 @@ +// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package gdes_test import ( @@ -139,7 +145,7 @@ func TestDesCBC(t *testing.T) { gtest.AssertEQ(errEncrypt, nil) // the iv is err errEncrypt, err = gdes.DesCBCEncrypt(key, text, errIv, padding) - //gtest.AssertNE(err,nil) + gtest.AssertNE(err, nil) gtest.AssertEQ(errEncrypt, nil) // the padding is err errEncrypt, err = gdes.DesCBCEncrypt(key, text, iv, errPadding) diff --git a/g/crypto/gmd5/gmd5.go b/g/crypto/gmd5/gmd5.go index 98e389f24..a4829e97a 100644 --- a/g/crypto/gmd5/gmd5.go +++ b/g/crypto/gmd5/gmd5.go @@ -9,38 +9,45 @@ package gmd5 import ( "crypto/md5" + "errors" "fmt" - "github.com/gogf/gf/g/util/gconv" "io" "os" + + "github.com/gogf/gf/g/util/gconv" ) // Encrypt encrypts any type of variable using MD5 algorithms. // It uses gconv package to convert to its bytes type. -func Encrypt(v interface{}) string { +func Encrypt(v interface{}) (encrypt string, err error) { h := md5.New() - h.Write([]byte(gconv.Bytes(v))) - return fmt.Sprintf("%x", h.Sum(nil)) + if _, err = h.Write([]byte(gconv.Bytes(v))); err != nil { + return "", err + } + return fmt.Sprintf("%x", h.Sum(nil)), nil } +// EncryptString is alias of Encrypt. // Deprecated. -func EncryptString(v string) string { - h := md5.New() - h.Write([]byte(v)) - return fmt.Sprintf("%x", h.Sum(nil)) +func EncryptString(v string) (encrypt string, err error) { + return Encrypt(v) } // EncryptFile encrypts file content of using MD5 algorithms. -func EncryptFile(path string) string { - f, e := os.Open(path) - if e != nil { - return "" +func EncryptFile(path string) (encrypt string, err error) { + f, err := os.Open(path) + if err != nil { + return "", err } - defer f.Close() + defer func() { + if e := f.Close(); e != nil { + err = errors.New(err.Error() + "; " + e.Error()) + } + }() h := md5.New() - _, e = io.Copy(h, f) - if e != nil { - return "" + _, err = io.Copy(h, f) + if err != nil { + return "", err } - return fmt.Sprintf("%x", h.Sum(nil)) + return fmt.Sprintf("%x", h.Sum(nil)), nil } diff --git a/g/crypto/gsha1/gsha1.go b/g/crypto/gsha1/gsha1.go index 77a342aa6..b296bdb74 100644 --- a/g/crypto/gsha1/gsha1.go +++ b/g/crypto/gsha1/gsha1.go @@ -10,9 +10,11 @@ package gsha1 import ( "crypto/sha1" "encoding/hex" - "github.com/gogf/gf/g/util/gconv" + "errors" "io" "os" + + "github.com/gogf/gf/g/util/gconv" ) // Encrypt encrypts any type of variable using SHA1 algorithms. @@ -22,23 +24,27 @@ func Encrypt(v interface{}) string { return hex.EncodeToString(r[:]) } +// EncryptString is alias of Encrypt. // Deprecated. func EncryptString(s string) string { - r := sha1.Sum([]byte(s)) - return hex.EncodeToString(r[:]) + return Encrypt(s) } // EncryptFile encrypts file content of using SHA1 algorithms. -func EncryptFile(path string) string { - f, e := os.Open(path) - if e != nil { - return "" +func EncryptFile(path string) (encrypt string, err error) { + f, err := os.Open(path) + if err != nil { + return "", err } - defer f.Close() + defer func() { + if e := f.Close(); e != nil { + err = errors.New(err.Error() + "; " + e.Error()) + } + }() h := sha1.New() - _, e = io.Copy(h, f) - if e != nil { - return "" + _, err = io.Copy(h, f) + if err != nil { + return "", err } - return hex.EncodeToString(h.Sum(nil)) + return hex.EncodeToString(h.Sum(nil)), nil } diff --git a/g/encoding/gcompress/gcompress.go b/g/encoding/gcompress/gcompress.go index bff744d5e..8447e4f24 100644 --- a/g/encoding/gcompress/gcompress.go +++ b/g/encoding/gcompress/gcompress.go @@ -15,53 +15,69 @@ import ( ) // Zlib compresses with zlib algorithm. -func Zlib(data []byte) []byte { +func Zlib(data []byte) ([]byte, error) { if data == nil || len(data) < 13 { - return data + return data, nil } var in bytes.Buffer + var err error w := zlib.NewWriter(&in) - _, _ = w.Write(data) - _ = w.Close() - return in.Bytes() + if _, err = w.Write(data); err != nil { + return nil, err + } + if err = w.Close(); err != nil { + return in.Bytes(), err + } + return in.Bytes(), nil } // UnZlib decompresses with zlib algorithm. -func UnZlib(data []byte) []byte { +func UnZlib(data []byte) ([]byte, error) { if data == nil || len(data) < 13 { - return data + return data, nil } + b := bytes.NewReader(data) var out bytes.Buffer + var err error r, err := zlib.NewReader(b) if err != nil { - return nil + return nil, err } - _, _ = io.Copy(&out, r) - return out.Bytes() + if _, err = io.Copy(&out, r); err != nil { + return nil, err + } + return out.Bytes(), nil } // Gzip compresses with gzip algorithm. -func Gzip(data []byte) []byte { +func Gzip(data []byte) ([]byte, error) { var buf bytes.Buffer + var err error zip := gzip.NewWriter(&buf) - _, err := zip.Write(data) + _, err = zip.Write(data) if err != nil { - return nil + return nil, err } - _ = zip.Close() - return buf.Bytes() + if err = zip.Close(); err != nil { + return nil, err + } + return buf.Bytes(), nil } // UnGzip decompresses with gzip algorithm. -func UnGzip(data []byte) []byte { +func UnGzip(data []byte) ([]byte, error) { var buf bytes.Buffer content := bytes.NewReader(data) zipData, err := gzip.NewReader(content) if err != nil { - return nil + return nil, err } - _, _ = io.Copy(&buf, zipData) - _ = zipData.Close() - return buf.Bytes() + if _, err = io.Copy(&buf, zipData); err != nil { + return nil, err + } + if err = zipData.Close(); err != nil { + return buf.Bytes(), err + } + return buf.Bytes(), nil } diff --git a/g/net/gtcp/gtcp_conn.go b/g/net/gtcp/gtcp_conn.go index b18526d4c..f1ace01e9 100644 --- a/g/net/gtcp/gtcp_conn.go +++ b/g/net/gtcp/gtcp_conn.go @@ -10,6 +10,7 @@ import ( "bufio" "bytes" "crypto/tls" + "errors" "io" "net" "time" @@ -17,7 +18,7 @@ import ( // 封装的链接对象 type Conn struct { - conn net.Conn // 底层tcp对象 + net.Conn // 底层tcp对象 reader *bufio.Reader // 当前链接的缓冲读取对象 buffer []byte // 读取缓冲区(用于数据读取时的缓冲区处理) recvDeadline time.Time // 读取超时时间 @@ -60,7 +61,7 @@ func NewConnKeyCrt(addr, crtFile, keyFile string) (*Conn, error) { // 将net.Conn接口对象转换为*gtcp.Conn对象 func NewConnByNetConn(conn net.Conn) *Conn { return &Conn{ - conn: conn, + Conn: conn, reader: bufio.NewReader(conn), recvDeadline: time.Time{}, sendDeadline: time.Time{}, @@ -68,15 +69,10 @@ func NewConnByNetConn(conn net.Conn) *Conn { } } -// 关闭连接 -func (c *Conn) Close() error { - return c.conn.Close() -} - // 发送数据 func (c *Conn) Send(data []byte, retry ...Retry) error { for { - if _, err := c.conn.Write(data); err != nil { + if _, err := c.Write(data); err != nil { // 链接已关闭 if err == io.EOF { return err @@ -124,7 +120,7 @@ func (c *Conn) Recv(length int, retry ...Retry) ([]byte, error) { // 仅对读取全部缓冲区数据操作有效 if length < 0 && index > 0 { bufferWait = true - if err = c.conn.SetReadDeadline(time.Now().Add(c.recvBufferWait)); err != nil { + if err = c.SetReadDeadline(time.Now().Add(c.recvBufferWait)); err != nil { return nil, err } } @@ -155,7 +151,7 @@ func (c *Conn) Recv(length int, retry ...Retry) ([]byte, error) { } // 判断数据是否全部读取完毕(由于超时机制的存在,获取的数据完整性不可靠) if bufferWait && isTimeout(err) { - if err = c.conn.SetReadDeadline(c.recvDeadline); err != nil { + if err = c.SetReadDeadline(c.recvDeadline); err != nil { return nil, err } err = nil @@ -207,21 +203,31 @@ func (c *Conn) RecvLine(retry ...Retry) ([]byte, error) { } // 带超时时间的数据获取 -func (c *Conn) RecvWithTimeout(length int, timeout time.Duration, retry ...Retry) ([]byte, error) { +func (c *Conn) RecvWithTimeout(length int, timeout time.Duration, retry ...Retry) (data []byte, err error) { if err := c.SetRecvDeadline(time.Now().Add(timeout)); err != nil { return nil, err } - defer c.SetRecvDeadline(time.Time{}) - return c.Recv(length, retry...) + defer func() { + if e := c.SetRecvDeadline(time.Time{}); e != nil { + err = errors.New(err.Error() + "; " + e.Error()) + } + }() + data, err = c.Recv(length, retry...) + return } // 带超时时间的数据发送 -func (c *Conn) SendWithTimeout(data []byte, timeout time.Duration, retry ...Retry) error { +func (c *Conn) SendWithTimeout(data []byte, timeout time.Duration, retry ...Retry) (err error) { if err := c.SetSendDeadline(time.Now().Add(timeout)); err != nil { return err } - defer c.SetSendDeadline(time.Time{}) - return c.Send(data, retry...) + defer func() { + if e := c.SetSendDeadline(time.Time{}); e != nil { + err = errors.New(err.Error() + "; " + e.Error()) + } + }() + err = c.Send(data, retry...) + return } // 发送数据并等待接收返回数据 @@ -243,7 +249,7 @@ func (c *Conn) SendRecvWithTimeout(data []byte, receive int, timeout time.Durati } func (c *Conn) SetDeadline(t time.Time) error { - err := c.conn.SetDeadline(t) + err := c.Conn.SetDeadline(t) if err == nil { c.recvDeadline = t c.sendDeadline = t @@ -252,7 +258,7 @@ func (c *Conn) SetDeadline(t time.Time) error { } func (c *Conn) SetRecvDeadline(t time.Time) error { - err := c.conn.SetReadDeadline(t) + err := c.SetReadDeadline(t) if err == nil { c.recvDeadline = t } @@ -260,7 +266,7 @@ func (c *Conn) SetRecvDeadline(t time.Time) error { } func (c *Conn) SetSendDeadline(t time.Time) error { - err := c.conn.SetWriteDeadline(t) + err := c.SetWriteDeadline(t) if err == nil { c.sendDeadline = t } @@ -272,11 +278,3 @@ func (c *Conn) SetSendDeadline(t time.Time) error { func (c *Conn) SetRecvBufferWait(bufferWaitDuration time.Duration) { c.recvBufferWait = bufferWaitDuration } - -func (c *Conn) LocalAddr() net.Addr { - return c.conn.LocalAddr() -} - -func (c *Conn) RemoteAddr() net.Addr { - return c.conn.RemoteAddr() -} diff --git a/g/net/gtcp/gtcp_conn_pkg.go b/g/net/gtcp/gtcp_conn_pkg.go index 8a39f76ce..0bc114a50 100644 --- a/g/net/gtcp/gtcp_conn_pkg.go +++ b/g/net/gtcp/gtcp_conn_pkg.go @@ -55,7 +55,7 @@ func (c *Conn) SendPkg(data []byte, option ...PkgOption) error { } length := len(data) if length > pkgOption.MaxSize { - return errors.New(fmt.Sprintf(`data size %d exceeds max pkg size %d`, length, gPKG_MAX_DATA_SIZE)) + return fmt.Errorf(`data size %d exceeds max pkg size %d`, length, gPKG_MAX_DATA_SIZE) } buffer := make([]byte, gPKG_HEADER_SIZE+1+len(data)) binary.BigEndian.PutUint32(buffer[0:], uint32(length)) @@ -68,12 +68,17 @@ func (c *Conn) SendPkg(data []byte, option ...PkgOption) error { } // 简单协议: 带超时时间的数据发送 -func (c *Conn) SendPkgWithTimeout(data []byte, timeout time.Duration, option ...PkgOption) error { +func (c *Conn) SendPkgWithTimeout(data []byte, timeout time.Duration, option ...PkgOption) (err error) { if err := c.SetSendDeadline(time.Now().Add(timeout)); err != nil { return err } - defer c.SetSendDeadline(time.Time{}) - return c.SendPkg(data, option...) + defer func() { + if e := c.SetSendDeadline(time.Time{}); e != nil { + err = errors.New(err.Error() + "; " + e.Error()) + } + }() + err = c.SendPkg(data, option...) + return } // 简单协议: 发送数据并等待接收返回数据 @@ -138,10 +143,15 @@ func (c *Conn) RecvPkg(option ...PkgOption) (result []byte, err error) { } // 简单协议: 带超时时间的消息包获取 -func (c *Conn) RecvPkgWithTimeout(timeout time.Duration, option ...PkgOption) ([]byte, error) { +func (c *Conn) RecvPkgWithTimeout(timeout time.Duration, option ...PkgOption) (data []byte, err error) { if err := c.SetRecvDeadline(time.Now().Add(timeout)); err != nil { return nil, err } - defer c.SetRecvDeadline(time.Time{}) - return c.RecvPkg(option...) + defer func() { + if e := c.SetRecvDeadline(time.Time{}); e != nil { + err = errors.New(err.Error() + "; " + e.Error()) + } + }() + data, err = c.RecvPkg(option...) + return } diff --git a/g/net/gtcp/gtcp_pool.go b/g/net/gtcp/gtcp_pool.go index cf8b4f00e..ee65f0f61 100644 --- a/g/net/gtcp/gtcp_pool.go +++ b/g/net/gtcp/gtcp_pool.go @@ -7,9 +7,11 @@ package gtcp import ( + "errors" + "time" + "github.com/gogf/gf/g/container/gmap" "github.com/gogf/gf/g/container/gpool" - "time" ) // 链接池链接对象 @@ -118,17 +120,27 @@ func (c *PoolConn) RecvWithTimeout(length int, timeout time.Duration, retry ...R if err := c.SetRecvDeadline(time.Now().Add(timeout)); err != nil { return nil, err } - defer c.SetRecvDeadline(time.Time{}) - return c.Recv(length, retry...) + defer func() { + if e := c.SetRecvDeadline(time.Time{}); e != nil { + err = errors.New(err.Error() + "; " + e.Error()) + } + }() + data, err = c.Recv(length, retry...) + return } // (方法覆盖)带超时时间的数据发送 -func (c *PoolConn) SendWithTimeout(data []byte, timeout time.Duration, retry ...Retry) error { +func (c *PoolConn) SendWithTimeout(data []byte, timeout time.Duration, retry ...Retry) (err error) { if err := c.SetSendDeadline(time.Now().Add(timeout)); err != nil { return err } - defer c.SetSendDeadline(time.Time{}) - return c.Send(data, retry...) + defer func() { + if e := c.SetSendDeadline(time.Time{}); e != nil { + err = errors.New(err.Error() + "; " + e.Error()) + } + }() + err = c.Send(data, retry...) + return } // (方法覆盖)发送数据并等待接收返回数据 diff --git a/g/net/gtcp/gtcp_pool_pkg.go b/g/net/gtcp/gtcp_pool_pkg.go index f487ce742..6b71eea0d 100644 --- a/g/net/gtcp/gtcp_pool_pkg.go +++ b/g/net/gtcp/gtcp_pool_pkg.go @@ -7,6 +7,7 @@ package gtcp import ( + "errors" "time" ) @@ -40,21 +41,31 @@ func (c *PoolConn) RecvPkg(option ...PkgOption) ([]byte, error) { } // 简单协议: (方法覆盖)带超时时间的数据获取 -func (c *PoolConn) RecvPkgWithTimeout(timeout time.Duration, option ...PkgOption) ([]byte, error) { +func (c *PoolConn) RecvPkgWithTimeout(timeout time.Duration, option ...PkgOption) (data []byte, err error) { if err := c.SetRecvDeadline(time.Now().Add(timeout)); err != nil { return nil, err } - defer c.SetRecvDeadline(time.Time{}) - return c.RecvPkg(option...) + defer func() { + if e := c.SetRecvDeadline(time.Time{}); e != nil { + err = errors.New(err.Error() + "; " + e.Error()) + } + }() + data, err = c.RecvPkg(option...) + return } // 简单协议: (方法覆盖)带超时时间的数据发送 -func (c *PoolConn) SendPkgWithTimeout(data []byte, timeout time.Duration, option ...PkgOption) error { +func (c *PoolConn) SendPkgWithTimeout(data []byte, timeout time.Duration, option ...PkgOption) (err error) { if err := c.SetSendDeadline(time.Now().Add(timeout)); err != nil { return err } - defer c.SetSendDeadline(time.Time{}) - return c.SendPkg(data, option...) + defer func() { + if e := c.SetSendDeadline(time.Time{}); e != nil { + err = errors.New(err.Error() + "; " + e.Error()) + } + }() + err = c.SendPkg(data, option...) + return } // 简单协议: (方法覆盖)发送数据并等待接收返回数据 diff --git a/g/os/gcfg/gcfg.go b/g/os/gcfg/gcfg.go index b8edb32da..1badfa033 100644 --- a/g/os/gcfg/gcfg.go +++ b/g/os/gcfg/gcfg.go @@ -11,6 +11,8 @@ import ( "bytes" "errors" "fmt" + "time" + "github.com/gogf/gf/g/container/garray" "github.com/gogf/gf/g/container/gmap" "github.com/gogf/gf/g/container/gtype" @@ -22,11 +24,10 @@ import ( "github.com/gogf/gf/g/os/glog" "github.com/gogf/gf/g/os/gspath" "github.com/gogf/gf/g/os/gtime" - "time" ) const ( - // Default configuration file name. + // DEFAULT_CONFIG_FILE is the default configuration file name. DEFAULT_CONFIG_FILE = "config.toml" ) @@ -144,7 +145,7 @@ func (c *Config) SetPath(path string) error { } // Should be a directory. if !gfile.IsDir(realPath) { - err := errors.New(fmt.Sprintf(`[gcfg] SetPath failed: path "%s" should be directory type`, path)) + err := fmt.Errorf(`[gcfg] SetPath failed: path "%s" should be directory type`, path) if errorPrint() { glog.Error(err) } @@ -205,7 +206,7 @@ func (c *Config) AddPath(path string) error { return err } if !gfile.IsDir(realPath) { - err := errors.New(fmt.Sprintf(`[gcfg] AddPath failed: path "%s" should be directory type`, path)) + err := fmt.Errorf(`[gcfg] AddPath failed: path "%s" should be directory type`, path) if errorPrint() { glog.Error(err) } @@ -220,8 +221,8 @@ func (c *Config) AddPath(path string) error { return nil } +// GetFilePath is alias of FilePath. // Deprecated. -// Alias of FilePath. func (c *Config) GetFilePath(file ...string) (path string) { return c.FilePath(file...) } @@ -281,9 +282,12 @@ func (c *Config) getJson(file ...string) *gjson.Json { // Add monitor for this configuration file, // any changes of this file will refresh its cache in Config object. if filePath != "" { - _, _ = gfsnotify.Add(filePath, func(event *gfsnotify.Event) { + _, err = gfsnotify.Add(filePath, func(event *gfsnotify.Event) { c.jsons.Remove(name) }) + if err != nil && errorPrint() { + glog.Error(err) + } } return j } else { diff --git a/g/os/gcfg/gcfg_z_unit_test.go b/g/os/gcfg/gcfg_z_unit_test.go index caadec849..e930db2dc 100644 --- a/g/os/gcfg/gcfg_z_unit_test.go +++ b/g/os/gcfg/gcfg_z_unit_test.go @@ -9,14 +9,15 @@ package gcfg_test import ( + "io/ioutil" + "os" + "testing" + "github.com/gogf/gf/g" "github.com/gogf/gf/g/encoding/gjson" "github.com/gogf/gf/g/os/gcfg" "github.com/gogf/gf/g/os/gfile" "github.com/gogf/gf/g/test/gtest" - "io/ioutil" - "os" - "testing" ) func init() { @@ -38,7 +39,9 @@ array = [1,2,3] path := gcfg.DEFAULT_CONFIG_FILE err := gfile.PutContents(path, config) gtest.Assert(err, nil) - defer gfile.Remove(path) + defer func() { + _ = gfile.Remove(path) + }() c := gcfg.New() gtest.Assert(c.Get("v1"), 1) @@ -163,7 +166,9 @@ func Test_SetFileName(t *testing.T) { path := "config.json" err := gfile.PutContents(path, config) gtest.Assert(err, nil) - defer gfile.Remove(path) + defer func() { + _ = gfile.Remove(path) + }() c := gcfg.New() c.SetFileName(path) @@ -232,7 +237,7 @@ func Test_Instance(t *testing.T) { path := gcfg.DEFAULT_CONFIG_FILE err := gfile.PutContents(path, config) gtest.Assert(err, nil) - defer gfile.Remove(path) + defer gtest.Assert(gfile.Remove(path), nil) c := gcfg.Instance() gtest.Assert(c.Get("v1"), 1) @@ -286,12 +291,14 @@ func TestCfg_New(t *testing.T) { gtest.Assert(c.GetFileName(), "config.yml") configPath := gfile.Pwd() + gfile.Separator + "config" - gfile.Mkdir(configPath) - defer gfile.Remove(configPath) + _ = gfile.Mkdir(configPath) + defer func() { + _ = gfile.Remove(configPath) + }() c = gcfg.New("config.yml") gtest.Assert(c.Get("name"), nil) - os.Unsetenv("GF_GCFG_PATH") + _ = os.Unsetenv("GF_GCFG_PATH") c = gcfg.New("config.yml") gtest.Assert(c.Get("name"), nil) }) @@ -340,9 +347,11 @@ func TestCfg_FilePath(t *testing.T) { func TestCfg_Get(t *testing.T) { gtest.Case(t, func() { configPath := gfile.Pwd() + gfile.Separator + "config" - gfile.Mkdir(configPath) - defer gfile.Remove(configPath) - ioutil.WriteFile(configPath+gfile.Separator+"config.yml", []byte("wrong config"), 0644) + _ = gfile.Mkdir(configPath) + defer func() { + _ = gfile.Remove(configPath) + }() + _ = ioutil.WriteFile(configPath+gfile.Separator+"config.yml", []byte("wrong config"), 0644) c := gcfg.New("config.yml") gtest.Assert(c.Get("name"), nil) gtest.Assert(c.GetVar("name").Val(), nil) @@ -379,7 +388,7 @@ func TestCfg_Get(t *testing.T) { c.Clear() arr, _ := gjson.Encode(g.Map{"name": "gf", "time": "2019-06-12", "person": g.Map{"name": "gf"}, "floats": g.Slice{1, 2, 3}}) - ioutil.WriteFile(configPath+gfile.Separator+"config.yml", arr, 0644) + _ = ioutil.WriteFile(configPath+gfile.Separator+"config.yml", arr, 0644) gtest.Assert(c.GetTime("time").Format("2006-01-02"), "2019-06-12") gtest.Assert(c.GetGTime("time").Format("Y-m-d"), "2019-06-12") gtest.Assert(c.GetDuration("time").String(), "0s")