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

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

Append

  • 说明:向数组的尾部追加数据,可以添加任意数量字符串。Append的方法是PushRight的别名
  • 格式:
Append(value ...string) *StrArray
  • 示例:建立一个空数组,设置完数据后,并在数组尾部添加新的数据。
func ExampleStrArray_Append() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"We", "are", "GF", "fans"})
      s.Append("a", "b", "c")
      fmt.Println(s)

      // Output:
      // ["We","are","GF","fans","a","b","c"]
}

At

  • 说明:返回数组指定索引的数据
  • 格式:
At(index int) (value string)
  • 示例:建立一个数组,找到index为2的数据。
func ExampleStrArray_At() {
      s := garray.NewStrArrayFrom(g.SliceStr{"We", "are", "GF", "fans", "!"})
      sAt := s.At(2)
      fmt.Println(sAt)

      // Output:
      // GF
}

Chunk

  • 说明:把指定数组按指定的大小Size,分割成多个数组,返回值为[][]string。最后一个数组包含数据的数量可能小于Size
  • 格式:
Chunk(size int) [][]string
  • 示例:建立一个数组,并将该数组分割成3个数组。
func ExampleStrArray_Chunk() {
      s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
      r := s.Chunk(3)
      fmt.Println(r)

      // Output:
      // [[a b c] [d e f] [g h]]
}

Clear

  • 说明:删除当前数组中所有的数据
  • 格式:
Clear() *StrArray
  • 示例:建立一个空数组,赋值后,并删除该数组的数据。
func ExampleStrArray_Clear() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
      fmt.Println(s)
      fmt.Println(s.Clear())
      fmt.Println(s)

      // Output:
      // ["a","b","c","d","e","f","g","h"]
      // []
      // []
}

Clone

  • 说明:克隆当前的数组。返回一个与当前数组相同的数组拷贝
  • 格式:
Clone() (newArray *StrArray)
  • 示例:建立一个空数组,赋值后,克隆出一个新数组。
func ExampleStrArray_Clone() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
      r := s.Clone()
      fmt.Println(r)
      fmt.Println(s)

      // Output:
      // ["a","b","c","d","e","f","g","h"]
      // ["a","b","c","d","e","f","g","h"]
}

Contains

  • 说明:判断一个数组是否包含给定的String值。字符串严格区分大小写。返回值为bool
  • 格式:
Contains(value string) bool
  • 示例:建立一个空数组,设置完数据后,判断是否包含指定数据e和z
func ExampleStrArray_Contains() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
      fmt.Println(s.Contains("e"))
      fmt.Println(s.Contains("z"))

      // Output:
      // true
      // false
}

ContainsI

  • 说明:判断一个数组是否包含给定的 String 值。字符串不区分大小写。返回值为 bool
  • 格式:
ContainsI(value string) bool
  • 示例:建立一个空数组,设置完数据后,判断是否包含指定数据 E 和 z
func ExampleStrArray_ContainsI() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
      fmt.Println(s.ContainsI("E"))
      fmt.Println(s.ContainsI("z"))

      // Output:
      // true
      // false
}

CountValues

  • 说明:统计每个值在数组中出现的次数。返回值为 map[string]int
  • 格式:
CountValues() map[string]int
  • 示例:建立一个数组,统计数组中每个字符串包含的个数
func ExampleStrArray_CountValues() {
      s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "c", "c", "d", "d"})
      fmt.Println(s.CountValues())

      // Output:
      // map[a:1 b:1 c:3 d:2]
}

Fill

  • 说明:在数组中指定的开始位置 startIndex,用指定的 value 进行填充。返回值为 error
  • 格式:
Fill(startIndex int, num int, value string) error
  • 示例:建立一个数组,在数组开始位置 index 为2的地方,用字符串 here 填充3个数据
func ExampleStrArray_Fill() {
      s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
      s.Fill(2, 3, "here")
      fmt.Println(s)

      // Output:
      // ["a","b","here","here","here","f","g","h"]
}

FilterEmpty

  • 说明:过滤指定数组中的空字符串
  • 格式:
FilterEmpty() *StrArray
  • 示例:建立一个数组,在赋值后,过滤该数组中的空字符串
