All skills
gogf avatar

/goframe-v2

@911705d
by GoFramegogf/skills81 stars
3

GoFrame v2 development skill. Use only when the target Go project uses or is explicitly adopting GoFrame v2: the nearest go.mod requires github.com/gogf/gf/v2, existing Go files import github.com/gogf/gf/v2 or any github.com/gogf/gf/v2/... component package, or the user asks to scaffold, migrate, or build with GoFrame. Trigger for GoFrame-backed Go work such as APIs/controllers/services, middleware, routing/config, ORM/DAO/DO/entity/database operations, gf CLI/codegen, HTTP/gRPC services, and microservice conventions. Do not trigger for generic Go projects without GoFrame evidence, frontend-only work, shell scripts, or unrelated infrastructure tasks.

Use this Skill: https://skilld.dev/gh/gogf/skills/goframe-v2

This session only. Nothing lands on disk.

references组件列表数据结构树形类型-gtree树形类型-方法介绍.md

:::tip 以下常用方法列表,文档更新可能滞后于代码新特性,更多的方法及示例请参考代码文档: https://pkg.go.dev/github.com/gogf/gf/v2/container/gtree :::

NewBTree

  • 说明: NewBTree 使用 m(最大子节点数量)和自定义的比较方法创建 BTree。参数 safe 用于指定是否使用并发安全的 tree,默认情况下为 false。

  • 注意:参数 m 必须大于等于 3,否则会 panic。

  • 格式:

NewBTree(m int, comparator func(v1, v2 interface{}) int, safe ...bool) *BTree
  • 示例:
func ExampleNewBTree() {
      bTree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 6; i++ {
          bTree.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }
      fmt.Println(bTree.Map())

      // Output:
      // map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
}

NewBTreeFrom

  • 说明: NewBTreeFrom 使用 m(最大子节点数量),自定义的比较方法和类型为 map[interface{}]interface{} 的 data 创建 BTree。参数 safe 用于指定是否使用并发安全的 tree,默认情况下为 false。

  • 注意:参数 m 必须大于等于 3,否则会 panic。

  • 格式:

NewBTreeFrom(m int, comparator func(v1, v2 interface{}) int, data map[interface{}]interface{}, safe ...bool) *BTree
  • 示例:
func ExampleNewBTreeFrom() {
      bTree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 6; i++ {
          bTree.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }

      otherBTree := gtree.NewBTreeFrom(3, gutil.ComparatorString, bTree.Map())
      fmt.Println(otherBTree.Map())

      // Output:
      // map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
}

Clone

  • 说明: clone 返回一个值为当前 tree 值的副本的新 BTree。

  • 格式:

Clone() *BTree
  • 示例:
func ExampleBTree_Clone() {
      b := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 6; i++ {
          b.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }

      tree := b.Clone()

      fmt.Println(tree.Map())
      fmt.Println(tree.Size())

      // Output:
      // map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
      // 6
}

Set

  • 说明: Set 为 tree 设置 key/value。

  • 格式:

Set(key interface{}, value interface{})
  • 示例:
func ExampleBTree_Set() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 6; i++ {
          tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }

      fmt.Println(tree.Map())
      fmt.Println(tree.Size())

      // Output:
      // map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
      // 6
}

Sets

  • 说明: Sets 为 tree 批量设置 key/value。

  • 格式:

Sets(data map[interface{}]interface{})
  • 示例:
func ExampleBTree_Sets() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)

      tree.Sets(map[interface{}]interface{}{
          "key1": "val1",
          "key2": "val2",
      })

      fmt.Println(tree.Map())
      fmt.Println(tree.Size())

      // Output:
      // map[key1:val1 key2:val2]
      // 2
}

Get

  • 说明: Get 返回参数 key 对应的值 value,如 key 不存在,则返回 Nil。

  • 格式:

Get(key interface{}) (value interface{})
  • 示例:
func ExampleBTree_Get() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 6; i++ {
          tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }

      fmt.Println(tree.Get("key1"))
      fmt.Println(tree.Get("key10"))

      // Output:
      // val1
      // <nil>
}

GetOrSet

  • 说明: GetOrSet 如 key 存在,则返回 value,如 key 不存在,使用 key 和 value 设置键值,然后返回该值。

  • 格式:

GetOrSet(key interface{}, value interface{}) interface{}
  • 示例:
func ExampleBTree_GetOrSet() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 6; i++ {
          tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }

      fmt.Println(tree.GetOrSet("key1", "newVal1"))
      fmt.Println(tree.GetOrSet("key6", "val6"))

      // Output:
      // val1
      // val6
}

GetOrSetFunc

  • 说明: GetOrSetFunc 如 key 存在,则返回 value,如 key 不存在,使用 key 和 func f 的返回值设置键值,然后返回该值。

  • 格式:

GetOrSetFunc(key interface{}, f func() interface{}) interface{}
  • 示例:
func ExampleBTree_GetOrSetFunc() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 6; i++ {
          tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }

      fmt.Println(tree.GetOrSetFunc("key1", func() interface{} {
          return "newVal1"
      }))
      fmt.Println(tree.GetOrSetFunc("key6", func() interface{} {
          return "val6"
      }))

      // Output:
      // val1
      // val6
}

GetOrSetFuncLock

  • 说明: GetOrSetFunc 如 key 存在,则返回 value,如 key 不存在,使用 key 和 func f 的返回值设置键值,然后返回该值。

  • 注意: GetOrSetFuncLock 与 GetOrSetFunc 函数的不同之处在于它在写锁中执行函数 f。

  • 格式:

GetOrSetFuncLock(key interface{}, f func() interface{}) interface{}
  • 示例:
func ExampleBTree_GetOrSetFuncLock() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 6; i++ {
          tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }

      fmt.Println(tree.GetOrSetFuncLock("key1", func() interface{} {
          return "newVal1"
      }))
      fmt.Println(tree.GetOrSetFuncLock("key6", func() interface{} {
          return "val6"
      }))

      // Output:
      // val1
      // val6
}

GetVar

  • 说明: GetVar 根据键名 key 查询并返回对应的键值,类型为 *gvar.Var。

  • 注意:返回的 gvar.Var 是非并发安全的。

  • 格式:

GetVar(key interface{}) *gvar.Var
  • 示例:
func ExampleBTree_GetVar() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 6; i++ {
          tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }

      fmt.Println(tree.GetVar("key1").String())

      // Output:
      // val1
}

GetVarOrSet

  • 说明: GetVarOrSet 使用 GetOrSet 的结果返回,类型为 *gvar.Var。

  • 注意:返回的 gvar.Var 是非并发安全的。

  • 格式:

GetVarOrSet(key interface{}, value interface{}) *gvar.Var
  • 示例:
func ExampleBTree_GetVarOrSet() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 6; i++ {
          tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }

      fmt.Println(tree.GetVarOrSet("key1", "newVal1"))
      fmt.Println(tree.GetVarOrSet("key6", "val6"))

      // Output:
      // val1
      // val6
}

GetVarOrSetFunc

  • 说明: GetVarOrSetFunc 使用 GetOrSetFunc 的结果返回,类型为 *gvar.Var。
  • 注意:返回的 gvar.Var 是非并发安全的。
  • 格式:
GetVarOrSetFunc(key interface{}, f func() interface{}) *gvar.Var
  • 示例:
func ExampleBTree_GetVarOrSetFunc() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 6; i++ {
          tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }

      fmt.Println(tree.GetVarOrSetFunc("key1", func() interface{} {
          return "newVal1"
      }))
      fmt.Println(tree.GetVarOrSetFunc("key6", func() interface{} {
          return "val6"
      }))

      // Output:
      // val1
      // val6
}

GetVarOrSetFuncLock

  • 说明: GetVarOrSetFuncLock 使用 GetOrSetFuncLock 的结果返回,类型为 *gvar.Var。

  • 注意:返回的 gvar.Var 是非并发安全的。

  • 格式:

GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gvar.Var
  • 示例:
func ExampleBTree_GetVarOrSetFuncLock() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 6; i++ {
          tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }

      fmt.Println(tree.GetVarOrSetFuncLock("key1", func() interface{} {
          return "newVal1"
      }))
      fmt.Println(tree.GetVarOrSetFuncLock("key6", func() interface{} {
          return "val6"
      }))

      // Output:
      // val1
      // val6
}

SetIfNotExist

  • 说明:如果 key 不存在,则 SetIfNotExist 为 map 设置值键值对 key/value,并且返回 true。如果 key 存在,则返回 false,而 value 将被忽略。

  • 格式:

SetIfNotExist(key interface{}, value interface{}) bool
  • 示例:
func ExampleBTree_SetIfNotExist() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 6; i++ {
          tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }

      fmt.Println(tree.SetIfNotExist("key1", "newVal1"))
      fmt.Println(tree.SetIfNotExist("key6", "val6"))

      // Output:
      // false
      // true
}

