diff --git a/.example/other/test.go b/.example/other/test.go index cd5867790..2fe3b9889 100644 --- a/.example/other/test.go +++ b/.example/other/test.go @@ -1,13 +1,29 @@ package main import ( + "encoding/hex" "fmt" - "github.com/gogf/gf/os/gtimer" - "time" + "github.com/gogf/gf/encoding/gbase64" + "github.com/gogf/gf/util/gconv" + "github.com/gogf/gf/util/grand" ) -func main() { - tr := gtimer.New(100, 1*time.Second, 10) - tr.Add(1*time.Second, func() { fmt.Println("hello") }) - select {} +// bytesToHexString converts binary content to hex string content. +func bytesToHexStr(b []byte) string { + dst := make([]byte, hex.EncodedLen(len(b))) + hex.Encode(dst, b) + return gconv.UnsafeBytesToStr(dst) +} + +func main() { + b := make([]byte, 1024) + for i := 0; i < 1024; i++ { + b[i] = byte(grand.N(0, 255)) + } + + fmt.Println(bytesToHexStr(b)) + fmt.Println(len(b)) + fmt.Println(len(bytesToHexStr(b))) + fmt.Println(gbase64.EncodeToString(b)) + fmt.Println(len(gbase64.EncodeToString(b))) } diff --git a/encoding/gcompress/gcompress_gzip.go b/encoding/gcompress/gcompress_gzip.go index fd5cc90a3..e6bd5f155 100644 --- a/encoding/gcompress/gcompress_gzip.go +++ b/encoding/gcompress/gcompress_gzip.go @@ -19,9 +19,11 @@ import ( // // Note that it returns error if given is invalid. func Gzip(data []byte, level ...int) ([]byte, error) { - var writer *gzip.Writer - var buf bytes.Buffer - var err error + var ( + writer *gzip.Writer + buf bytes.Buffer + err error + ) if len(level) > 0 { writer, err = gzip.NewWriterLevel(&buf, level[0]) if err != nil { @@ -41,8 +43,10 @@ func Gzip(data []byte, level ...int) ([]byte, error) { // GzipFile compresses the file to using gzip algorithm. func GzipFile(src, dst string, level ...int) error { - var writer *gzip.Writer - var err error + var ( + writer *gzip.Writer + err error + ) srcFile, err := gfile.Open(src) if err != nil { return err