func ExampleStrArray_FilterEmpty() {
      s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "", "c", "", "", "d"})
      fmt.Println(s.FilterEmpty())

      // Output:
      // ["a","b","c","d"]
}

Get

  • 说明:返回数组中指定 index 的值,返回值有2个参数,返回值 value,和是否找到指定位置的数据 found,为 true 则找到,为 false 则未找到
  • 格式:
Get(index int) (value string, found bool)
  • 示例:建立一个数组,在赋值后,得到数组 index 为3的值
func ExampleStrArray_Get() {
      s := garray.NewStrArrayFrom(g.SliceStr{"We", "are", "GF", "fans", "!"})
      sGet, sBool := s.Get(3)
      fmt.Println(sGet, sBool)

      // Output:
      // fans true
}

InsertAfter

  • 说明:在数组中指定 index 的位置之后插入值 value,返回值为 error
  • 格式:
InsertAfter(index int, value string) error
  • 示例:建立一个数组,在 index 为1的值之后,插入字符串 here
func ExampleStrArray_InsertAfter() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"a", "b", "c", "d"})
      s.InsertAfter(1, "here")
      fmt.Println(s.Slice())

      // Output:
      // [a b here c d]
}

InsertBefore

  • 说明:在数组中指定 index 的位置之前插入值 value,返回值为 error
  • 格式:
InsertBefore(index int, value string) error
  • 示例:建立一个数组并初始化,在 index 为1的值之前,插入字符串 here
func ExampleStrArray_InsertBefore() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"a", "b", "c", "d"})
      s.InsertBefore(1, "here")
      fmt.Println(s.Slice())

      // Output:
      // [a here b c d]
}

Interfaces

  • 说明:把当前数组作为 []interface{} 进行返回
  • 格式:
Interfaces() []interface{}
  • 示例:建立一个数组并初始化,并打印出返回值 []interface{} 的内容
func ExampleStrArray_Interfaces() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
      r := s.Interfaces()
      fmt.Println(r)

      // Output:
      // [a b c d e f g h]
}

IsEmpty

  • 说明:判断当前数组是不是空数组,如果是空数组,则返回 true,如果不是空数组,则返回 false
  • 格式:
IsEmpty() bool
  • 示例:建立2个数组并初始化,并判断是否为空数组
func ExampleStrArray_IsEmpty() {
      s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "", "c", "", "", "d"})
      fmt.Println(s.IsEmpty())
      s1 := garray.NewStrArray()
      fmt.Println(s1.IsEmpty())

      // Output:
      // false
      // true
}

Iterator

  • 说明:数组遍历
  • 格式:
Iterator(f func(k int, v string) bool)
  • 示例:建立1个数组,并对其进行遍历
func ExampleStrArray_Iterator() {
      s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"})
      s.Iterator(func(k int, v string) bool {
          fmt.Println(k, v)
          return true
      })

      // Output:
      // 0 a
      // 1 b
      // 2 c
}

IteratorAsc

  • 说明:根据给定的回调函数 f,按升序对数组进行遍历,如果 f 返回 true,则继续进行遍历,否则停止遍历
  • 格式:
IteratorAsc(f func(k int, v string) bool)
  • 示例:建立1个数组,并按照自定义函数对其进行升序遍历
func ExampleStrArray_Iterator() {
      s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"})
      s.Iterator(func(k int, v string) bool {
          fmt.Println(k, v)
          return true
      })

      // Output:
      // 0 a
      // 1 b
      // 2 c
}

IteratorDesc

  • 说明:根据给定的回调函数 f,按降序对数组进行遍历,如果 f 返回 true,则继续进行遍历,否则停止遍历
  • 格式:
IteratorAsc(f func(k int, v string) bool)
  • 示例:建立1个数组,并按照自定义函数对其进行降序遍历
func ExampleStrArray_IteratorDesc() {
      s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"})
      s.IteratorDesc(func(k int, v string) bool {
          fmt.Println(k, v)
          return true
      })

      // Output:
      // 2 c
      // 1 b
      // 0 a
}

Join

  • 说明:将数组元素根据给定的字符串连接符 gule,连接起来
  • 格式:
Join(glue string) string
  • 示例:给定连接符 ',',将数组中的字符串连接起来
