feat: 支持切片追加及 -1 头部插入

This commit is contained in:
2025-12-04 11:28:18 +08:00
parent cbe079ddcd
commit b373dde7f7
4 changed files with 139 additions and 8 deletions

View File

@@ -57,11 +57,17 @@ func ExampleNew_withPointer_slice() {
rfx.Set("0", "orange")
rfx.Set("2", "grape")
// 使用 Append 追加元素
rfx.Append("kiwi")
// 使用索引 -1 在切片前面插入新元素
rfx.Set("-1", "pear")
// 原始 slice 已被修改
fmt.Printf("Items: %v\n", items)
// Output:
// Items: [orange banana grape]
// Items: [pear orange banana grape kiwi]
}
// ExampleNew_withValue 演示如何使用 New 函数传入值
@@ -158,19 +164,27 @@ func ExampleNew_withValue_slice() {
rfx.Set("0", "orange")
rfx.Set("2", "grape")
// 使用 Append 追加元素
rfx.Append("kiwi")
// 使用索引 -1 在切片前面插入新元素
rfx.Set("-1", "pear")
// 原始 slice 未被修改
fmt.Printf("Original: %v\n", items)
// rfx 内部的数据已修改
fmt.Printf("Clone: [%s %s %s]\n",
fmt.Printf("Clone: [%s %s %s %s %s]\n",
rfx.Get("0").String(),
rfx.Get("1").String(),
rfx.Get("2").String())
rfx.Get("2").String(),
rfx.Get("3").String(),
rfx.Get("4").String())
// Output:
// After New: [apple banana cherry]
// Original: [apple banana cherry]
// Clone: [orange banana grape]
// Clone: [pear orange banana grape kiwi]
}
// ExampleNew_withValue_nestedStruct 演示传入嵌套结构体值