ffcb1a77b9
对 60 个非测试源文件做文档/注释/代码一致性审查,修复 23 项。全部为注释/文档 改动 + 1 处死代码删除,无运行逻辑变更,无 breaking change。 A 档·实质错误(10 项,会误导下游): - app.go: closeResources 三连契约注释统一修正--failAfterInit 走 rollbackReplacedResources 不走 closeResources,closeResources 仅供 doShutdown 复用 - database/manager.go: RandomPicker 注释从过时的 rand.Intn/issue 54899 改为 crypto/rand - model/base.go: datetime(0) 与“亚秒精度”矛盾,改为 datetime(6) - cache/keybuilder.go: 三处示例下划线分隔符改为冒号(与默认分隔符 : 一致) - repository/repository.go: Delete 接口注释从绝对“软删除”改为条件性; UpdateFields 示例移除把字段名当 WHERE 条件的误用写法 - middleware/csrf.go: CSRFToken 注释从“用于 API 模式”改为 Cookie/双重提交模式说明 - examples/full/main.go: POST /users 补上“需 Authorization”标注;删除 _ = app 死代码 - utils/random.go: RandInt/RandInt64 补 min==max 边界说明(对齐 RandIntSecure 写法) B 档·导出符号注释缺失/不完整(6 项): - ws/ws.go: 5 个 Type* 常量、4 个 Err* 变量补注释 - console/console.go: 5 个 Level 常量补注释 - jwt/jwt.go: 2 个 Blacklist 常量补注释 - router/router.go: 包级 GroupWithMiddlewareGroup 补注释 - middleware/ratelimit.go: NewRateLimiter 补 panic 条件与 Stop 生命周期说明 C 档·描述不完整(7 项): - utils/strings.go Substr、convert.go CalcPageCount/CalcOffset、datetime.go StartOfMonth/EndOfMonth、file.go CopyFile、validator.go IsChinese、crypto.go HashFile 补全边界/默认行为/返回值语义 - response/error.go: WithDetail 补 nil 接收者行为说明 验证:go build / go vet / go test -race 全绿。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
107 lines
2.4 KiB
Go
107 lines
2.4 KiB
Go
package utils
|
|
|
|
import (
|
|
"strconv"
|
|
)
|
|
|
|
// ToInt 字符串转 int,失败返回 0
|
|
func ToInt(s string) int {
|
|
n, _ := strconv.Atoi(s)
|
|
return n
|
|
}
|
|
|
|
// ToIntE converts a string to int and returns parse errors. Prefer it when 0
|
|
// is a meaningful value and parse failure must be distinguishable.
|
|
func ToIntE(s string) (int, error) {
|
|
return strconv.Atoi(s)
|
|
}
|
|
|
|
// ToIntDefault 字符串转 int,失败返回默认值
|
|
func ToIntDefault(s string, def int) int {
|
|
n, err := strconv.Atoi(s)
|
|
if err != nil {
|
|
return def
|
|
}
|
|
return n
|
|
}
|
|
|
|
// ToInt64 字符串转 int64,失败返回 0
|
|
func ToInt64(s string) int64 {
|
|
n, _ := strconv.ParseInt(s, 10, 64)
|
|
return n
|
|
}
|
|
|
|
// ToInt64E converts a string to int64 and returns parse errors. Prefer it when
|
|
// 0 is a meaningful value and parse failure must be distinguishable.
|
|
func ToInt64E(s string) (int64, error) {
|
|
return strconv.ParseInt(s, 10, 64)
|
|
}
|
|
|
|
// ToInt64Default 字符串转 int64,失败返回默认值
|
|
func ToInt64Default(s string, def int64) int64 {
|
|
n, err := strconv.ParseInt(s, 10, 64)
|
|
if err != nil {
|
|
return def
|
|
}
|
|
return n
|
|
}
|
|
|
|
// ToUint64 字符串转 uint64,失败返回 0
|
|
func ToUint64(s string) uint64 {
|
|
n, _ := strconv.ParseUint(s, 10, 64)
|
|
return n
|
|
}
|
|
|
|
// ToUint64Default 字符串转 uint64,失败返回默认值
|
|
func ToUint64Default(s string, def uint64) uint64 {
|
|
n, err := strconv.ParseUint(s, 10, 64)
|
|
if err != nil {
|
|
return def
|
|
}
|
|
return n
|
|
}
|
|
|
|
// ToFloat64 字符串转 float64,失败返回 0
|
|
func ToFloat64(s string) float64 {
|
|
n, _ := strconv.ParseFloat(s, 64)
|
|
return n
|
|
}
|
|
|
|
// ToFloat64Default 字符串转 float64,失败返回默认值
|
|
func ToFloat64Default(s string, def float64) float64 {
|
|
n, err := strconv.ParseFloat(s, 64)
|
|
if err != nil {
|
|
return def
|
|
}
|
|
return n
|
|
}
|
|
|
|
// ToString 整数转字符串
|
|
func ToString(n int) string {
|
|
return strconv.Itoa(n)
|
|
}
|
|
|
|
// ToString64 int64 转字符串
|
|
func ToString64(n int64) string {
|
|
return strconv.FormatInt(n, 10)
|
|
}
|
|
|
|
// CalcPageCount 计算总页数(向上取整)。total<=0 或 pageSize<=0 时返回 0。
|
|
func CalcPageCount(total, pageSize int64) int64 {
|
|
if total <= 0 || pageSize <= 0 {
|
|
return 0
|
|
}
|
|
return (total + pageSize - 1) / pageSize
|
|
}
|
|
|
|
// CalcOffset 计算分页偏移量 (page-1)*pageSize。page<=0 时按 1 处理,pageSize<=0 时按 20 处理。
|
|
func CalcOffset(page, pageSize int) int {
|
|
if page <= 0 {
|
|
page = 1
|
|
}
|
|
if pageSize <= 0 {
|
|
pageSize = 20
|
|
}
|
|
return (page - 1) * pageSize
|
|
}
|