SetIfNotExistFunc

  • 说明:如果 key 不存在,则 SetIfNotExistFunc 设置值为函数 f 的返回值,并且返回 true。如果 key 存在,则返回 false,并且 value 将被忽略。

  • 格式:

SetIfNotExistFunc(key interface{}, f func() interface{}) bool
  • 示例:
func ExampleBTree_SetIfNotExistFunc() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 6; i++ {
          tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }

      fmt.Println(tree.SetIfNotExistFunc("key1", func() interface{} {
          return "newVal1"
      }))
      fmt.Println(tree.SetIfNotExistFunc("key6", func() interface{} {
          return "val6"
      }))

      // Output:
      // false
      // true
}

SetIfNotExistFuncLock

  • 说明:如果 key 不存在,则 SetIfNotExistFunc 设置值为 func c 的返回值,然后返回 true。如果 key 存在,则返回 false,而 value 将被忽略。

  • SetIfNotExistFuncLock 与 SetIfNotExistFunc 函数的不同之处在于它在 mutex.Lock 中执行函数 f。

  • 格式:

SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool
  • 示例:
func ExampleBTree_SetIfNotExistFuncLock() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 6; i++ {
          tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }

      fmt.Println(tree.SetIfNotExistFuncLock("key1", func() interface{} {
          return "newVal1"
      }))
      fmt.Println(tree.SetIfNotExistFuncLock("key6", func() interface{} {
          return "val6"
      }))

      // Output:
      // false
      // true
}

Contains

  • 说明: Contains 检查 key 在 tree 中是否存在。如果 key 存在,则返回 true,否则返回 false。

  • 格式:

Contains(key interface{}) bool
  • 示例:
func ExampleBTree_Contains() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 6; i++ {
          tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }

      fmt.Println(tree.Contains("key1"))
      fmt.Println(tree.Contains("key6"))

      // Output:
      // true
      // false
}

Remove

  • 说明:按给定的 key 从 tree 中删除 value,并返回此删除的 value。

  • 格式:

Remove(key interface{}) (value interface{})
  • 示例:
func ExampleBTree_Remove() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 6; i++ {
          tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }

      fmt.Println(tree.Remove("key1"))
      fmt.Println(tree.Remove("key6"))
      fmt.Println(tree.Map())

      // Output:
      // val1
      // <nil>
      // map[key0:val0 key2:val2 key3:val3 key4:val4 key5:val5]
}

Removes

  • 说明: Removes 按给定的 key 批量删除 tree 的 value。

  • 格式:

Removes(keys []interface{})
  • 示例:
func ExampleBTree_Removes() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 6; i++ {
          tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }

      removeKeys := make([]interface{}, 2)
      removeKeys = append(removeKeys, "key1")
      removeKeys = append(removeKeys, "key6")

      tree.Removes(removeKeys)

      fmt.Println(tree.Map())

      // Output:
      // map[key0:val0 key2:val2 key3:val3 key4:val4 key5:val5]
}

IsEmpty

  • 说明: IsEmpty 检查 tree 是否为空。如果 tree 为空,则返回 true,否则返回 false。

  • 格式:

IsEmpty() bool
  • 示例:
func ExampleBTree_IsEmpty() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)

      fmt.Println(tree.IsEmpty())

      for i := 0; i < 6; i++ {
          tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }

      fmt.Println(tree.IsEmpty())

      // Output:
      // true
      // false
}

Size

  • 说明: Size 返回 tree 的大小。

  • 格式:

Size() int
  • 示例:
func ExampleBTree_Size() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)

      fmt.Println(tree.Size())

      for i := 0; i < 6; i++ {
          tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }

      fmt.Println(tree.Size())

      // Output:
      // 0
      // 6
}

Keys

  • 说明: Keys 按升序返回所有的 key。

  • 格式:

Keys() []interface{}
  • 示例:
func ExampleBTree_Keys() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 6; i > 0; i-- {
          tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }

      fmt.Println(tree.Keys())

      // Output:
      // [key1 key2 key3 key4 key5 key6]
}

Values

  • 说明: Values 按 key 的升序返回所有的 value。

  • 格式:

Values() []interface{}
  • 示例:
func ExampleBTree_Values() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 6; i > 0; i-- {
          tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }

      fmt.Println(tree.Values())

      // Output:
      // [val1 val2 val3 val4 val5 val6]
}