func ExampleStrArray_Join() {
      s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"})
      fmt.Println(s.Join(","))

      // Output:
      // a,b,c
}

Len

  • 说明:得到数组的长度
  • 格式:
Len() int
  • 示例:建立一个新数组,初始化后得到该数组的长度
func ExampleStrArray_Len() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
      fmt.Println(s.Len())

      // Output:
      // 8
}

LockFunc

  • 说明:通过回调函数 f 对数组进行写锁定
  • 格式:
LockFunc(f func(array []string)) *StrArray
  • 示例:建立一个新数组,并对该数组在写锁定的状态下,修改最后一个数据
func ExampleStrArray_LockFunc() {
      s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"})
      s.LockFunc(func(array []string) {
          array[len(array)-1] = "GF fans"
      })
      fmt.Println(s)

      // Output:
      // ["a","b","GF fans"]
}

MarshalJSON

  • 说明:实现 json.Marshal 的 JSON 格式的序列化接口
  • 格式:
MarshalJSON() ([]byte, error)
  • 示例:建立一个新 JSON 格式的数据,并对该数据进行序列化的操作后,打印出相应结果
func ExampleStrArray_MarshalJSON() {
      type Student struct {
          Id      int
          Name    string
          Lessons []string
      }
      s := Student{
          Id:      1,
          Name:    "john",
          Lessons: []string{"Math", "English", "Music"},
      }
      b, _ := json.Marshal(s)
      fmt.Println(string(b))

      // Output:
      // {"Id":1,"Name":"john","Lessons":["Math","English","Music"]}
}

Merge

  • 说明:合并数组,将指定数组中的内容合并到当前数组中。参数 array 可以是任意 garray 或 slice 类型。 Merge 和 Append 的主要区别是 Append 仅仅支持 slice 类型, Merge 则支持更多的参数类型
  • 格式:
Merge(array interface{}) *StrArray
  • 示例:建立2个新数组 s1 和 s2,并将 s2 的数据合并到 s1 上
func ExampleStrArray_Merge() {
      s1 := garray.NewStrArray()
      s2 := garray.NewStrArray()
      s1.SetArray(g.SliceStr{"a", "b", "c"})
      s2.SetArray(g.SliceStr{"d", "e", "f"})
      s1.Merge(s2)
      fmt.Println(s1)

      // Output:
      // ["a","b","c","d","e","f"]
}

NewStrArray

  • 说明:创建一个新数组。 safe 为非必需参数,布尔型,是并发安全的开关,缺省值为 False
  • 格式:
NewStrArray(safe ...bool) *StrArray
  • 示例:建立一个空数组,并添加数据。此时没有指定 Safe 参数,默认为非并发安全设置
func ExampleNewStrArray() {
      s := garray.NewStrArray()
      s.Append("We")
      s.Append("are")
      s.Append("GF")
      s.Append("Fans")
      fmt.Println(s.Slice())

      // Output:
      // [We are GF Fans]
}

NewStrArrayFrom

  • 说明:根据给定的数组内容,创建一个新数组。 safe 为非必需参数,布尔型,是并发安全的开关,缺省值为 False
  • 格式:
NewStrArrayFrom(array []string, safe ...bool) *StrArray
  • 示例:建立一个空数组,并根据指定内容添加数据。此时没有指定 Safe 参数,默认为非并发安全设置
func ExampleNewStrArrayFrom() {
      s := garray.NewStrArrayFrom(g.SliceStr{"We", "are", "GF", "fans", "!"})
      fmt.Println(s.Slice(), s.Len(), cap(s.Slice()))

      // Output:
      // [We are GF fans !] 5 5
}

NewStrArrayFromCopy

  • 说明:根据给定的数组内容的拷贝,创建一个新数组。 safe 为非必需参数,布尔型,是并发安全的开关,缺省值为 False
  • 格式:
NewStrArrayFrom(array []string, safe ...bool) *StrArray
  • 示例:建立一个空数组,并根据指定内容添加数据。此时没有指定 Safe 参数,默认为非并发安全设置
func ExampleNewStrArrayFromCopy() {
      s := garray.NewStrArrayFromCopy(g.SliceStr{"We", "are", "GF", "fans", "!"})
      fmt.Println(s.Slice(), s.Len(), cap(s.Slice()))

      // Output:
      // [We are GF fans !] 5 5
}

