From cf2a7e985a80e99957c172eb5ddf0d17c7107f2f Mon Sep 17 00:00:00 2001 From: vicanso Date: Fri, 5 Aug 2022 20:44:21 +0800 Subject: [PATCH] refactor: fix lint for go 1.19 --- .github/workflows/test.yml | 1 + context.go | 3 +-- docs/custom_body_parser.md | 10 +++++----- docs/http2_http3.md | 12 ++++++------ go.mod | 2 +- go.sum | 4 ++-- middleware/body_parser.go | 3 +-- middleware/body_parser_test.go | 3 +-- middleware/brotli.go | 3 +-- middleware/compressor_test.go | 5 ++--- middleware/gzip.go | 3 +-- middleware/renderer_test.go | 3 +-- middleware/static_serve.go | 3 +-- template.go | 4 ++-- template_test.go | 3 +-- 15 files changed, 27 insertions(+), 35 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 374e176..c0b732b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,6 +14,7 @@ jobs: strategy: matrix: go: + - '1.19' - '1.18' - '1.17' - '1.16' diff --git a/context.go b/context.go index d5b80b4..ce9d135 100644 --- a/context.go +++ b/context.go @@ -27,7 +27,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "mime" "mime/multipart" "net" @@ -627,7 +626,7 @@ func (c *Context) ReadFile(key string) ([]byte, *multipart.FileHeader, error) { return nil, nil, err } defer file.Close() - buf, err := ioutil.ReadAll(file) + buf, err := io.ReadAll(file) if err != nil { return nil, nil, err } diff --git a/docs/custom_body_parser.md b/docs/custom_body_parser.md index 654dfd1..5e38044 100644 --- a/docs/custom_body_parser.md +++ b/docs/custom_body_parser.md @@ -12,7 +12,7 @@ package main import ( "bytes" "fmt" - "io/ioutil" + "io" "net/http" "strconv" "strings" @@ -42,7 +42,7 @@ func post() { if err != nil { panic(err) } - result, _ := ioutil.ReadAll(resp.Body) + result, _ := io.ReadAll(resp.Body) fmt.Println(string(result)) } @@ -59,7 +59,7 @@ func NewInfluxParser() elton.Handler { c.GetRequestHeader(elton.HeaderContentType) != ContentTypeIfx { return c.Next() } - body, err := ioutil.ReadAll(c.Request.Body) + body, err := io.ReadAll(c.Request.Body) // 如果读取数据时出错,直接返回 if err != nil { return @@ -109,7 +109,7 @@ package main import ( "bytes" "fmt" - "io/ioutil" + "io" "net/http" "regexp" "strconv" @@ -141,7 +141,7 @@ func post() { if err != nil { panic(err) } - result, _ := ioutil.ReadAll(resp.Body) + result, _ := io.ReadAll(resp.Body) fmt.Println(string(result)) } diff --git a/docs/http2_http3.md b/docs/http2_http3.md index 921a759..383475a 100644 --- a/docs/http2_http3.md +++ b/docs/http2_http3.md @@ -52,7 +52,8 @@ package main import ( "bytes" "crypto/tls" - "io/ioutil" + "io" + "os" "github.com/vicanso/elton" ) @@ -60,11 +61,11 @@ import ( // 获取证书内容 func getCert() (cert []byte, key []byte, err error) { // 此处仅简单从文件中读取,在实际使用,是从数据库中读取 - cert, err = ioutil.ReadFile("/tmp/me.dev+3.pem") + cert, err = os.ReadFile("/tmp/me.dev+3.pem") if err != nil { return } - key, err = ioutil.ReadFile("/tmp/me.dev+3-key.pem") + key, err = os.ReadFile("/tmp/me.dev+3-key.pem") if err != nil { return } @@ -188,7 +189,6 @@ package main import ( "bytes" "crypto/tls" - "io/ioutil" "log" "net/http" "time" @@ -202,11 +202,11 @@ const listenAddr = ":4000" // 获取证书内容 func getCert() (cert []byte, key []byte, err error) { // 此处仅简单从文件中读取,在实际使用,是从数据库中读取 - cert, err = ioutil.ReadFile("/tmp/me.dev+3.pem") + cert, err = os.ReadFile("/tmp/me.dev+3.pem") if err != nil { return } - key, err = ioutil.ReadFile("/tmp/me.dev+3-key.pem") + key, err = os.ReadFile("/tmp/me.dev+3-key.pem") if err != nil { return } diff --git a/go.mod b/go.mod index edd8b99..b28baaa 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.17 require ( github.com/andybalholm/brotli v1.0.4 github.com/stretchr/testify v1.8.0 - github.com/tidwall/gjson v1.14.1 + github.com/tidwall/gjson v1.14.2 github.com/vicanso/hes v0.6.0 github.com/vicanso/intranet-ip v0.1.0 github.com/vicanso/keygrip v1.2.1 diff --git a/go.sum b/go.sum index c72edc6..811a3cb 100644 --- a/go.sum +++ b/go.sum @@ -12,8 +12,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/tidwall/gjson v1.14.1 h1:iymTbGkQBhveq21bEvAQ81I0LEBork8BFe1CUZXdyuo= -github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/gjson v1.14.2 h1:6BBkirS0rAHjumnjHF6qgy5d2YAJ1TLIaFE2lzfOLqo= +github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= diff --git a/middleware/body_parser.go b/middleware/body_parser.go index ba7de29..41b5d5f 100644 --- a/middleware/body_parser.go +++ b/middleware/body_parser.go @@ -27,7 +27,6 @@ import ( "compress/gzip" "fmt" "io" - "io/ioutil" "net/http" "net/url" "strconv" @@ -217,7 +216,7 @@ func doGunzip(buf []byte) ([]byte, error) { return nil, err } defer r.Close() - return ioutil.ReadAll(r) + return io.ReadAll(r) } type maxBytesReader struct { diff --git a/middleware/body_parser_test.go b/middleware/body_parser_test.go index 2016c10..0889f0f 100644 --- a/middleware/body_parser_test.go +++ b/middleware/body_parser_test.go @@ -28,7 +28,6 @@ import ( "encoding/base64" "errors" "io" - "io/ioutil" "math/rand" "net/http" "net/http/httptest" @@ -246,7 +245,7 @@ func TestMaxBytesReader(t *testing.T) { } for _, tt := range tests { r := MaxBytesReader(tt.reader, tt.max) - result, err := ioutil.ReadAll(r) + result, err := io.ReadAll(r) assert.Equal(tt.err, err) assert.Equal(tt.result, result) } diff --git a/middleware/brotli.go b/middleware/brotli.go index f10d645..c0f4efd 100644 --- a/middleware/brotli.go +++ b/middleware/brotli.go @@ -25,7 +25,6 @@ package middleware import ( "bytes" "io" - "io/ioutil" "github.com/andybalholm/brotli" "github.com/vicanso/elton" @@ -92,7 +91,7 @@ func BrotliCompress(buf []byte, level int) (*bytes.Buffer, error) { // BrotliDecompress decompress data of brotli func BrotliDecompress(buf []byte) (*bytes.Buffer, error) { r := brotli.NewReader(bytes.NewBuffer(buf)) - data, err := ioutil.ReadAll(r) + data, err := io.ReadAll(r) if err != nil { return nil, err } diff --git a/middleware/compressor_test.go b/middleware/compressor_test.go index bd39b80..eba0885 100644 --- a/middleware/compressor_test.go +++ b/middleware/compressor_test.go @@ -26,7 +26,6 @@ import ( "bytes" "compress/gzip" "io" - "io/ioutil" "net/http/httptest" "testing" @@ -105,13 +104,13 @@ func TestCompressorPipe(t *testing.T) { return nil, err } defer gzipReader.Close() - return ioutil.ReadAll(gzipReader) + return io.ReadAll(gzipReader) }, }, { compressor: new(BrCompressor), uncompress: func(r io.Reader) ([]byte, error) { - return ioutil.ReadAll(brotli.NewReader(r)) + return io.ReadAll(brotli.NewReader(r)) }, }, } diff --git a/middleware/gzip.go b/middleware/gzip.go index 3a56458..8026f55 100644 --- a/middleware/gzip.go +++ b/middleware/gzip.go @@ -26,7 +26,6 @@ import ( "bytes" "compress/gzip" "io" - "io/ioutil" "github.com/vicanso/elton" ) @@ -79,7 +78,7 @@ func GzipDecompress(buf []byte) (*bytes.Buffer, error) { return nil, err } defer r.Close() - data, err := ioutil.ReadAll(r) + data, err := io.ReadAll(r) if err != nil { return nil, err } diff --git a/middleware/renderer_test.go b/middleware/renderer_test.go index f894bd3..cf5a53d 100644 --- a/middleware/renderer_test.go +++ b/middleware/renderer_test.go @@ -23,7 +23,6 @@ package middleware import ( - "io/ioutil" "net/http/httptest" "os" "testing" @@ -113,7 +112,7 @@ func TestRenderer(t *testing.T) { t.Run("render html from file", func(t *testing.T) { // render file - f, err := ioutil.TempFile("", "") + f, err := os.CreateTemp("", "") assert.Nil(err) filename := f.Name() defer os.Remove(filename) diff --git a/middleware/static_serve.go b/middleware/static_serve.go index 3067f8e..a327a9c 100644 --- a/middleware/static_serve.go +++ b/middleware/static_serve.go @@ -28,7 +28,6 @@ import ( "encoding/base64" "fmt" "io" - "io/ioutil" "net/http" "os" "path/filepath" @@ -111,7 +110,7 @@ func (fs *FS) Stat(file string) os.FileInfo { // Get get the file's content func (fs *FS) Get(file string) ([]byte, error) { - return ioutil.ReadFile(file) + return os.ReadFile(file) } // NewReader new a reader for file diff --git a/template.go b/template.go index c0ad536..fb7f7d4 100644 --- a/template.go +++ b/template.go @@ -26,7 +26,7 @@ import ( "bytes" "context" "html/template" - "io/ioutil" + "os" ) type TemplateParser interface { @@ -94,7 +94,7 @@ func (ht *HTMLTemplate) Render(ctx context.Context, text string, data interface{ func (ht *HTMLTemplate) RenderFile(ctx context.Context, filename string, data interface{}) (string, error) { read := ht.readFile if read == nil { - read = ioutil.ReadFile + read = os.ReadFile } buf, err := read(filename) if err != nil { diff --git a/template_test.go b/template_test.go index 74c27aa..4fb33ed 100644 --- a/template_test.go +++ b/template_test.go @@ -24,7 +24,6 @@ package elton import ( "context" - "io/ioutil" "os" "testing" @@ -54,7 +53,7 @@ func TestHTMLTemplate(t *testing.T) { t.Run("render file", func(t *testing.T) { // render file - f, err := ioutil.TempFile("", "") + f, err := os.CreateTemp("", "") assert.Nil(err) filename := f.Name() defer os.Remove(filename)