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组件列表数据结构字典类型-gmap字典类型-方法介绍.md

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

New

  • 说明: New 创建并返回一个空的 AnyAnyMap。参数 safe 用于指定是否使用并发安全的 map,默认情况下为 false。

  • 格式:

New(safe ...bool) *Map
  • 示例:
func ExampleNew() {
      m := gmap.New()

      // Add data.
      m.Set("key1", "val1")

      // Print size.
      fmt.Println(m.Size())

      addMap := make(map[interface{}]interface{})
      addMap["key2"] = "val2"
      addMap["key3"] = "val3"
      addMap[1] = 1

      fmt.Println(m.Values())

      // Batch add data.
      m.Sets(addMap)

      // Gets the value of the corresponding key.
      fmt.Println(m.Get("key3"))

      // Get the value by key, or set it with given key-value if not exist.
      fmt.Println(m.GetOrSet("key4", "val4"))

      // Set key-value if the key does not exist, then return true; or else return false.
      fmt.Println(m.SetIfNotExist("key3", "val3"))

      // Remove key
      m.Remove("key2")
      fmt.Println(m.Keys())

      // Batch remove keys.
      m.Removes([]interface{}{"key1", 1})
      fmt.Println(m.Keys())

      // Contains checks whether a key exists.
      fmt.Println(m.Contains("key3"))

      // Flip exchanges key-value of the map, it will change key-value to value-key.
      m.Flip()
      fmt.Println(m.Map())

      // Clear deletes all data of the map.
      m.Clear()

      fmt.Println(m.Size())

      // May Output:
      // 1
      // [val1]
      // val3
      // val4
      // false
      // [key4 key1 key3 1]
      // [key4 key3]
      // true
      // map[val3:key3 val4:key4]
      // 0
}

NewFrom

  • 说明: NewFrom 使用给定 map 的数据创建并返回 AnyAnyMap。

  • 注意:入参 map 将被设置为底层数据映射(无深度拷贝),在外部更改 map 时,可能会同时出现一些安全问题。可选参数 safe 指定是否在并发安全中使用此结构,默认情况下为 false。

  • 格式:

NewFrom(data map[interface{}]interface{}, safe ...bool) *Map
  • 示例:
func ExampleNewFrom() {
      m := gmap.New()

      m.Set("key1", "val1")
      fmt.Println(m)

      n := gmap.NewFrom(m.MapCopy(), true)
      fmt.Println(n)

      // Output:
      // {"key1":"val1"}
      // {"key1":"val1"}
}

Iterator

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

  • 格式:

Iterator(f func(k interface{}, v interface{}) bool)
  • 示例:
func ExampleAnyAnyMap_Iterator() {
      m := gmap.New()
      for i := 0; i < 10; i++ {
          m.Set(i, i*2)
      }

      var totalKey, totalValue int
      m.Iterator(func(k interface{}, v interface{}) bool {
          totalKey += k.(int)
          totalValue += v.(int)

          return totalKey < 10
      })

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

      // May Output:
      // totalKey: 11
      // totalValue: 22
}

Clone

  • 说明: Clone 返回一个新的 AnyAnyMap,其中包含当前 map 数据的副本。

  • 格式:

Clone(safe ...bool) *AnyAnyMap
  • 示例:
func ExampleAnyAnyMap_Clone() {
      m := gmap.New()

      m.Set("key1", "val1")
      fmt.Println(m)

      n := m.Clone()
      fmt.Println(n)

      // Output:
      // {"key1":"val1"}
      // {"key1":"val1"}
}

Map

  • 说明: Map 返回底层数据 map。

  • 注意:如果在并发安全中,它将返回底层数据的副本,否则返回指向底层数据的指针。

  • 格式:

Map() map[interface{}]interface{}
  • 示例:
func ExampleAnyAnyMap_Map() {
      // non concurrent-safety, a pointer to the underlying data
      m1 := gmap.New()
      m1.Set("key1", "val1")
      fmt.Println("m1:", m1)

      n1 := m1.Map()
      fmt.Println("before n1:", n1)
      m1.Set("key1", "val2")
      fmt.Println("after n1:", n1)

      // concurrent-safety, copy of underlying data
      m2 := gmap.New(true)
      m2.Set("key1", "val1")
      fmt.Println("m1:", m2)

      n2 := m2.Map()
      fmt.Println("before n2:", n2)
      m2.Set("key1", "val2")
      fmt.Println("after n2:", n2)

      // Output:
      // m1: {"key1":"val1"}
      // before n1: map[key1:val1]
      // after n1: map[key1:val2]
      // m1: {"key1":"val1"}
      // before n2: map[key1:val1]
      // after n2: map[key1:val1]
}

MapCopy

  • 说明: MapCopy 返回 map 的数据的副本。

  • 格式:

MapCopy() map[interface{}]interface{}
  • 示例:
func ExampleAnyAnyMap_MapCopy() {
      m := gmap.New()

      m.Set("key1", "val1")
      m.Set("key2", "val2")
      fmt.Println(m)

      n := m.MapCopy()
      fmt.Println(n)

      // Output:
      // {"key1":"val1","key2":"val2"}
      // map[key1:val1 key2:val2]
}

MapStrAny

  • 说明: MapStrAny 以 map[string]interface{} 的形式返回 map 的数据的副本。

  • 格式:

MapStrAny() map[string]interface{}
  • 示例:
func ExampleAnyAnyMap_MapStrAny() {
      m := gmap.New()
      m.Set(1001, "val1")
      m.Set(1002, "val2")

      n := m.MapStrAny()
      fmt.Println(n)

      // Output:
      // map[1001:val1 1002:val2]
}

FilterEmpty

  • 说明: FilterEmpty 删除值为空的所有键值对。如: 0, nil, false, "", len(slice/map/chan) == 0 这样的值被认为是空的。

  • 格式:

FilterEmpty()
  • 示例:
func ExampleAnyAnyMap_FilterEmpty() {
      m := gmap.NewFrom(g.MapAnyAny{
          "k1": "",
          "k2": nil,
          "k3": 0,
          "k4": 1,
      })
      m.FilterEmpty()
      fmt.Println(m.Map())

      // Output:
      // map[k4:1]
}

FilterNil

  • 说明: FilterNil 删除其值为 nil 的所有键值对。

  • 格式:

FilterNil()
  • 示例:
func ExampleAnyAnyMap_FilterNil() {
      m := gmap.NewFrom(g.MapAnyAny{
          "k1": "",
          "k2": nil,
          "k3": 0,
          "k4": 1,
      })
      m.FilterNil()
      fmt.Println(m.Map())

      // May Output:
      // map[k1: k3:0 k4:1]
}

Set

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

  • 格式:

Set(key interface{}, value interface{})
  • 示例:
func ExampleAnyAnyMap_Set() {
      m := gmap.New()

      m.Set("key1", "val1")
      fmt.Println(m)

      // Output:
      // {"key1":"val1"}
}

Sets

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

  • 格式:

Sets(data map[interface{}]interface{})
  • 示例:
func ExampleAnyAnyMap_Sets() {
      m := gmap.New()

      addMap := make(map[interface{}]interface{})
      addMap["key1"] = "val1"
      addMap["key2"] = "val2"
      addMap["key3"] = "val3"

      m.Sets(addMap)
      fmt.Println(m)

      // Output:
      // {"key1":"val1","key2":"val2","key3":"val3"}
}

Search

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

  • 格式:

Search(key interface{}) (value interface{}, found bool)
  • 示例:
func ExampleAnyAnyMap_Search() {
      m := gmap.New()

      m.Set("key1", "val1")

      value, found := m.Search("key1")
      if found {
          fmt.Println("find key1 value:", value)
      }

      value, found = m.Search("key2")
      if !found {
          fmt.Println("key2 not find")
      }

      // Output:
      // find key1 value: val1
      // key2 not find
}

Get

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

  • 格式:

Get(key interface{}) (value interface{})
  • 示例:
func ExampleAnyAnyMap_Get() {
      m := gmap.New()

      m.Set("key1", "val1")

      fmt.Println("key1 value:", m.Get("key1"))
      fmt.Println("key2 value:", m.Get("key2"))

      // Output:
      // key1 value: val1
      // key2 value: <nil>
}

Pop

  • 说明: Pop 从 map 中随机取出返回一个键值对,并在内部删除该键值对。

  • 格式:

Pop() (key, value interface{})
  • 示例:
func ExampleAnyAnyMap_Pop() {
      var m gmap.Map
      m.Sets(g.MapAnyAny{
          "k1": "v1",
          "k2": "v2",
          "k3": "v3",
          "k4": "v4",
      })

      fmt.Println(m.Pop())

      // May Output:
      // k1 v1
}

Pops

  • 说明: Pops 从 map 中随机取出并删除 size 个键值对。如果 size == -1,则删除并返回所有键值对。

  • 格式:

Pops(size int) map[interface{}]interface{}
  • 示例:
func ExampleAnyAnyMap_Pops() {
      var m gmap.Map
      m.Sets(g.MapAnyAny{
          "k1": "v1",
          "k2": "v2",
          "k3": "v3",
          "k4": "v4",
      })
      fmt.Println(m.Pops(-1))
      fmt.Println("size:", m.Size())

      m.Sets(g.MapAnyAny{
          "k1": "v1",
          "k2": "v2",
          "k3": "v3",
          "k4": "v4",
      })
      fmt.Println(m.Pops(2))
      fmt.Println("size:", m.Size())

      // May Output:
      // map[k1:v1 k2:v2 k3:v3 k4:v4]
      // size: 0
      // map[k1:v1 k2:v2]
      // size: 2
}

GetOrSet

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

  • 格式:

GetOrSet(key interface{}, value interface{}) interface{}
  • 示例:
func ExampleAnyAnyMap_GetOrSet() {
      m := gmap.New()
      m.Set("key1", "val1")

      fmt.Println(m.GetOrSet("key1", "NotExistValue"))
      fmt.Println(m.GetOrSet("key2", "val2"))

      // Output:
      // val1
      // val2
}

GetOrSetFunc

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

  • 格式:

GetOrSetFunc(key interface{}, f func() interface{}) interface{}
  • 示例:
func ExampleAnyAnyMap_GetOrSetFunc() {
      m := gmap.New()
      m.Set("key1", "val1")

      fmt.Println(m.GetOrSetFunc("key1", func() interface{} {
          return "NotExistValue"
      }))
      fmt.Println(m.GetOrSetFunc("key2", func() interface{} {
          return "NotExistValue"
      }))

      // Output:
      // val1
      // NotExistValue
}

GetOrSetFuncLock

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

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

  • 格式:

GetOrSetFuncLock(key interface{}, f func() interface{}) interface{}
  • 示例:
func ExampleAnyAnyMap_GetOrSetFuncLock() {
      m := gmap.New()
      m.Set("key1", "val1")

      fmt.Println(m.GetOrSetFuncLock("key1", func() interface{} {
          return "NotExistValue"
      }))
      fmt.Println(m.GetOrSetFuncLock("key2", func() interface{} {
          return "NotExistValue"
      }))

      // Output:
      // val1
      // NotExistValue
}

GetVar

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

  • 格式:

GetVar(key interface{}) *gvar.Var
  • 示例:
func ExampleAnyAnyMap_GetVar() {
      m := gmap.New()
      m.Set("key1", "val1")

      fmt.Println(m.GetVar("key1"))
      fmt.Println(m.GetVar("key2").IsNil())

      // Output:
      // val1
      // true
}

GetVarOrSet

  • 说明: GetVarOrSet 根据键名 key 查询并返回对应的键值。当对应的键值不存在时,使用 value 设置该键值,并返回查询/设置的键值。键值使用泛型类型 *gvar.Var 返回。

  • 格式:

GetVarOrSet(key interface{}, value interface{}) *gvar.Var
  • 示例:
func ExampleAnyAnyMap_GetVarOrSet() {
      m := gmap.New()
      m.Set("key1", "val1")

      fmt.Println(m.GetVarOrSet("key1", "NotExistValue"))
      fmt.Println(m.GetVarOrSet("key2", "val2"))

      // Output:
      // val1
      // val2
}

GetVarOrSetFunc

  • 说明: GetVarOrSetFunc 根据键名 key 查询并返回对应的键值。当对应的键值不存在时,使用 func f 的返回值设置该键值,并返回查询/设置的键值。键值使用泛型类型 *gvar.Var 返回。

  • 格式:

GetVarOrSetFunc(key interface{}, f func() interface{}) *gvar.Var
  • 示例:
func ExampleAnyAnyMap_GetVarOrSetFunc() {
      m := gmap.New()
      m.Set("key1", "val1")

      fmt.Println(m.GetVarOrSetFunc("key1", func() interface{} {
          return "NotExistValue"
      }))
      fmt.Println(m.GetVarOrSetFunc("key2", func() interface{} {
          return "NotExistValue"
      }))

      // Output:
      // val1
      // NotExistValue
}

GetVarOrSetFuncLock

  • 说明: GetVarOrSetFuncLock 根据键名 key 查询并返回对应的键值。当对应的键值不存在时,使用 func f 的返回值设置该键值,并返回查询/设置的键值。键值使用泛型类型 *gvar.Var 返回。

  • 注意: GetVarOrSetFuncLock 与 GetVarOrSetFunc 函数的不同之处在于它在写锁中执行函数 f。即当有多个 goroutine 同时调用该方法时,函数 f 将会在执行之前被阻塞。

  • 格式:

GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gvar.Var
  • 示例:
func ExampleAnyAnyMap_GetVarOrSetFuncLock() {
      m := gmap.New()
      m.Set("key1", "val1")

      fmt.Println(m.GetVarOrSetFuncLock("key1", func() interface{} {
          return "NotExistValue"
      }))
      fmt.Println(m.GetVarOrSetFuncLock("key2", func() interface{} {
          return "NotExistValue"
      }))

      // Output:
      // val1
      // NotExistValue
}

SetIfNotExist

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

  • 格式:

SetIfNotExist(key interface{}, value interface{}) bool
  • 示例:
func ExampleAnyAnyMap_SetIfNotExist() {
      var m gmap.Map
      fmt.Println(m.SetIfNotExist("k1", "v1"))
      fmt.Println(m.SetIfNotExist("k1", "v1"))
      fmt.Println(m.Map())

      // Output:
      // true
      // false
      // map[k1:v1]
}

SetIfNotExistFunc

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

  • 格式:

SetIfNotExistFunc(key interface{}, f func() interface{}) bool
  • 示例:
func ExampleAnyAnyMap_SetIfNotExistFunc() {
      var m gmap.Map
      fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} {
          return "v1"
      }))
      fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} {
          return "v1"
      }))
      fmt.Println(m.Map())

      // Output:
      // true
      // false
      // map[k1:v1]
}

SetIfNotExistFuncLock

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

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

  • 格式:

SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool
  • 示例:
func ExampleAnyAnyMap_SetIfNotExistFuncLock() {
      var m gmap.Map
      fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} {
          return "v1"
      }))
      fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} {
          return "v1"
      }))
      fmt.Println(m.Map())

      // Output:
      // true
      // false
      // map[k1:v1]
}

Remove

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

  • 格式:

Remove(key interface{}) (value interface{})
  • 示例:
func ExampleAnyAnyMap_Remove() {
      var m gmap.Map
      m.Set("k1", "v1")

      fmt.Println(m.Remove("k1"))
      fmt.Println(m.Remove("k2"))

      // Output:
      // v1
      // <nil>
}

Removes

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

  • 格式:

Removes(keys []interface{})
  • 示例:
func ExampleAnyAnyMap_Removes() {
      var m gmap.Map
      m.Sets(g.MapAnyAny{
          "k1": "v1",
          "k2": "v2",
          "k3": "v3",
          "k4": "v4",
      })

      removeList := make([]interface{}, 2)
      removeList = append(removeList, "k1")
      removeList = append(removeList, "k2")

      m.Removes(removeList)

      fmt.Println(m.Map())

      // Output:
      // map[k3:v3 k4:v4]
}

Keys

  • 说明: Keys 将 map 的所有 key 作为 slice 返回。

  • 格式:

Keys() []interface{}
  • 示例:
func ExampleAnyAnyMap_Keys() {
      var m gmap.Map
      m.Sets(g.MapAnyAny{
          "k1": "v1",
          "k2": "v2",
          "k3": "v3",
          "k4": "v4",
      })
      fmt.Println(m.Keys())

      // Output:
      // [k1 k2 k3 k4]
}

Values

  • 说明: Values 将 map 的所有 value 作为 slice 返回。

  • 格式:

Values() []interface{}
  • 示例:
func ExampleAnyAnyMap_Values() {
      var m gmap.Map
      m.Sets(g.MapAnyAny{
          "k1": "v1",
          "k2": "v2",
          "k3": "v3",
          "k4": "v4",
      })
      fmt.Println(m.Values())

      // May Output:
      // [v1 v2 v3 v4]
}

Contains

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

  • 注意:键名类型为 interface{},因此匹配判断需要保证类型和数值一致。

  • 格式:

Contains(key interface{}) bool
  • 示例:
func ExampleAnyAnyMap_Contains() {
      var m gmap.Map
      m.Sets(g.MapAnyAny{
          "k1": "v1",
          "k2": "v2",
          "k3": "v3",
          "k4": "v4",
      })
      fmt.Println(m.Contains("k1"))
      fmt.Println(m.Contains("k5"))

      // Output:
      // true
      // false
}

Size

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

  • 格式:

Size() int
  • 示例:
func ExampleAnyAnyMap_Size() {
      var m gmap.Map
      m.Sets(g.MapAnyAny{
          "k1": "v1",
          "k2": "v2",
          "k3": "v3",
          "k4": "v4",
      })

      fmt.Println(m.Size())

      // Output:
      // 4
}

IsEmpty

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

  • 格式:

IsEmpty() bool
  • 示例:
func ExampleAnyAnyMap_IsEmpty() {
      var m gmap.Map
      fmt.Println(m.IsEmpty())

      m.Set("k1", "v1")
      fmt.Println(m.IsEmpty())

      // Output:
      // true
      // false
}

Clear

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

  • 格式:

Clear()
  • 示例:
func ExampleAnyAnyMap_Clear() {
      var m gmap.Map
      m.Sets(g.MapAnyAny{
          "k1": "v1",
          "k2": "v2",
          "k3": "v3",
          "k4": "v4",
      })

      m.Clear()

      fmt.Println(m.Map())

      // Output:
      // map[]
}

Replace

  • 说明: Replace 用给定的 data 完整替换 map 的 value。

  • 格式:

Replace(data map[interface{}]interface{})
  • 示例:
func ExampleAnyAnyMap_Replace() {
      var m gmap.Map
      m.Sets(g.MapAnyAny{
          "k1": "v1",
      })

      var n gmap.Map
      n.Sets(g.MapAnyAny{
          "k2": "v2",
      })

      fmt.Println(m.Map())

      m.Replace(n.Map())
      fmt.Println(m.Map())

      n.Set("k2", "v1")
      fmt.Println(m.Map())

      // Output:
      // map[k1:v1]
      // map[k2:v2]
      // map[k2:v1]
}

LockFunc

  • 说明: LockFunc 在写锁中执行函数 f。

  • 格式:

LockFunc(f func(m map[interface{}]interface{}))
  • 示例:
func ExampleAnyAnyMap_LockFunc() {
      var m gmap.Map
      m.Sets(g.MapAnyAny{
          "k1": 1,
          "k2": 2,
          "k3": 3,
          "k4": 4,
      })

      m.LockFunc(func(m map[interface{}]interface{}) {
          totalValue := 0
          for _, v := range m {
              totalValue += v.(int)
          }
          fmt.Println("totalValue:", totalValue)
      })

      // Output:
      // totalValue: 10
}

RLockFunc

  • 说明: RLockFunc 在读锁中执行函数 f。

  • 格式:

RLockFunc(f func(m map[interface{}]interface{}))
  • 示例:
func ExampleAnyAnyMap_RLockFunc() {
      var m gmap.Map
      m.Sets(g.MapAnyAny{
          "k1": 1,
          "k2": 2,
          "k3": 3,
          "k4": 4,
      })

      m.RLockFunc(func(m map[interface{}]interface{}) {
          totalValue := 0
          for _, v := range m {
              totalValue += v.(int)
          }
          fmt.Println("totalValue:", totalValue)
      })

      // Output:
      // totalValue: 10
}

Flip

  • 说明: Flip 将 map 的 key 与 value 进行交换。

  • 格式:

Flip()
  • 示例:
func ExampleAnyAnyMap_Flip() {
      var m gmap.Map
      m.Sets(g.MapAnyAny{
          "k1": "v1",
      })
      m.Flip()
      fmt.Println(m.Map())

      // Output:
      // map[v1:k1]
}

Merge

  • 说明: Merge 合并两个AnyAnyMap。入参 map 将合并到原 map 中。

  • 格式:

Merge(other *AnyAnyMap)
  • 示例:
func ExampleAnyAnyMap_Merge() {
      var m1, m2 gmap.Map
      m1.Set("key1", "val1")
      m2.Set("key2", "val2")
      m1.Merge(&m2)
      fmt.Println(m1.Map())

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

String

  • 说明: String 以字符串形式返回 map。

  • 格式:

String() string
  • 示例:
func ExampleAnyAnyMap_String() {
      var m gmap.Map
      m.Sets(g.MapAnyAny{
          "k1": "v1",
      })

      fmt.Println(m.String())

      // Output:
      // {"k1":"v1"}
}

MarshalJSON

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

  • 格式:

MarshalJSON() ([]byte, error)
  • 示例:
func ExampleAnyAnyMap_MarshalJSON() {
      var m gmap.Map
      m.Sets(g.MapAnyAny{
          "k1": "v1",
          "k2": "v2",
          "k3": "v3",
          "k4": "v4",
      })

      bytes, err := m.MarshalJSON()
      if err == nil {
          fmt.Println(gconv.String(bytes))
      }

      // Output:
      // {"k1":"v1","k2":"v2","k3":"v3","k4":"v4"}
}

UnmarshalJSON

  • 说明: UnmarshalJSON 实现了 json.Unmarshal 的接口。

  • 格式:

UnmarshalJSON(b []byte) error
  • 示例:
func ExampleAnyAnyMap_UnmarshalJSON() {
      var m gmap.Map
      m.Sets(g.MapAnyAny{
          "k1": "v1",
          "k2": "v2",
          "k3": "v3",
          "k4": "v4",
      })

      var n gmap.Map

      err := n.UnmarshalJSON(gconv.Bytes(m.String()))
      if err == nil {
          fmt.Println(n.Map())
      }

      // Output:
      // map[k1:v1 k2:v2 k3:v3 k4:v4]
}

UnmarshalValue

  • 说明: UnmarshalValue 是一个接口实现,它通过任意类型的变量初始化当前 map。

  • 格式:

UnmarshalValue(value interface{}) (err error)
  • 示例:
func ExampleAnyAnyMap_UnmarshalValue() {
      var m gmap.Map
      m.Sets(g.MapAnyAny{
          "k1": "v1",
          "k2": "v2",
          "k3": "v3",
          "k4": "v4",
      })

      var n gmap.Map
      err := n.UnmarshalValue(m.String())
      if err == nil {
          fmt.Println(n.Map())
      }
      // Output:
      // map[k1:v1 k2:v2 k3:v3 k4:v4]
}

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.