NewStrArraySize

  • 说明:根据给定的 size 和 cap,创建一个新数组。 safe 为非必需参数,布尔型,是并发安全的开关,缺省值为 False
  • 格式:
NewStrArraySize(size int, cap int, safe ...bool) *StrArray
  • 示例:建立一个空数组, Size为3, Cap为5,并添加数据。打印出相应的内容。此时没有指定 Safe 参数,默认为非并发安全设置
func ExampleNewStrArraySize() {
      s := garray.NewStrArraySize(3, 5)
      s.Set(0, "We")
      s.Set(1, "are")
      s.Set(2, "GF")
      s.Set(3, "fans")
      fmt.Println(s.Slice(), s.Len(), cap(s.Slice()))

      // Output:
      // [We are GF] 3 5
}

Pad

  • 说明:填充指定大小为 size 的值 value 到数组中。如果大小 size 是正数,则从数组的右边开始填充。如果 size 是负数,则从数组的左边开始填充。如果 size 的大小正好等于数组的长度,那么将不会填充任何数据。
  • 格式:
Pad(size int, value string) *StrArray
  • 示例:建立1个新数组,先从左边将数组,用指定的字符串 here 填充到 size 为7,然后用指定的字符串 there 将数组用字符串填充到 size 为10
func ExampleStrArray_Pad() {
      s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"})
      s.Pad(7, "here")
      fmt.Println(s)
      s.Pad(-10, "there")
      fmt.Println(s)

      // Output:
      // ["a","b","c","here","here","here","here"]
      // ["there","there","there","a","b","c","here","here","here","here"]
}

PopLeft

  • 说明:从数组的左侧将一个字符串数据出栈,返回值 value 为出栈的字符串数据。更新后的数组数据为剩余数据。当数组为空时, found 为 false。
  • 格式:
PopLeft() (value string, found bool)
  • 示例:建立1个新数组,将最左边的数据出栈,并打印出剩余的数据
func ExampleStrArray_PopLeft() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"a", "b", "c", "d"})
      s.PopLeft()
      fmt.Println(s.Slice())

      // Output:
      // [b c d]
}

PopLefts

  • 说明:从数组的左侧将多个字符串数据出栈,返回值为出栈的字符串数据,出栈数据的个数为 size。如果 size 比数组的 size 大,那么方法将返回数组中所有的数据。如果 size<=0或者为空,那么将返回 nil
  • 格式:
PopLefts(size int) []string
  • 示例:建立1个新数组,将最左边的2个数据做出栈操作,并打印出出栈的数据和原数组的剩余数据
func ExampleStrArray_PopLefts() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
      r := s.PopLefts(2)
      fmt.Println(r)
      fmt.Println(s)

      // Output:
      // [a b]
      // ["c","d","e","f","g","h"]
}

PopRand

  • 说明:从数组中随机出栈1个数据,返回值为出栈的字符串数据。如果数组 为空,那么 found 将返回 false
  • 格式:
PopRand() (value string, found bool)
  • 示例:建立1个新数组,从数组中随机出栈1个数据,并打印出出栈的数据
func ExampleStrArray_PopRand() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
      r, _ := s.PopRand()
      fmt.Println(r)

      // May Output:
      // e
}

PopRands

  • 说明:从数组中随机出栈 size 个数据,返回值为出栈的字符串数据。如果 size<=0或者为空,那么将返回 nil
  • 格式:
PopRands(size int) []string
  • 示例:建立1个新数组,从数组中随机出栈2个数据,并打印出出栈的数据
func ExampleStrArray_PopRands() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
      r := s.PopRands(2)
      fmt.Println(r)

      // May Output:
      // [e c]
}

PopRight

  • 说明:从数组的右侧将一个字符串数据出栈,返回值 value 为出栈的字符串数据。更新后的数组数据为剩余数据。当数组为空时, found 为 false。
  • 格式:
PopRight() (value string, found bool)
  • 示例:建立1个新数组,将最右边的数据出栈,并打印出剩余的数据
func ExampleStrArray_PopRight() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"a", "b", "c", "d"})
      s.PopRight()
      fmt.Println(s.Slice())

      // Output:
      // [a b c]
}