Map

  • 说明: Map 以 map 的形式返回所有的 key/value。

  • 格式:

Map() map[interface{}]interface{}
  • 示例:
func ExampleBTree_Map() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 6; i++ {
          tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }

      fmt.Println(tree.Map())

      // Output:
      // map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
}

MapStrAny

  • 说明: MapStrAny 以 map[string]interface{} 的形式返回所有的 key/value。

  • 格式:

MapStrAny() map[string]interface{}
  • 示例:
func ExampleBTree_MapStrAny() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 6; i++ {
          tree.Set(1000+i, "val"+gconv.String(i))
      }

      fmt.Println(tree.MapStrAny())

      // Output:
      // map[1000:val0 1001:val1 1002:val2 1003:val3 1004:val4 1005:val5]
}

Clear

  • 说明: Clear 删除 tree 的所有数据。

  • 格式:

Clear()
  • 示例:
func ExampleBTree_Clear() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 6; i++ {
          tree.Set(1000+i, "val"+gconv.String(i))
      }
      fmt.Println(tree.Size())

      tree.Clear()
      fmt.Println(tree.Size())

      // Output:
      // 6
      // 0
}

Replace

  • 说明: Replace 用类型为 map[interface{}]interface{} 的 data 替换 tree 的 key/value。

  • 格式:

Replace(data map[interface{}]interface{})
  • 示例:
func ExampleBTree_Replace() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 6; i++ {
          tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }

      fmt.Println(tree.Map())

      data := map[interface{}]interface{}{
          "newKey0": "newVal0",
          "newKey1": "newVal1",
          "newKey2": "newVal2",
      }

      tree.Replace(data)

      fmt.Println(tree.Map())

      // Output:
      // map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
      // map[newKey0:newVal0 newKey1:newVal1 newKey2:newVal2]
}

Height

  • 说明: Height 返回 tree 的高度。

  • 格式:

Height() int
  • 示例:
func ExampleBTree_Height() {
      tree := gtree.NewBTree(3, gutil.ComparatorInt)
      for i := 0; i < 100; i++ {
          tree.Set(i, i)
      }
      fmt.Println(tree.Height())

      // Output:
      // 6
}

Left

  • 说明: Left 返回最左边(最小)的类型为 *BTreeEntry 的 node,如果 tree 是空的,则返回 nil。

  • 格式:

Left() *BTreeEntry
  • 示例:
func ExampleBTree_Left() {
      tree := gtree.NewBTree(3, gutil.ComparatorInt)
      for i := 1; i < 100; i++ {
          tree.Set(i, i)
      }
      fmt.Println(tree.Left().Key, tree.Left().Value)

      emptyTree := gtree.NewBTree(3, gutil.ComparatorInt)
      fmt.Println(emptyTree.Left())

      // Output:
      // 1 1
      // <nil>
}

Right

  • 说明: Left 返回最右边(最大)的类型为 *BTreeEntry 的 node,如果 tree 是空的,则返回 nil。

  • 格式:

Right() *BTreeEntry
  • 示例:
func ExampleBTree_Right() {
      tree := gtree.NewBTree(3, gutil.ComparatorInt)
      for i := 1; i < 100; i++ {
          tree.Set(i, i)
      }
      fmt.Println(tree.Right().Key, tree.Right().Value)

      emptyTree := gtree.NewBTree(3, gutil.ComparatorInt)
      fmt.Println(emptyTree.Left())

      // Output:
      // 99 99
      // <nil>
}

String

  • 说明: String 返回 tree 的 node 显示(用于调试)。

  • 格式:

String() string
  • 示例:
func ExampleBTree_String() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 6; i++ {
          tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }

      fmt.Println(tree.String())

      // Output:
      // key0
      // key1
      //     key2
      // key3
      //     key4
      //     key5
}

Search

  • 说明: Search 使用参数 key 搜索 tree。如果找到 key,则返回其对应的键值,并且返回参数 found 为 true,否则为 false。

  • 格式:

Search(key interface{}) (value interface{}, found bool)
  • 示例:
func ExampleBTree_Search() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 6; i++ {
          tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }

      fmt.Println(tree.Search("key0"))
      fmt.Println(tree.Search("key6"))

      // Output:
      // val0 true
      // <nil> false
}

Print

  • 说明: Print 将 tree 打印到标准输出。

  • 格式:

Print()
  • 示例:
func ExampleBTree_Print() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 6; i++ {
          tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }

      tree.Print()

      // Output:
      // key0
      // key1
      //     key2
      // key3
      //     key4
      //     key5
}

Iterator

  • 说明: Iterator 等同于 IteratorAsc 。

  • 格式:

Iterator(f func(key, value interface{}) bool)
  • 示例:
func ExampleBTree_Iterator() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 10; i++ {
          tree.Set(i, 10-i)
      }

      var totalKey, totalValue int
      tree.Iterator(func(key, value interface{}) bool {
          totalKey += key.(int)
          totalValue += value.(int)

          return totalValue < 20
      })

      fmt.Println("totalKey:", totalKey)
      fmt.Println("totalValue:", totalValue)

      // Output:
      // totalKey: 3
      // totalValue: 27
}

IteratorFrom

  • 说明: IteratorFrom 等同于 IteratorAscFrom 。

  • 格式:

IteratorFrom(key interface{}, match bool, f func(key, value interface{}) bool)
  • 示例:
func ExampleBTree_IteratorFrom() {
      m := make(map[interface{}]interface{})
      for i := 1; i <= 5; i++ {
          m[i] = i * 10
      }
      tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m)

      tree.IteratorFrom(1, true, func(key, value interface{}) bool {
          fmt.Println("key:", key, ", value:", value)
          return true
      })

      // Output:
      // key: 1 , value: 10
      // key: 2 , value: 20
      // key: 3 , value: 30
      // key: 4 , value: 40
      // key: 5 , value: 50
}

IteratorAsc

  • 说明: IteratorAsc 使用自定义回调函数 f 以只读方式按升序迭代 tree。如果 f 返回 true,则继续迭代,返回 false 则停止。

  • 格式:

IteratorAsc(f func(key, value interface{}) bool)
  • 示例:
func ExampleBTree_IteratorAsc() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 10; i++ {
          tree.Set(i, 10-i)
      }

      tree.IteratorAsc(func(key, value interface{}) bool {
          fmt.Println("key:", key, ", value:", value)
          return true
      })

      // Output:
      // key: 0 , value: 10
      // key: 1 , value: 9
      // key: 2 , value: 8
      // key: 3 , value: 7
      // key: 4 , value: 6
      // key: 5 , value: 5
      // key: 6 , value: 4
      // key: 7 , value: 3
      // key: 8 , value: 2
      // key: 9 , value: 1
}

IteratorAscFrom

  • 说明: IteratorAscFrom 使用自定义回调函数 f 以只读方式按升序迭代 tree。参数 key 指定从哪个 key 开始迭代。参数 match 为 true 时,从 key 完全匹配时开始迭代,否则使用索引搜索迭代。如果 f 返回 true,则继续迭代,返回 false 则停止。

  • 格式:

IteratorAscFrom(key interface{}, match bool, f func(key, value interface{}) bool)
  • 示例:
func ExampleBTree_IteratorAscFrom_Normal() {
      m := make(map[interface{}]interface{})
      for i := 1; i <= 5; i++ {
          m[i] = i * 10
      }
      tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m)

      tree.IteratorAscFrom(1, true, func(key, value interface{}) bool {
          fmt.Println("key:", key, ", value:", value)
          return true
      })

      // Output:
      // key: 1 , value: 10
      // key: 2 , value: 20
      // key: 3 , value: 30
      // key: 4 , value: 40
      // key: 5 , value: 50
}
func ExampleBTree_IteratorAscFrom_NoExistKey() {
      m := make(map[interface{}]interface{})
      for i := 1; i <= 5; i++ {
          m[i] = i * 10
      }
      tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m)

      tree.IteratorAscFrom(0, true, func(key, value interface{}) bool {
          fmt.Println("key:", key, ", value:", value)
          return true
      })

      // Output:
}
func ExampleBTree_IteratorAscFrom_NoExistKeyAndMatchFalse() {
      m := make(map[interface{}]interface{})
      for i := 1; i <= 5; i++ {
          m[i] = i * 10
      }
      tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m)

      tree.IteratorAscFrom(0, false, func(key, value interface{}) bool {
          fmt.Println("key:", key, ", value:", value)
          return true
      })

      // Output:
      // key: 1 , value: 10
      // key: 2 , value: 20
      // key: 3 , value: 30
      // key: 4 , value: 40
      // key: 5 , value: 50
}

IteratorDesc

  • 说明: IteratorDesc 使用自定义回调函数 f 以只读方式按降序迭代 tree。如果 f 返回 true,则继续迭代,返回 false 则停止。

  • 格式:

IteratorDesc(f func(key, value interface{}) bool)
  • 示例:
func ExampleBTree_IteratorDesc() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 10; i++ {
          tree.Set(i, 10-i)
      }

      tree.IteratorDesc(func(key, value interface{}) bool {
          fmt.Println("key:", key, ", value:", value)
          return true
      })

      // Output:
      // key: 9 , value: 1
      // key: 8 , value: 2
      // key: 7 , value: 3
      // key: 6 , value: 4
      // key: 5 , value: 5
      // key: 4 , value: 6
      // key: 3 , value: 7
      // key: 2 , value: 8
      // key: 1 , value: 9
      // key: 0 , value: 10
}

IteratorDescFrom

  • 说明: IteratorDescFrom 使用自定义回调函数 f 以只读方式按降序迭代 tree。参数 key 指定从哪个 key 开始迭代。参数 match 为 true 时,从 key 完全匹配时开始迭代,否则使用索引搜索迭代。如果 f 返回 true,则继续迭代,返回 false 则停止。

  • 格式:

IteratorDescFrom(key interface{}, match bool, f func(key, value interface{}) bool)
  • 示例:
func ExampleBTree_IteratorDescFrom() {
      m := make(map[interface{}]interface{})
      for i := 1; i <= 5; i++ {
          m[i] = i * 10
      }
      tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m)

      tree.IteratorDescFrom(5, true, func(key, value interface{}) bool {
          fmt.Println("key:", key, ", value:", value)
          return true
      })

      // Output:
      // key: 5 , value: 50
      // key: 4 , value: 40
      // key: 3 , value: 30
      // key: 2 , value: 20
      // key: 1 , value: 10
}

MarshalJson

  • 说明: MarshalJSON 实现 json.Marshal 的接口。

  • 格式:

MarshalJSON() ([]byte, error)
  • 示例:
func ExampleBTree_MarshalJSON() {
      tree := gtree.NewBTree(3, gutil.ComparatorString)
      for i := 0; i < 6; i++ {
          tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
      }

      bytes, err := json.Marshal(tree)
      if err == nil {
          fmt.Println(gconv.String(bytes))
      }

      // Output:
      // {"key0":"val0","key1":"val1","key2":"val2","key3":"val3","key4":"val4","key5":"val5"}
}

Source: SKILL.md on GitHub

1 alert16d5 checks · Risk MEDIUM
  • Gen Agent Trust Hub16d

    The skill provides a comprehensive development guide and code examples for the GoFrame v2 framework. While highly informative for Go developers, it contains several examples with hardcoded placeholder secrets, tokens, and specific local file paths to sensitive configuration files (like Kubernetes credentials). These elements are provided for demonstration purposes but represent insecure practices if used in production without modification.

  • Socket16d

    5 alerts: gptSecurity, gptAnomaly

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    227/949 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 911705d. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 4 months ago.

Steadyupdated 4 months ago
  • Go
  • Database
  • goframe
  • orm
  • http
  • grpc
  • microservices
  • crud
  • scaffolding
  • middleware

README badge

README badge for gogf/skills

Provides instruction and conventions for developing with GoFrame v2, a modular Go framework for building HTTP and gRPC services, microservices, and database operations using its ORM, CLI tooling, and project scaffolding. Use this skill when the target project has GoFrame v2 as a dependency or when the user requests scaffolding, migrations, or service development with GoFrame conventions.

Generated from the current SKILL.md.

When should I use this skill?
Use this skill only when your Go project uses GoFrame v2, indicated by github.com/gogf/gf/v2 in go.mod or imports. Do not use for generic Go projects, frontend work, or infrastructure tasks unrelated to GoFrame.
Should I manually set created_at and updated_at fields?
No. GoFrame automatically writes created_at on insert and updated_at on insert/update/save. Manually setting these fields is redundant and violates project conventions.
What should I use for database operations instead of g.Map?
Always use DO objects from internal/model/do/. DO struct fields are interface{}, unset fields remain nil and are automatically ignored by the ORM.
How does soft delete work in GoFrame?
Call Delete() on the DAO; GoFrame automatically converts it to UPDATE SET deleted_at = NOW(). Queries automatically filter out soft-deleted rows without requiring manual WhereNull conditions.
Where should I implement business logic?
Implement business logic directly in the service/ directory. Do not use the logic/ directory unless explicitly requested.

Generated from the current SKILL.md. These answers refresh after source changes.