diff --git a/internal/db/type.go b/internal/db/type.go index e79fc99..b0e8b25 100644 --- a/internal/db/type.go +++ b/internal/db/type.go @@ -53,7 +53,7 @@ func (d *DB) get() (db dbif.DB) { panic("DB format not supported!") } - if err != nil || db == nil { + if err != nil { log.Fatalln("Database init failed:", err) } diff --git a/pkg/cdn/cdn.go b/pkg/cdn/cdn.go index e39ccee..a487c9e 100644 --- a/pkg/cdn/cdn.go +++ b/pkg/cdn/cdn.go @@ -40,7 +40,7 @@ func (r CDNResult) String() string { } func NewCDN(filePath string) (*CDN, error) { - fileData := make([]byte, 0) + var fileData []byte _, err := os.Stat(filePath) if err != nil && os.IsNotExist(err) { log.Println("文件不存在,尝试从网络获取最新CDN数据库") diff --git a/pkg/common/httpclient.go b/pkg/common/httpclient.go index 2d5418b..9969889 100644 --- a/pkg/common/httpclient.go +++ b/pkg/common/httpclient.go @@ -1,7 +1,7 @@ package common import ( - "io/ioutil" + "io" "log" "net/http" "time" @@ -46,8 +46,8 @@ func (c *HttpClient) Get(urls ...string) (body []byte, err error) { resp, err = c.Do(req) if err == nil && resp != nil && resp.StatusCode == 200 { - defer resp.Body.Close() - body, err = ioutil.ReadAll(resp.Body) + body, err = io.ReadAll(resp.Body) + _ = resp.Body.Close() if err != nil { continue } diff --git a/pkg/ip2location/ip2location.go b/pkg/ip2location/ip2location.go index 7fb4eff..9ac0572 100644 --- a/pkg/ip2location/ip2location.go +++ b/pkg/ip2location/ip2location.go @@ -10,12 +10,11 @@ import ( "github.com/ip2location/ip2location-go/v9" ) -// IP2Location type IP2Location struct { db *ip2location.DB } -// new IP2Location from database file +// NewIP2Location from database file func NewIP2Location(filePath string) (*IP2Location, error) { _, err := os.Stat(filePath) if err != nil && os.IsNotExist(err) { diff --git a/pkg/wry/index.go b/pkg/wry/index.go index ef0b3b0..d6cfa36 100644 --- a/pkg/wry/index.go +++ b/pkg/wry/index.go @@ -8,8 +8,9 @@ func (db *IPDB[uint32]) SearchIndexV4(ip uint32) uint32 { ipLen := db.IPLen entryLen := uint32(db.OffLen + db.IPLen) - buf := make([]byte, entryLen) - l, r, mid, ipc := db.IdxStart, db.IdxEnd, uint32(0), uint32(0) + l, r := db.IdxStart, db.IdxEnd + var ipc, mid uint32 + var buf []byte for { mid = (r-l)/entryLen/2*entryLen + l diff --git a/pkg/zxipv6wry/update.go b/pkg/zxipv6wry/update.go index 1df1d80..67c1cd3 100644 --- a/pkg/zxipv6wry/update.go +++ b/pkg/zxipv6wry/update.go @@ -39,6 +39,9 @@ const ( func getData() (data []byte, err error) { data, err = common.GetHttpClient().Get(zx) + if err != nil { + return nil, err + } file7z, err := os.CreateTemp("", "*") if err != nil {