PopRights

  • 说明:从数组的右侧将多个字符串数据出栈,返回值为出栈的字符串数据,出栈数据的个数为 size。如果 size 比数组的 size 大,那么方法将返回数组中所有的数据。如果 size<=0或者为空,那么将返回 nil
  • 格式:
PopRights(size int) []string
  • 示例:建立1个新数组,将最右边的2个数据做出栈操作,并打印出出栈的数据和原数组的剩余数据
func ExampleStrArray_PopRights() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
      r := s.PopRights(2)
      fmt.Println(r)
      fmt.Println(s)

      // Output:
      // [g h]
      // ["a","b","c","d","e","f"]
}

PushLeft

  • 说明:从数组的左侧入栈一个或多个字符串
  • 格式:
PushLeft(value ...string) *StrArray
  • 示例:建立1个新数组,从数组的左侧入栈多个字符串数据,并打印出更新后的数据
func ExampleStrArray_PushLeft() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"a", "b", "c", "d"})
      s.PushLeft("We", "are", "GF", "fans")
      fmt.Println(s.Slice())

      // Output:
      // [We are GF fans a b c d]
}

PushRight

  • 说明:从数组的右侧入栈一个或多个字符串
  • 格式:
PushRight(value ...string) *StrArray
  • 示例:建立1个新数组,从数组的右侧入栈多个字符串数据,并打印出更新后的数据
func ExampleStrArray_PushRight() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"a", "b", "c", "d"})
      s.PushRight("We", "are", "GF", "fans")
      fmt.Println(s.Slice())

      // Output:
      // [a b c d We are GF fans]
}

Rand

  • 说明:从数组中随机取出1个字符串(非删除式)
  • 格式:
Rand() (value string, found bool)
  • 示例:建立1个新数组,从数组中随机取出一个字符串
func ExampleStrArray_Rand() {
      s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
      fmt.Println(s.Rand())

      // May Output:
      // c true
}

Rands

  • 说明:从数组中随机取出 size 个字符串(非删除式)
  • 格式:
Rands(size int) []string
  • 示例:建立1个新数组,从数组中随机取出3个字符串
func ExampleStrArray_Rands() {
      s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
      fmt.Println(s.Rands(3))

      // May Output:
      // [e h e]
}

Range

  • 说明:获取数组中指定范围的数据。如果是在并发安全的模式下使用,则该方法返回一个 slice 拷贝。
  • 格式:
Range(start int, end ...int) []string
  • 示例:建立1个新数组,获取数组从 index 为2至5位置的数据
func ExampleStrArray_Range() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
      r := s.Range(2, 5)
      fmt.Println(r)

      // Output:
      // [c d e]
}

Remove

  • 说明:从数组中移除位置在 index 处的数据。如果 index 超过数组的边界,则 found 返回 false
  • 格式:
Remove(index int) (value string, found bool)
  • 示例:建立1个新数组,移除数组 index 为1的数据
func ExampleStrArray_Remove() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"a", "b", "c", "d"})
      s.Remove(1)
      fmt.Println(s.Slice())

      // Output:
      // [a c d]
}

RemoveValue

  • 说明:从数组中移除指定的数据 value。如果 value 在数组中被找到,则 found 返回 true,否则 found 返回 false
  • 格式:
RemoveValue(value string) bool
  • 示例:建立1个新数组,移除数组中值为 b 的数据
func ExampleStrArray_RemoveValue() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"a", "b", "c", "d"})
      s.RemoveValue("b")
      fmt.Println(s.Slice())

      // Output:
      // [a c d]
}

Replace

  • 说明:将原字符串数组用指定的字符串数组进行替换,替换从原数组的头部位置开始。
  • 格式:
Replace(array []string) *StrArray
  • 示例:建立1个新数组,并用指定的字符串数组进行替换
func ExampleStrArray_Replace() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"We", "are", "GF", "fans", "!"})
      fmt.Println(s.Slice())
      s.Replace(g.SliceStr{"Happy", "coding"})
      fmt.Println(s.Slice())

      // Output:
      // [We are GF fans !]
      // [Happy coding GF fans !]
}

Reverse

  • 说明:将整个数组反转,例如:["qaz","wsx","edc","rfv"] => ["rfv","edc","wsx","qaz"]。
  • 格式:
