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组件列表文本处理正则表达式-gregex正则表达式-方法介绍.md

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

当函数名中有 All 的时候,它会继续查找非重叠的后续并返回 slice

当函数名中有 String 的时候,参数及返回值都是 string,否则为 []byte

IsMatch/IsMatchString

  • 说明: IsMatch() 方法可以测试字符串,判断是否匹配正则表达式的模式。如果发现一次匹配,该方法返回 true,否则返回 false。
  • 格式:
IsMatch(pattern string, src []byte) bool
IsMatchString(pattern string, src string) bool
  • Tip: regexp 已经在底层处理并缓存了 Regex 对象,以极大地提高执行效率,无需每次显式重新创建,下同。
  • 示例:
func ExampleIsMatch() {
      patternStr := `\d+`
      g.Dump(gregex.IsMatch(patternStr, []byte("hello 2022! hello GoFrame!")))
      g.Dump(gregex.IsMatch(patternStr, nil))
      g.Dump(gregex.IsMatch(patternStr, []byte("hello GoFrame!")))

      // Output:
      // true
      // false
      // false
}

Match/MatchString

  • 说明:用来匹配子字符串, Match 只返回第一个匹配的结果。区别于原生的正则方法, gregex 是对 FindSubmatch 进行封装,直接返回第一个包括子模式结果的 slice。
  • 格式
Match(pattern string, src []byte) ([][]byte, error)
MatchString(pattern string, src string) ([]string, error)
  • 示例:匹配 url 中的参数。
func ExampleMatch() {
      patternStr := `(\w+)=(\w+)`
      matchStr := "https://goframe.org/pages/viewpage.action?pageId=1114219&searchId=8QC5D1D2E!"
      // This method looks for the first match index
      result, err := gregex.Match(patternStr, []byte(matchStr))
      g.Dump(result)
      g.Dump(err)

      // Output:
      // [
      //     "pageId=1114219",
      //     "pageId",
      //     "1114219",
      // ]
      // <nil>
}

MatchAll/MatchAllString

  • 说明:用来匹配子字符串, MatchAll 返回全部的结果。区别于原生的正则方法, gregex的 MatchAll 是对 FindAllSubmatch 方法进行封装,返回所有结果集的 slice,包括结果集中的子模式的结果。
  • 格式:
MatchAllString(pattern string, src string) ([][]string, error)
MatchAll(pattern string, src []byte) ([][][]byte, error)
  • 示例:下面这个例子是匹配 url 中的参数。
func ExampleMatchAll() {
      patternStr := `(\w+)=(\w+)`
      matchStr := "https://goframe.org/pages/viewpage.action?pageId=1114219&searchId=8QC5D1D2E!"
      result, err := gregex.MatchAll(patternStr, []byte(matchStr))
      g.Dump(result)
      g.Dump(err)

      // Output:
      //  [
      //     [
      //         "pageId=1114219",
      //         "pageId",
      //         "1114219",
      //     ],
      //     [
      //         "searchId=8QC5D1D2E",
      //         "searchId",
      //         "8QC5D1D2E",
      //     ],
      // ]
      // <nil>
}

func ExampleMatchAllString() {
      patternStr := `(\w+)=(\w+)`
      matchStr := "https://goframe.org/pages/viewpage.action?pageId=1114219&searchId=8QC5D1D2E!"
      result, err := gregex.MatchAllString(patternStr, matchStr)
      g.Dump(result)
      g.Dump(err)

      // Output:
      // [
      //     [
      //         "pageId=1114219",
      //         "pageId",
      //         "1114219",
      //     ],
      //     [
      //         "searchId=8QC5D1D2E",
      //         "searchId",
      //         "8QC5D1D2E",
      //     ],
      // ]
      // <nil>
}

Quote

  • 说明:将指定正则表达式中的特定符号进行转义。
  • 格式:
Quote(s string) string
  • 示例:
func ExampleQuote() {
      result := gregex.Quote(`[1-9]\d+`)
      g.Dump(result)

      // Output:
      // "\[1-9\]\\d\+"
}

Replace/ReplaceString

  • 说明:用来替换所有匹配的字符串并返回一个源字符串的拷贝。
  • 格式:
Replace(pattern string, replace, src []byte) ([]byte, error)
ReplaceString(pattern, replace, src string) (string, error)
  • 示例
