Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
55e63b1261 | ||
| 8b641f1cff |
54
AI.md
54
AI.md
@ -9,37 +9,37 @@
|
|||||||
3. **使用 If 替代简单 if-else 赋值**:在需要进行条件赋值时,优先使用泛型函数 `cast.If(cond, val1, val2)`。
|
3. **使用 If 替代简单 if-else 赋值**:在需要进行条件赋值时,优先使用泛型函数 `cast.If(cond, val1, val2)`。
|
||||||
4. **无错误处理倾向**:理解 `cast` 函数不返回 `error`,在生成调用代码时不要进行错误检查。
|
4. **无错误处理倾向**:理解 `cast` 函数不返回 `error`,在生成调用代码时不要进行错误检查。
|
||||||
|
|
||||||
## 🛠 关键 API 逻辑约定
|
## 🛠 API Reference
|
||||||
|
|
||||||
| 函数 | 逻辑特征 |
|
### 基础转换
|
||||||
| :--- | :--- |
|
- `func Int(v any) int` / `func Int64(v any) int64` / `func Uint(v any) uint` / `func Uint64(v any) uint64`
|
||||||
| `Int / String / Bool` | 自动处理多级指针,转换失败返回零值。 |
|
- `func Float(v any) float32` / `func Float64(v any) float64`
|
||||||
| `Json / JsonP` | 禁用 HTML Escape,保持 `<` 等符号原始状态。 |
|
- `func String(v any) string` / `func StringP(v any) string`
|
||||||
| `FixedJson` | 序列化后递归处理 Key,将首字母由大写改为小写。 |
|
- `func Bool(v any) bool`
|
||||||
| `If[T]` | 泛型实现,要求 `val1` 和 `val2` 类型一致。 |
|
- `func Ints(v any) []int64` / `func Strings(v any) []string`
|
||||||
| `Duration` | 输入纯数字字符串时,默认单位为 `ms`。 |
|
- `func Duration(v string) time.Duration`
|
||||||
|
|
||||||
|
### 泛型工具
|
||||||
|
- `func If[T any](cond bool, a, b T) T`:泛型三元表达式。
|
||||||
|
- `func Switch[T any](i uint, args ...T) T`:泛型分支选择。
|
||||||
|
- `func In[T comparable](arr []T, val T) bool`:判断切片是否包含某值。
|
||||||
|
|
||||||
|
### 序列化 (JSON/YAML)
|
||||||
|
- `func Json(v any) string` / `func JsonP(v any) string` / `func JsonBytes(v any) []byte`
|
||||||
|
- `func FixedJson(v any) string`:Struct 首字母转小写。
|
||||||
|
- `func UnJson(str string, v any) any` / `func UnJsonBytes(data []byte, v any) any`
|
||||||
|
- `func Yaml(v any) string` / `func UnYaml(str string, v any) any`
|
||||||
|
|
||||||
|
### 辅助工具
|
||||||
|
- `func StringPtr(v string) *string` / `func IntPtr(v int) *int` / `func Int64Ptr(v int64) *int64` / `func BoolPtr(v bool) *bool`
|
||||||
|
- `func SplitTrim(s, sep string) []string` / `func SplitArgs(s string) []string`
|
||||||
|
- `func GetLowerName(s string) string` / `func GetUpperName(s string) string`
|
||||||
|
|
||||||
## 🧩 典型模式 (Best Practices)
|
## 🧩 典型模式 (Best Practices)
|
||||||
|
|
||||||
* **❌ 不推荐 (Standard Go)**:
|
* **✅ 推荐**:
|
||||||
```go
|
|
||||||
var status string
|
|
||||||
if ok { status = "A" } else { status = "B" }
|
|
||||||
```
|
|
||||||
* **✅ 推荐 (@go/cast)**:
|
|
||||||
```go
|
```go
|
||||||
status := cast.If(ok, "A", "B")
|
status := cast.If(ok, "A", "B")
|
||||||
```
|
age := cast.Int("18")
|
||||||
|
jsonStr := cast.Json(myStruct)
|
||||||
* **❌ 不推荐 (Standard Go)**:
|
|
||||||
```go
|
|
||||||
type Data struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
}
|
|
||||||
```
|
|
||||||
* **✅ 推荐 (@go/cast)**:
|
|
||||||
```go
|
|
||||||
type Data struct {
|
|
||||||
Name string // 直接使用大写导出
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|||||||
21
LICENSE
Normal file
21
LICENSE
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2026 ssgo
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
@ -1,3 +1,7 @@
|
|||||||
|
# 关于本项目
|
||||||
|
|
||||||
|
本项目完全由 AI 维护。代码源自 github.com/ssgo/u 的重构。
|
||||||
|
|
||||||
# @go/cast
|
# @go/cast
|
||||||
|
|
||||||
`@go/cast` 是一个为“敏捷开发”设计的 Go 基础工具库。它的设计初衷是打破 Go 语言严苛类型系统带来的繁琐摩擦,让开发者在处理数据转换时能够拥有类似 **JavaScript** 或 **PHP** 的丝滑体验。
|
`@go/cast` 是一个为“敏捷开发”设计的 Go 基础工具库。它的设计初衷是打破 Go 语言严苛类型系统带来的繁琐摩擦,让开发者在处理数据转换时能够拥有类似 **JavaScript** 或 **PHP** 的丝滑体验。
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user