Replace(array []string) *StrArray
  • 示例:建立1个新数组,初始化后执行反转操作并打印
func ExampleStrArray_Reverse() {
      s := garray.NewStrArrayFrom(g.SliceStr{"a", "m", "c"})
      fmt.Println(s.Reverse())

      // Output:
      // ["c","m","a"]
}

RLockFunc

  • 说明:通过自定义回调函数 f 进行数组的读锁定
  • 格式:
RLockFunc(f func(array []string)) *StrArray
  • 示例:建立1个新数组,在回调函数 f 中,对数组进行遍历,并打印出数组元素
func ExampleStrArray_RLockFunc() {
      s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e"})
      s.RLockFunc(func(array []string) {
          for i := 0; i < len(array); i++ {
              fmt.Println(array[i])
          }
      })

      // Output:
      // a
      // b
      // c
      // d
      // e
}

Search

  • 说明:在数组中搜索指定的字符串,返回值为该值在数组中的 index,如果没有查询到,则返回 -1
  • 格式:
Search(value string) int
  • 示例:建立1个新数组,并在该数组中搜索字符串 e 和z
func ExampleStrArray_Search() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
      fmt.Println(s.Search("e"))
      fmt.Println(s.Search("z"))

      // Output:
      // 4
      // -1
}

Set

  • 说明:给数组中指定位置的 index 设置值 value,如果 index<0 或者 index 超出了数组的边界,则返回错误 error
  • 格式:
Set(index int, value string) error
  • 示例:建立1个新数组,长度为3,给数组设置值,值最终只按顺序设置到了 index 为2的位置,因为数组长度限制,最后一个值未设置成功
func ExampleStrArray_Set() {
      s := garray.NewStrArraySize(3, 5)
      s.Set(0, "We")
      s.Set(1, "are")
      s.Set(2, "GF")
      s.Set(3, "fans")
      fmt.Println(s.Slice())

      // Output:
      // [We are GF]
}

SetArray

  • 说明:根据给定的 slice 数组内容给数组赋值
  • 格式:
SetArray(array []string) *StrArray
  • 示例:建立1个新数组,在给其赋值后,打印出来
func ExampleStrArray_SetArray() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"We", "are", "GF", "fans", "!"})
      fmt.Println(s.Slice())

      // Output:
      // [We are GF fans !]
}

Shuffle

  • 说明:将数组中的内容进行乱序排列
  • 格式:
Shuffle() *StrArray
  • 示例:建立1个新数组,在给其赋值后进行乱序排列,并打印出来
func ExampleStrArray_Shuffle() {
      s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
      fmt.Println(s.Shuffle())

      // May Output:
      // ["a","c","e","d","b","g","f","h"]
}

Slice

  • 说明:得到数组的 slice 切片数据,注意,如果是在并发安全模式下,返回的是一份拷贝数据,否则返回的是指向数据的指针
  • 格式:
Shuffle() *StrArray
  • 示例:建立1个新数组,在给其赋值后,并打印该数组的切片数据
func ExampleStrArray_Slice() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
      fmt.Println(s.Slice())

      // Output:
      // [a b c d e f g h]
}

Sort

  • 说明:对数组内容进行升序排序。 reverse 控制排序的方向,默认为 true 升序, false 为降序
  • 格式:
Sort(reverse ...bool) *StrArray
  • 示例:建立1个新数组,在给其赋值后,并按升序进行排序
func ExampleStrArray_Sort() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"b", "d", "a", "c"})
      a := s.Sort()
      fmt.Println(a)

      // Output:
      // ["a","b","c","d"]
}

SortFunc

  • 说明:通过自定义函数 less 对数组内容进行排序。
  • 格式:
SortFunc(less func(v1, v2 string) bool) *StrArray
  • 示例:建立1个新数组,在给其赋值后,首先用自定义函数对其进行降序排序,然后用自定义函数对其进行升序排序,并打印出相应的结果
func ExampleStrArray_SortFunc() {
      s := garray.NewStrArrayFrom(g.SliceStr{"b", "c", "a"})
      fmt.Println(s)
      s.SortFunc(func(v1, v2 string) bool {
          return gstr.Compare(v1, v2) > 0
      })
      fmt.Println(s)
      s.SortFunc(func(v1, v2 string) bool {
          return gstr.Compare(v1, v2) < 0
      })
      fmt.Println(s)

      // Output:
      // ["b","c","a"]
      // ["c","b","a"]
      // ["a","b","c"]
}