func ExampleReplace() {
      var (
          patternStr  = `\d+`
          str         = "hello GoFrame 2020!"
          repStr      = "2021"
          result, err = gregex.Replace(patternStr, []byte(repStr), []byte(str))
      )
      g.Dump(err)
      g.Dump(result)

      // Output:
      // <nil>
      // "hello GoFrame 2021!"
}

ReplaceFunc/ReplaceStringFunc

  • 说明:用来替换所有匹配的字符串,返回一个源字符串的拷贝。与 Replace 方法的区别在于,该方法可以在闭包中对查询进行二次判断或处理,而非简单的替换。
  • 格式:
ReplaceFunc(pattern string, src []byte, replaceFunc func(b []byte) []byte) ([]byte, error)
ReplaceStringFunc(pattern string, src string, replaceFunc func(s string) string) (string, error)
  • 示例:
func ExampleReplaceFunc() {
      var (
          patternStr = `(\d+)~(\d+)`
          str        = "hello GoFrame 2018~2020!"
      )
      // In contrast to [ExampleReplace]
      result, err := gregex.ReplaceFunc(patternStr, []byte(str), func(match []byte) []byte {
          g.Dump(match)
          return bytes.Replace(match, []byte("2020"), []byte("2023"), -1)
      })
      g.Dump(result)
      g.Dump(err)

      // ReplaceStringFunc
      resultStr, _ := gregex.ReplaceStringFunc(patternStr, str, func(match string) string {
          g.Dump(match)
          return strings.Replace(match, "2020", "2023", -1)
      })
      g.Dump(resultStr)
      g.Dump(err)

         // Output:
      // "2018~2020"
      // "hello gf 2018~2023!" // ReplaceFunc result
      // <nil>
      // "2018~2020"
      // "hello gf 2018-2023!" // ReplaceStringFunc result
      // <nil>
}

ReplaceFuncMatch/ReplaceStringFuncMatch

ReplaceFuncMatch 返回 src 的拷贝,其中 regexp 的所有匹配都被应用于匹配字节切片的函数的返回值替换。 返回的替换直接替换。

  • 说明:用来替换所有匹配的字符串,返回一个源字符串的拷贝。该方法的强大之处在于可以在闭包中对查询进行二次判断或处理,且 MatchString 函数包含了所有子模式的查询结果,而非简单的替换。
  • 格式:
ReplaceFuncMatch(pattern string, src []byte, replaceFunc func(match [][]byte) []byte) ([]byte, error)
ReplaceStringFuncMatch(pattern string, src string, replaceFunc func(match []string) string) (string, error)
  • 示例:
func ExampleReplaceStringFuncMatch() {
       var (
          patternStr = `([A-Z])\w+`
          str        = "hello Golang 2018~2021!"
      )
      // In contrast to [ExampleReplaceFunc]
      // the result contains the `pattern' of all subpatterns that use the matching function
      result, err := gregex.ReplaceFuncMatch(patternStr, []byte(str), func(match [][]byte) []byte {
          g.Dump(match)
          return []byte("GoFrame")
      })
      g.Dump(result)
      g.Dump(err)

      // ReplaceStringFuncMatch
      resultStr, err := gregex.ReplaceStringFuncMatch(patternStr, str, func(match []string) string {
          g.Dump(match)
          match[0] = "Gf"
          return match[0]
      })
      g.Dump(resultStr)
      g.Dump(err)

      // Output:
      // [
      //     "Golang",
      //     "G",
      // ]
      // "hello GoFrame 2018~2021!" // ReplaceFuncMatch result
      // <nil>
      // [
      //     "Golang",
      //     "G",
      // ]
      // "hello Gf 2018~2021!" // ReplaceStringFuncMatch result
      // <nil>
}

Split

  • 说明:将文本内容由指定的正则表达式进行切割。不包含元字符,相当于 strings.SplitN。
  • 格式:
Split(pattern string, src string) []string
  • 示例:
func ExampleSplit() {
      patternStr := `\d+`
      str := "hello2020GoFrame"
      result := gregex.Split(patternStr, str)
      g.Dump(result)

      // Output:
      // [
      //     "hello",
      //     "GoFrame",
      // ]
}

Validate

  • 说明:基于原生方法 compile 封装,检查给定的正则表达式是否有效
  • 格式:
Validate(pattern string) error
  • 示例:
func ExampleValidate() {
      // Valid match statement
      g.Dump(gregex.Validate(`\d+`))
      // Mismatched statement
      g.Dump(gregex.Validate(`[a-9]\d+`))

      // Output:
      // <nil>
      // {
      //     Code: "invalid character class range",
      //     Expr: "a-9",
      // }
}

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.