From e09538bd3bfa289f51282d5b9b0e971852ffc9ed Mon Sep 17 00:00:00 2001 From: helei Date: Mon, 28 Sep 2026 10:55:03 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E5=8D=87=E7=BA=A7=E7=AC=AC=E4=B8=89?= =?UTF-8?q?=E6=96=B9=E4=BE=9D=E8=B5=96=E5=B9=B6=E7=8E=B0=E4=BB=A3=E5=8C=96?= =?UTF-8?q?=E5=8F=8D=E5=B0=84=E4=B8=8E=E5=88=87=E7=89=87=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - go.mod: 批量升级依赖到最新版本(sarama v1.61.1、etcd v3.7.2、quic-go v0.63.0、 minio-go v7.3.0、excelize v2.11.0、go-redis v9.22.0、golang.org/x/* 等),go 指令细化为 1.26.0 - tools/any2any.go: reflect.Ptr 改用 reflect.Pointer,reflect.TypeOf 改用泛型 reflect.TypeFor, 嵌套结构体递归处复用已声明的 err 变量 - tools/array.go: 分块循环改用 range over int 与内置 min,精简 CutStrSlice2Slice 的越界分支 --- go.mod | 128 +++++++++++++++++++++++------------------------ tools/any2any.go | 20 ++++---- tools/array.go | 22 +++----- 3 files changed, 82 insertions(+), 88 deletions(-) diff --git a/go.mod b/go.mod index 428bccc..6baf7ca 100644 --- a/go.mod +++ b/go.mod @@ -1,55 +1,55 @@ module helay.net/go/utils/v3 -go 1.26 +go 1.26.0 require ( - github.com/IBM/sarama v1.50.3 + github.com/IBM/sarama v1.61.1 github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de github.com/cespare/xxhash/v2 v2.3.0 github.com/colinmarc/hdfs/v2 v2.4.0 github.com/dchest/captcha v1.1.0 - github.com/elastic/go-elasticsearch/v8 v8.19.6 + github.com/elastic/go-elasticsearch/v8 v8.19.7 github.com/gin-gonic/gin v1.12.0 - github.com/go-playground/form/v4 v4.3.0 - github.com/go-sql-driver/mysql v1.10.0 + github.com/go-playground/form/v4 v4.5.0 + github.com/go-sql-driver/mysql v1.10.1 github.com/go-stomp/stomp/v3 v3.1.5 github.com/go-zookeeper/zk v1.0.4 - github.com/goccy/go-json v0.10.6 + github.com/goccy/go-json v0.11.1 github.com/golang-jwt/jwt/v5 v5.3.1 github.com/gomodule/redigo v1.9.3 github.com/google/uuid v1.6.0 github.com/gorilla/handlers v1.5.2 github.com/gorilla/schema v1.4.1 github.com/helays/gomail/v2 v2.0.3 - github.com/jackc/pgx/v5 v5.10.0 - github.com/jlaffaye/ftp v0.2.1 + github.com/jackc/pgx/v5 v5.11.0 + github.com/jlaffaye/ftp v0.2.4 github.com/malfunkt/iprange v0.9.0 - github.com/minio/minio-go/v7 v7.2.1 + github.com/minio/minio-go/v7 v7.3.0 github.com/nacos-group/nacos-sdk-go/v2 v2.3.5 github.com/natefinch/lumberjack v2.0.0+incompatible github.com/pkg/errors v0.9.1 - github.com/pkg/sftp v1.13.10 - github.com/quic-go/quic-go v0.60.0 + github.com/pkg/sftp v1.13.11 + github.com/quic-go/quic-go v0.63.0 github.com/rcrowley/go-metrics v0.0.0-20250401214520-65e299d6c5c9 - github.com/redis/go-redis/v9 v9.21.0 + github.com/redis/go-redis/v9 v9.22.0 github.com/robfig/cron/v3 v3.0.1 github.com/sony/sonyflake/v2 v2.2.0 github.com/tjfoc/gmsm v1.4.1 github.com/vmihailenco/msgpack/v5 v5.4.1 github.com/xdg-go/scram v1.2.0 - github.com/xuri/excelize/v2 v2.10.1 - go.etcd.io/etcd/client/v3 v3.6.13 + github.com/xuri/excelize/v2 v2.11.0 + go.etcd.io/etcd/client/v3 v3.7.2 go.uber.org/zap v1.28.0 - go4.org/netipx v0.0.0-20231129151722-fdeea329fbba - golang.org/x/crypto v0.53.0 - golang.org/x/exp v0.0.0-20260611194520-c48552f49976 - golang.org/x/net v0.56.0 - golang.org/x/text v0.38.0 + go4.org/netipx v0.0.0-20260823151212-3075585bcbeb + golang.org/x/crypto v0.57.0 + golang.org/x/exp v0.0.0-20260908205506-85c1c2202aba + golang.org/x/net v0.59.0 + golang.org/x/text v0.42.0 gopkg.in/ini.v1 v1.67.3 gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 gopkg.in/yaml.v3 v3.0.1 gorm.io/driver/mysql v1.6.0 - gorm.io/driver/postgres v1.6.0 + gorm.io/driver/postgres v1.6.3 gorm.io/driver/sqlite v1.6.0 gorm.io/gorm v1.31.2 ) @@ -61,50 +61,50 @@ require ( github.com/alibabacloud-go/darabonba-array v0.1.0 // indirect github.com/alibabacloud-go/darabonba-encode-util v0.0.2 // indirect github.com/alibabacloud-go/darabonba-map v0.0.2 // indirect - github.com/alibabacloud-go/darabonba-openapi/v2 v2.2.3 // indirect + github.com/alibabacloud-go/darabonba-openapi/v2 v2.2.4 // indirect github.com/alibabacloud-go/darabonba-signature-util v0.0.7 // indirect github.com/alibabacloud-go/darabonba-string v1.0.2 // indirect github.com/alibabacloud-go/debug v1.0.1 // indirect github.com/alibabacloud-go/endpoint-util v1.1.1 // indirect github.com/alibabacloud-go/kms-20160120/v3 v3.4.0 // indirect github.com/alibabacloud-go/openapi-util v0.1.2 // indirect - github.com/alibabacloud-go/tea v1.5.2 // indirect + github.com/alibabacloud-go/tea v1.5.3 // indirect github.com/alibabacloud-go/tea-utils/v2 v2.0.9 // indirect github.com/alibabacloud-go/tea-xml v1.1.3 // indirect github.com/aliyun/alibaba-cloud-sdk-go v1.63.107 // indirect github.com/aliyun/alibabacloud-dkms-gcs-go-sdk v0.5.1 // indirect github.com/aliyun/alibabacloud-dkms-transfer-go-sdk v0.1.9 // indirect github.com/aliyun/aliyun-secretsmanager-client-go v1.1.5 // indirect - github.com/aliyun/credentials-go v1.4.12 // indirect + github.com/aliyun/credentials-go v1.4.13 // indirect github.com/beorn7/perks v1.0.1 // indirect - github.com/buger/jsonparser v1.2.0 // indirect + github.com/buger/jsonparser v1.6.1 // indirect github.com/bytedance/gopkg v0.1.4 // indirect - github.com/bytedance/sonic v1.15.2 // indirect - github.com/bytedance/sonic/loader v0.5.1 // indirect + github.com/bytedance/sonic v1.15.4 // indirect + github.com/bytedance/sonic/loader v0.5.2 // indirect github.com/clbanning/mxj/v2 v2.7.0 // indirect github.com/cloudwego/base64x v0.1.7 // indirect github.com/coreos/go-semver v0.3.1 // indirect github.com/coreos/go-systemd/v22 v22.7.0 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/deckarep/golang-set v1.8.0 // indirect - github.com/dustin/go-humanize v1.0.1 // indirect + github.com/dustin/go-humanize v1.1.0 // indirect github.com/eapache/go-resiliency v1.7.0 // indirect github.com/elastic/elastic-transport-go/v8 v8.11.0 // indirect github.com/felixge/httpsnoop v1.1.0 // indirect - github.com/gabriel-vasile/mimetype v1.4.13 // indirect - github.com/gin-contrib/sse v1.1.1 // indirect - github.com/go-logr/logr v1.4.3 // indirect + github.com/gabriel-vasile/mimetype v1.4.15 // indirect + github.com/gin-contrib/sse v1.1.2 // indirect + github.com/go-logr/logr v1.4.4 // indirect github.com/go-logr/stdr v1.2.2 // indirect - github.com/go-playground/locales v0.14.1 // indirect - github.com/go-playground/universal-translator v0.18.1 // indirect - github.com/go-playground/validator/v10 v10.30.3 // indirect + github.com/go-playground/locales v0.14.2 // indirect + github.com/go-playground/universal-translator v0.18.2 // indirect + github.com/go-playground/validator/v10 v10.30.5 // indirect github.com/goccy/go-yaml v1.19.2 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/mock v1.7.0-rc.1 // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/gorilla/websocket v1.5.3 // indirect - github.com/grpc-ecosystem/grpc-gateway/v2 v2.29.0 // indirect - github.com/hashicorp/go-uuid v1.0.3 // indirect + github.com/grpc-ecosystem/grpc-gateway/v2 v2.31.0 // indirect + github.com/hashicorp/go-uuid v1.0.4 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/puddle/v2 v2.2.2 // indirect @@ -118,13 +118,13 @@ require ( github.com/jinzhu/now v1.1.5 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/json-iterator/go v1.1.12 // indirect - github.com/klauspost/compress v1.19.0 // indirect + github.com/klauspost/compress v1.20.1 // indirect github.com/klauspost/cpuid/v2 v2.4.0 // indirect github.com/klauspost/crc32 v1.3.0 // indirect github.com/kr/fs v0.1.0 // indirect - github.com/leodido/go-urn v1.4.0 // indirect - github.com/mattn/go-isatty v0.0.22 // indirect - github.com/mattn/go-sqlite3 v1.14.47 // indirect + github.com/leodido/go-urn v1.5.0 // indirect + github.com/mattn/go-isatty v0.0.24 // indirect + github.com/mattn/go-sqlite3 v1.14.52 // indirect github.com/minio/crc64nvme v1.1.1 // indirect github.com/minio/md5-simd v1.1.2 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect @@ -132,46 +132,46 @@ require ( github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b // indirect github.com/orcaman/concurrent-map v1.0.0 // indirect - github.com/pelletier/go-toml/v2 v2.4.2 // indirect + github.com/pelletier/go-toml/v2 v2.4.3 // indirect github.com/philhofer/fwd v1.2.0 // indirect - github.com/pierrec/lz4/v4 v4.1.27 // indirect + github.com/pierrec/lz4/v4 v4.1.31 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect - github.com/prometheus/client_golang v1.23.2 // indirect - github.com/prometheus/client_model v0.6.2 // indirect - github.com/prometheus/common v0.69.0 // indirect - github.com/prometheus/procfs v0.21.1 // indirect + github.com/prometheus/client_golang v1.24.1 // indirect + github.com/prometheus/client_model v0.6.3 // indirect + github.com/prometheus/common v0.71.0 // indirect + github.com/prometheus/procfs v0.22.0 // indirect github.com/quic-go/qpack v0.6.0 // indirect - github.com/richardlehane/mscfb v1.0.7 // indirect + github.com/richardlehane/mscfb v1.0.9 // indirect github.com/richardlehane/msoleps v1.0.6 // indirect github.com/rs/xid v1.6.0 // indirect github.com/tiendc/go-deepcopy v1.7.2 // indirect github.com/tinylib/msgp v1.6.4 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect - github.com/ugorji/go/codec v1.3.1 // indirect + github.com/ugorji/go/codec v1.3.2 // indirect github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect github.com/xdg-go/pbkdf2 v1.0.0 // indirect github.com/xdg-go/stringprep v1.0.4 // indirect - github.com/xuri/efp v0.0.1 // indirect + github.com/xuri/efp v0.0.2 // indirect github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9 // indirect github.com/zeebo/xxh3 v1.1.0 // indirect - go.etcd.io/etcd/api/v3 v3.6.13 // indirect - go.etcd.io/etcd/client/pkg/v3 v3.6.13 // indirect - go.mongodb.org/mongo-driver/v2 v2.7.0 // indirect + go.etcd.io/etcd/api/v3 v3.7.2 // indirect + go.etcd.io/etcd/client/pkg/v3 v3.7.2 // indirect + go.mongodb.org/mongo-driver/v2 v2.9.1 // indirect go.opentelemetry.io/auto/sdk v1.2.1 // indirect - go.opentelemetry.io/otel v1.44.0 // indirect - go.opentelemetry.io/otel/metric v1.44.0 // indirect - go.opentelemetry.io/otel/trace v1.44.0 // indirect - go.uber.org/atomic v1.11.0 // indirect + go.opentelemetry.io/otel v1.46.0 // indirect + go.opentelemetry.io/otel/metric v1.46.0 // indirect + go.opentelemetry.io/otel/trace v1.46.0 // indirect + go.uber.org/atomic v1.12.0 // indirect go.uber.org/multierr v1.11.0 // indirect - go.yaml.in/yaml/v3 v3.0.4 // indirect - golang.org/x/arch v0.28.0 // indirect - golang.org/x/sync v0.21.0 // indirect - golang.org/x/sys v0.46.0 // indirect - golang.org/x/time v0.15.0 // indirect - google.golang.org/genproto/googleapis/api v0.0.0-20260630182238-925bb5da69e7 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20260630182238-925bb5da69e7 // indirect - google.golang.org/grpc v1.82.0 // indirect - google.golang.org/protobuf v1.36.11 // indirect + go.yaml.in/yaml/v3 v3.0.5 // indirect + golang.org/x/arch v0.31.0 // indirect + golang.org/x/sync v0.23.0 // indirect + golang.org/x/sys v0.48.0 // indirect + golang.org/x/time v0.16.0 // indirect + google.golang.org/genproto/googleapis/api v0.0.0-20260921155816-b14227669459 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20260921155816-b14227669459 // indirect + google.golang.org/grpc v1.84.0 // indirect + google.golang.org/protobuf v1.36.12 // indirect gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect ) diff --git a/tools/any2any.go b/tools/any2any.go index 59811c8..8bf6d6e 100644 --- a/tools/any2any.go +++ b/tools/any2any.go @@ -32,7 +32,7 @@ func Map2Struct(dst any, src map[string]any, customConvert map[string]func(dst a // 如果这个字段是一个匿名结构体,还需要递归处理 if field.Type.Kind() == reflect.Struct && field.Anonymous { // 递归处理嵌套结构体,注意这里不能使用field - if err := Map2Struct(val.Field(i).Addr().Interface(), src, customConvert); err != nil { + if err = Map2Struct(val.Field(i).Addr().Interface(), src, customConvert); err != nil { return err } continue @@ -146,7 +146,7 @@ func Any2string(v any) string { // 使用反射处理更多类型 rv := reflect.ValueOf(v) // 处理指针类型 - if rv.Kind() == reflect.Ptr { + if rv.Kind() == reflect.Pointer { if rv.IsNil() { return "" } @@ -316,7 +316,7 @@ func Any2float64(_v any) (float64, error) { // 使用反射处理更多类型 rv := reflect.ValueOf(v) // 处理指针类型 - if rv.Kind() == reflect.Ptr { + if rv.Kind() == reflect.Pointer { if rv.IsNil() { return 0, nil } @@ -421,7 +421,7 @@ func Any2bool(_v any) (bool, error) { // 使用反射处理更多类型 rv := reflect.ValueOf(v) // 处理指针类型 - if rv.Kind() == reflect.Ptr { + if rv.Kind() == reflect.Pointer { if rv.IsNil() { return false, nil } @@ -517,7 +517,7 @@ func Any2Map(src any) (any, error) { switch val.Kind() { case reflect.Map, reflect.Struct, reflect.Slice, reflect.Array: return _src, nil - case reflect.Ptr: + case reflect.Pointer: // 解引用指针 if val.IsNil() { return nil, nil @@ -613,7 +613,7 @@ func tryConvertOtherMap(input any) (map[string]any, bool) { // 尝试将结构体转换为 map func tryConvertStruct(input any) (map[string]any, bool) { val := reflect.ValueOf(input) - if val.Kind() == reflect.Ptr { + if val.Kind() == reflect.Pointer { val = val.Elem() } @@ -663,9 +663,9 @@ func tryConvertStruct(input any) (map[string]any, bool) { // 预定义常见类型,减少反射调用 var ( - mapStringInterfaceType = reflect.TypeOf(map[string]any(nil)) - sliceInterfaceType = reflect.TypeOf([]any(nil)) - timeType = reflect.TypeOf(time.Time{}) + mapStringInterfaceType = reflect.TypeFor[map[string]any]() + sliceInterfaceType = reflect.TypeFor[[]any]() + timeType = reflect.TypeFor[time.Time]() ) // CheckIsObject 检查给定的值是否是一个对象(map、slice、struct、指针) @@ -689,7 +689,7 @@ func CheckIsObject(v any) bool { return false // nil 不需要序列化 } // 检查是否是指针 - if typ.Kind() == reflect.Ptr { + if typ.Kind() == reflect.Pointer { if reflect.ValueOf(v).IsNil() { return false // nil 指针不需要序列化 } diff --git a/tools/array.go b/tools/array.go index f2e2964..cf08ea6 100644 --- a/tools/array.go +++ b/tools/array.go @@ -25,12 +25,9 @@ func ArrayChunk[T any](slice []T, size int) [][]T { chunkCount := (length + size - 1) / size chunks := make([][]T, chunkCount) - for i := 0; i < chunkCount; i++ { + for i := range chunkCount { start := i * size - end := start + size - if end > length { - end = length - } + end := min(start+size, length) // 直接引用原切片的底层数组,避免额外内存分配 chunks[i] = slice[start:end:end] // 使用完整切片表达式限制容量 } @@ -54,12 +51,9 @@ func ArrayChunkCopy[T any](slice []T, size int) [][]T { chunkCount := (length + size - 1) / size chunks := make([][]T, chunkCount) - for i := 0; i < chunkCount; i++ { + for i := range chunkCount { start := i * size - end := start + size - if end > length { - end = length - } + end := min(start+size, length) chunk := make([]T, end-start) copy(chunk, slice[start:end]) chunks[i] = chunk @@ -282,9 +276,9 @@ func CutStrSlice2Slice(s []string, key string, direct int) []string { if v == key { if idx+direct < len(s) { return s[idx+direct:] - } else { - return []string{} // 索引越界时返回空切片 } + + return []string{} // 索引越界时返回空切片 } } return []string{} @@ -436,11 +430,11 @@ func Slice2MapWithHeader(rows any, header []string) map[string]any { // 获取 rows 的反射值 rowsValue := reflect.ValueOf(rows) // 检查 rows 是否为切片类型 - if rowsValue.Kind() != reflect.Slice && rowsValue.Kind() != reflect.Ptr { + if rowsValue.Kind() != reflect.Slice && rowsValue.Kind() != reflect.Pointer { return nil } // 如果 rows 是切片的指针,则获取指向的切片 - if rowsValue.Kind() == reflect.Ptr { + if rowsValue.Kind() == reflect.Pointer { if rowsValue.IsNil() { return nil }