String

  • 说明:将当前的数组转换成 string,
  • 格式:
String() string
  • 示例:建立1个新数组,在给其赋值后,将数组转换成 string,并打印出相应的结果
func ExampleStrArray_String() {
      s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"})
      fmt.Println(s.String())

      // Output:
      // ["a","b","c"]
}

Subslice

  • 说明:根据给定的偏离值 offset 和长度 length 参数获得数组的切片,注意,如果是在并发安全模式下,返回拷贝数据,否则返回指向数据的指针。如果偏离值 offset 是非负数,则从数组的开始位置进行切片,否则从数组的尾部开始切片。
  • 格式:
SubSlice(offset int, length ...int)
  • 示例:建立1个新数组,在给其赋值后,将数组转换成 string,并打印出相应的结果
func ExampleStrArray_SubSlice() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
      r := s.SubSlice(3, 4)
      fmt.Println(r)

      // Output:
      // [d e f g]
}

Sum

  • 说明:对数组中的整数值进行求和
  • 格式:
Sum() (sum int)
  • 示例:建立1个新数组,在给其赋值后,对数组中的整数值进行求和
func ExampleStrArray_Sum() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"3", "5", "10"})
      a := s.Sum()
      fmt.Println(a)

      // Output:
      // 18
}

Unique

  • 说明:对数组中的数据进行去重处理
  • 格式:
Unique() *StrArray
  • 示例:建立1个新数组,在给其赋值后,对数组中的整数值进行求和
func ExampleStrArray_Unique() {
      s := garray.NewStrArray()
      s.SetArray(g.SliceStr{"a", "b", "c", "c", "c", "d", "d"})
      fmt.Println(s.Unique())

      // Output:
      // ["a","b","c","d"]
}

UnmarshalJSON

  • 说明:实现 json.Unmarshal 的 UnmarshalJSON 接口
  • 格式:
UnmarshalJSON(b []byte) error
  • 示例:建立1个 byte 切片,将其赋值给结构体后,进行反序列化操作,打印出相应的内容
func ExampleStrArray_UnmarshalJSON() {
      b := []byte(`{"Id":1,"Name":"john","Lessons":["Math","English","Sport"]}`)
      type Student struct {
          Id      int
          Name    string
          Lessons *garray.StrArray
      }
      s := Student{}
      json.Unmarshal(b, &s)
      fmt.Println(s)

      // Output:
      // {1 john ["Math","English","Sport"]}
}

UnmarshalValue

  • 说明:对任意类型的值实现反序列化接口
  • 格式:
UnmarshalValue(value interface{}) error
  • 示例:建立1个结构体,并对其值 进行反序列化操作,打印出相应的内容
func ExampleStrArray_UnmarshalValue() {
      type Student struct {
          Name    string
          Lessons *garray.StrArray
      }
      var s *Student
      gconv.Struct(g.Map{
          "name":    "john",
          "lessons": []byte(`["Math","English","Sport"]`),
      }, &s)
      fmt.Println(s)

      var s1 *Student
      gconv.Struct(g.Map{
          "name":    "john",
          "lessons": g.SliceStr{"Math", "English", "Sport"},
      }, &s1)
      fmt.Println(s1)

      // Output:
      // &{john ["Math","English","Sport"]}
      // &{john ["Math","English","Sport"]}
}

Walk

  • 说明:通过自定义函数 f,对数组内容进行遍历修改
  • 格式:
Walk(f func(value string) string) *StrArray
  • 示例:建立1个数组,对数组内容进行遍历修改,为每个字符串添加前缀,并打印出相应的内容
func ExampleStrArray_Walk() {
      var array garray.StrArray
      tables := g.SliceStr{"user", "user_detail"}
      prefix := "gf_"
      array.Append(tables...)
      // Add prefix for given table names.
      array.Walk(func(value string) string {
          return prefix + value
      })
      fmt.Println(array.Slice())

      // Output:
      // [gf_user gf_user_detail]
}

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.