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组件列表编码解码通用编解码-gjson通用编解码-方法介绍.md

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

New

  • 说明: New 可以用任意类型的值 data 创建一个 Json 对象,但是由于数据访问的关系, data 应该是一个 map 或者 slice,否则是无意义的。

  • 注意: safe 参数决定了 Json 对象是否是并发安全的,默认为 false

  • 格式:

func New(data interface{}, safe ...bool) *Json
  • 示例:
func ExampleNew() {
      jsonContent := `{"name":"john", "score":"100"}`
      j := gjson.New(jsonContent)
      fmt.Println(j.Get("name"))
      fmt.Println(j.Get("score"))

      // Output:
      // john
      // 100
}

NewWithTag

  • 说明: NewWithTag 可以用任意类型的值 data 创建一个 Json 对象,但是由于数据访问的关系, data 应该是一个 map 或者 slice,否则是无意义的。

  • 注意: tgts 参数指定了结构体转换到map的标签名的优先级,多个标签用 ',' 分割。

  • safe 参数决定了 Json 对象是否是并发安全的,默认为 false

  • 格式:

func NewWithTag(data interface{}, tags string, safe ...bool) *Json
  • 示例:
func ExampleNewWithTag() {
      type Me struct {
          Name  string `tag:"name"`
          Score int    `tag:"score"`
          Title string
      }
      me := Me{
          Name:  "john",
          Score: 100,
          Title: "engineer",
      }
      j := gjson.NewWithTag(me, "tag", true)
      fmt.Println(j.Get("name"))
      fmt.Println(j.Get("score"))
      fmt.Println(j.Get("Title"))

      // Output:
      // john
      // 100
      // engineer
}

NewWithOptions

  • 说明: NewWithOptions 可以用任意类型的值 data 创建一个 Json 对象,但是由于数据访问的关系, data 应该是一个 map 或者 slice,否则是无意义的。

  • 格式:

func NewWithOptions(data interface{}, options Options) *Json
  • 示例:
func ExampleNewWithOptions() {
      type Me struct {
          Name  string `tag:"name"`
          Score int    `tag:"score"`
          Title string
      }
      me := Me{
          Name:  "john",
          Score: 100,
          Title: "engineer",
      }

      j := gjson.NewWithOptions(me, gjson.Options{
          Tags: "tag",
      })
      fmt.Println(j.Get("name"))
      fmt.Println(j.Get("score"))
      fmt.Println(j.Get("Title"))

      // Output:
      // john
      // 100
      // engineer
}
func ExampleNewWithOptions_UTF8BOM() {
      jsonContent := `{"name":"john", "score":"100"}`

      content := make([]byte, 3, len(jsonContent)+3)
      content[0] = 0xEF
      content[1] = 0xBB
      content[2] = 0xBF
      content = append(content, jsonContent...)

      j := gjson.NewWithOptions(content, gjson.Options{
          Tags: "tag",
      })
      fmt.Println(j.Get("name"))
      fmt.Println(j.Get("score"))

      // Output:
      // john
      // 100
}

Load

  • 说明:Load 从指定的文件 path 中加载内容,并将其内容创建一个 Json 对象。

  • 格式:

func Load(path string, safe ...bool) (*Json, error)
  • 示例:
func ExampleLoad() {
      jsonFilePath := gtest.DataPath("json", "data1.json")
      j, _ := gjson.Load(jsonFilePath)
      fmt.Println(j.Get("name"))
      fmt.Println(j.Get("score"))

      notExistFilePath := gtest.DataPath("json", "data2.json")
      j2, _ := gjson.Load(notExistFilePath)
      fmt.Println(j2.Get("name"))

      // Output:
      // john
      // 100
}
func ExampleLoad_Xml() {
      jsonFilePath := gtest.DataPath("xml", "data1.xml")
      j, _ := gjson.Load(jsonFilePath)
      fmt.Println(j.Get("doc.name"))
      fmt.Println(j.Get("doc.score"))
}

LoadJson

  • 说明: LoadJson 用给定的 JSON 格式的内容创建一个 Json 对象。

  • 格式:

func LoadJson(data interface{}, safe ...bool) (*Json, error)
  • 示例:
func ExampleLoadJson() {
      jsonContent := `{"name":"john", "score":"100"}`
      j, _ := gjson.LoadJson(jsonContent)
      fmt.Println(j.Get("name"))
      fmt.Println(j.Get("score"))

      // Output:
      // john
      // 100
}

LoadXml

  • 说明: LoadXml 用给定的 XML 格式的内容创建一个 Json 对象。

  • 格式:

func LoadXml(data interface{}, safe ...bool) (*Json, error)
  • 示例:
func ExampleLoadXml() {
      xmlContent := `<?xml version="1.0" encoding="UTF-8"?>
      <base>
          <name>john</name>
          <score>100</score>
      </base>`
      j, _ := gjson.LoadXml(xmlContent)
      fmt.Println(j.Get("base.name"))
      fmt.Println(j.Get("base.score"))

      // Output:
      // john
      // 100
}

LoadIni

  • 说明: LoadIni 用给定的 INI 格式的内容创建一个 Json 对象。

  • 格式:

func LoadIni(data interface{}, safe ...bool) (*Json, error)
  • 示例:
func ExampleLoadIni() {
      iniContent := `
      [base]
      name = john
      score = 100
      `
      j, _ := gjson.LoadIni(iniContent)
      fmt.Println(j.Get("base.name"))
      fmt.Println(j.Get("base.score"))

      // Output:
      // john
      // 100
}

LoadYaml

  • 说明: LoadYaml 用给定的 YAML 格式的内容创建一个 Json 对象。

  • 格式:

func LoadYaml(data interface{}, safe ...bool) (*Json, error)
  • 示例:
func ExampleLoadYaml() {
      yamlContent :=
          `base:
    name: john
    score: 100`

      j, _ := gjson.LoadYaml(yamlContent)
      fmt.Println(j.Get("base.name"))
      fmt.Println(j.Get("base.score"))

      // Output:
      // john
      // 100
}

LoadToml

  • 说明: LoadToml 用给定的 TOML 格式的内容创建一个 Json 对象。

  • 格式:

func LoadToml(data interface{}, safe ...bool) (*Json, error)
  • 示例:
func ExampleLoadToml() {
      tomlContent :=
          `[base]
    name = "john"
    score = 100`

      j, _ := gjson.LoadToml(tomlContent)
      fmt.Println(j.Get("base.name"))
      fmt.Println(j.Get("base.score"))

      // Output:
      // john
      // 100
}

LoadContent

  • 说明:LoadContent 根据给定的内容创建一个 Json 对象,它自动检查 content 的数据类型,支持的内容类型如下: JSON, XML, INI, YAML和TOML。

  • 格式:

func LoadContent(data interface{}, safe ...bool) (*Json, error)
  • 示例:
func ExampleLoadContent() {
      jsonContent := `{"name":"john", "score":"100"}`

      j, _ := gjson.LoadContent(jsonContent)

      fmt.Println(j.Get("name"))
      fmt.Println(j.Get("score"))

      // Output:
      // john
      // 100
}
func ExampleLoadContent_UTF8BOM() {
      jsonContent := `{"name":"john", "score":"100"}`

      content := make([]byte, 3, len(jsonContent)+3)
      content[0] = 0xEF
      content[1] = 0xBB
      content[2] = 0xBF
      content = append(content, jsonContent...)

      j, _ := gjson.LoadContent(content)

      fmt.Println(j.Get("name"))
      fmt.Println(j.Get("score"))

      // Output:
      // john
      // 100
}
func ExampleLoadContent_Xml() {
      xmlContent := `<?xml version="1.0" encoding="UTF-8"?>
      <base>
          <name>john</name>
          <score>100</score>
      </base>`

      x, _ := gjson.LoadContent(xmlContent)

      fmt.Println(x.Get("base.name"))
      fmt.Println(x.Get("base.score"))

      // Output:
      // john
      // 100
}

LoadContentType

  • 说明:LoadContentType 根据给定的内容和类型创建一个 Json 对象,支持的内容类型如下: Json, XML, INI, YAML和TOML。

  • 格式:

func LoadContentType(dataType string, data interface{}, safe ...bool) (*Json, error)
  • 示例:
func ExampleLoadContentType() {
      jsonContent := `{"name":"john", "score":"100"}`
      xmlContent := `<?xml version="1.0" encoding="UTF-8"?>
      <base>
          <name>john</name>
          <score>100</score>
      </base>`

      j, _ := gjson.LoadContentType("json", jsonContent)
      x, _ := gjson.LoadContentType("xml", xmlContent)
      j1, _ := gjson.LoadContentType("json", "")

      fmt.Println(j.Get("name"))
      fmt.Println(j.Get("score"))
      fmt.Println(x.Get("base.name"))
      fmt.Println(x.Get("base.score"))
      fmt.Println(j1.Get(""))

      // Output:
      // john
      // 100
      // john
      // 100
}

IsValidDataType

  • 说明:IsValidDataType 检查给定的 dataType 是否是可以用于加载的有效数据内容。

  • 格式:

func IsValidDataType(dataType string) bool
  • 示例:
func ExampleIsValidDataType() {
      fmt.Println(gjson.IsValidDataType("json"))
      fmt.Println(gjson.IsValidDataType("yml"))
      fmt.Println(gjson.IsValidDataType("js"))
      fmt.Println(gjson.IsValidDataType("mp4"))
      fmt.Println(gjson.IsValidDataType("xsl"))
      fmt.Println(gjson.IsValidDataType("txt"))
      fmt.Println(gjson.IsValidDataType(""))
      fmt.Println(gjson.IsValidDataType(".json"))

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

Valid

  • 说明:Valid 检查 data 是否为有效的 JSON 数据类型。 参数 data 指定 JSON 格式数据,可以是 bytes 或 string 类型。

  • 格式:

func Valid(data interface{}) bool
  • 示例:
func ExampleValid() {
      data1 := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
      data2 := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]`)
      fmt.Println(gjson.Valid(data1))
      fmt.Println(gjson.Valid(data2))

      // Output:
      // true
      // false
}

Marshal

  • 说明:Marshal 是 Encode 的别名。

  • 格式:

func Marshal(v interface{}) (marshaledBytes []byte, err error)
  • 示例:
func ExampleMarshal() {
      data := map[string]interface{}{
          "name":  "john",
          "score": 100,
      }

      jsonData, _ := gjson.Marshal(data)
      fmt.Println(string(jsonData))

      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "Guo Qiang",
          Age:  18,
      }

      infoData, _ := gjson.Marshal(info)
      fmt.Println(string(infoData))

      // Output:
      // {"name":"john","score":100}
      // {"Name":"Guo Qiang","Age":18}
}

MarshalIndent

  • 说明:MarshalIndent 是 json.``MarshalIndent 的别名 。

  • 格式:

func MarshalIndent(v interface{}, prefix, indent string) (marshaledBytes []byte, err error)
  • 示例:
func ExampleMarshalIndent() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      infoData, _ := gjson.MarshalIndent(info, "", "\t")
      fmt.Println(string(infoData))

      // Output:
      // {
      //    "Name": "John",
      //    "Age": 18
      // }
}

Unmarshal

  • 说明: Unmarshal 是 DecodeTo 的别名。

  • 格式:

func Unmarshal(data []byte, v interface{}) (err error)
  • 示例:
func ExampleUnmarshal() {
      type BaseInfo struct {
          Name  string
          Score int
      }

      var info BaseInfo

      jsonContent := "{\"name\":\"john\",\"score\":100}"
      gjson.Unmarshal([]byte(jsonContent), &info)
      fmt.Printf("%+v", info)

      // Output:
      // {Name:john Score:100}
}

Encode

  • 说明: Encode 将任意类型 value 序列化为内容为 JSON 的 byte 数组。

  • 格式:

func Encode(value interface{}) ([]byte, error)
  • 示例:
func ExampleEncode() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      infoData, _ := gjson.Encode(info)
      fmt.Println(string(infoData))

      // Output:
      // {"Name":"John","Age":18}
}

MustEncode

  • 说明:MustEncode 执行 Encode 操作,但如果发生任何错误,它会 panic。

  • 格式:

func MustEncode(value interface{}) []byte
  • 示例:
func ExampleMustEncode() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      infoData := gjson.MustEncode(info)
      fmt.Println(string(infoData))

      // Output:
      // {"Name":"John","Age":18}
}

EncodeString

  • 说明: EncodeString 将任意类型 value 序列化为内容为 JSON 的 string 。

  • 格式:

func EncodeString(value interface{}) (string, error)
  • 示例:
func ExampleEncodeString() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      infoData, _ := gjson.EncodeString(info)
      fmt.Println(infoData)

      // Output:
      // {"Name":"John","Age":18}
}

MustEncodeString

  • 说明: MustEncodeString 将任意类型 value 序列化为内容为 JSON 的 string,但如果发生任何错误,它会 panic。

  • 格式:

func MustEncodeString(value interface{}) string
  • 示例:
func ExampleMustEncodeString() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      infoData := gjson.MustEncodeString(info)
      fmt.Println(infoData)

      // Output:
      // {"Name":"John","Age":18}
}

Decode

  • 说明:Decode 将 JSON 格式的内容 data 解码为 interface{}。 参数 data 可以是 []byte 或 string。

  • 格式:

func Decode(data interface{}, options ...Options) (interface{}, error)
  • 示例:
func ExampleDecode() {
      jsonContent := `{"name":"john","score":100}`
      info, _ := gjson.Decode([]byte(jsonContent))
      fmt.Println(info)

      // Output:
      // map[name:john score:100]
}

DecodeTo

  • 说明:DecodeTo 将 JSON 格式的数据 data 解码到指定的 interface 类型的变量 v 中。参数 data 可以是 []byte 或 string。参数 v 应该是指针类型。

  • 格式:

func DecodeTo(data interface{}, v interface{}, options ...Options) (err error)
  • 示例:
func ExampleDecodeTo() {
      type BaseInfo struct {
          Name  string
          Score int
      }

      var info BaseInfo

      jsonContent := "{\"name\":\"john\",\"score\":100}"
      gjson.DecodeTo([]byte(jsonContent), &info)
      fmt.Printf("%+v", info)

      // Output:
      // {Name:john Score:100}
}

DecodeToJson

  • 说明:DecodeToJson 将 JSON 格式的数据 data 编码为 json 对象。参数 data 可以是 []byte 或 string。

  • 格式:

func DecodeToJson(data interface{}, options ...Options) (*Json, error)
  • 示例:
func ExampleDecodeToJson() {
      jsonContent := `{"name":"john","score":100}"`
      j, _ := gjson.DecodeToJson([]byte(jsonContent))
      fmt.Println(j.Map())

      // May Output:
      // map[name:john score:100]
}

SetSplitChar

  • 说明:SetSplitChar 设置数据访问的层级分隔符。

  • 格式:

func (j *Json) SetSplitChar(char byte)
  • 示例:
func ExampleJson_SetSplitChar() {
      data :=
          `{
          "users" : {
              "count" : 2,
              "list"  : [
                  {"name" : "Ming",  "score" : 60},
                  {"name" : "John", "score" : 99.5}
              ]
          }
      }`
      if j, err := gjson.DecodeToJson(data); err != nil {
          panic(err)
      } else {
          j.SetSplitChar('#')
          fmt.Println("John Score:", j.Get("users#list#1#score").Float32())
      }
      // Output:
      // John Score: 99.5
}

SetViolenceCheck

  • 说明: SetViolenceCheck 启用/禁用数据层级访问的暴力检查。

  • 格式:

func (j *Json) SetViolenceCheck(enabled bool)
  • 示例:
func ExampleJson_SetViolenceCheck() {
      data :=
          `{
          "users" : {
              "count" : 100
          },
          "users.count" : 101
      }`
      if j, err := gjson.DecodeToJson(data); err != nil {
          fmt.Println(err)
      } else {
          j.SetViolenceCheck(true)
          fmt.Println("Users Count:", j.Get("users.count"))
      }
      // Output:
      // Users Count: 101
}

ToJson

  • 说明: ToJson 返回类型为 []byte 的 JSON 内容。

  • 格式:

func (j *Json) ToJson() ([]byte, error)
  • 示例:
func ExampleJson_ToJson() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      jsonBytes, _ := j.ToJson()
      fmt.Println(string(jsonBytes))

      // Output:
      // {"Age":18,"Name":"John"}
}

ToJsonString

  • 说明: ToJsonString 返回类型为 string 的 JSON 内容。

  • 格式:

func (j *Json) ToJsonString() (string, error)
  • 示例:
func ExampleJson_ToJsonString() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      jsonStr, _ := j.ToJsonString()
      fmt.Println(jsonStr)

      // Output:
      // {"Age":18,"Name":"John"}
}

ToJsonIndent

  • 说明: ToJsonIndent 返回类型为 []byte 的带缩进格式的 JSON 内容。

  • 格式:

func (j *Json) ToJsonIndent() ([]byte, error)
  • 示例:
func ExampleJson_ToJsonIndent() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      jsonBytes, _ := j.ToJsonIndent()
      fmt.Println(string(jsonBytes))

      // Output:
      //{
      //    "Age": 18,
      //    "Name": "John"
      //}
}

ToJsonIndentString

  • 说明: ToJsonIndentString 返回类型为 string 的带缩进格式的 JSON 内容。

  • 格式:

func (j *Json) ToJsonIndentString() (string, error)
  • 示例:
func ExampleJson_ToJsonIndentString() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      jsonStr, _ := j.ToJsonIndentString()
      fmt.Println(jsonStr)

      // Output:
      //{
      //    "Age": 18,
      //    "Name": "John"
      //}
}

MustToJson

  • 说明: MustToJson 返回类型为 []byte 的 JSON 内容,如果发生任何错误,会发生 panic。

  • 格式:

func (j *Json) MustToJson() []byte
  • 示例:
func ExampleJson_MustToJson() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      jsonBytes := j.MustToJson()
      fmt.Println(string(jsonBytes))

      // Output:
      // {"Age":18,"Name":"John"}
}

MustToJsonString

  • 说明: MustToJsonString 返回类型为 string 的 JSON 内容,如果发生任何错误,会发生 panic。

  • 格式:

func (j *Json) MustToJsonString() string
  • 示例:
func ExampleJson_MustToJsonString() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      jsonStr := j.MustToJsonString()
      fmt.Println(jsonStr)

      // Output:
      // {"Age":18,"Name":"John"}
}

MustToJsonIndent

  • 说明: MustToJsonStringIndent 返回类型为 []byte 的带缩进格式的 JSON 内容,如果发生任何错误,会发生 panic。

  • 格式:

func (j *Json) MustToJsonIndent() []byte
  • 示例:
func ExampleJson_MustToJsonIndent() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      jsonBytes := j.MustToJsonIndent()
      fmt.Println(string(jsonBytes))

      // Output:
      //{
      //    "Age": 18,
      //    "Name": "John"
      //}
}

MustToJsonIndentString

  • 说明: MustToJsonStringIndent 返回类型为 string 的带缩进格式的 JSON 内容,如果发生任何错误,会发生 panic。

  • 格式:

func (j *Json) MustToJsonIndentString() string
  • 示例:
func ExampleJson_MustToJsonIndentString() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      jsonStr := j.MustToJsonIndentString()
      fmt.Println(jsonStr)

      // Output:
      //{
      //    "Age": 18,
      //    "Name": "John"
      //}
}

ToXml

  • 说明: ToXml 返回类型为 []byte 格式为 XML 的内容。

  • 格式:

func (j *Json) ToXml(rootTag ...string) ([]byte, error)
  • 示例:
func ExampleJson_ToXml() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      xmlBytes, _ := j.ToXml()
      fmt.Println(string(xmlBytes))

      // Output:
      // <doc><Age>18</Age><Name>John</Name></doc>
}

ToXmlString

  • 说明: ToXmlString 返回类型为 string 格式为 XML 的内容。

  • 格式:

func (j *Json) ToXmlString(rootTag ...string) (string, error)
  • 示例:
func ExampleJson_ToXmlString() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      xmlStr, _ := j.ToXmlString()
      fmt.Println(string(xmlStr))

      // Output:
      // <doc><Age>18</Age><Name>John</Name></doc>
}

ToXmlIndent

  • 说明: ToXmlIndent 返回类型为 []byte 的带缩进格式的 XML 内容。

  • 格式:

func (j *Json) ToXmlIndent(rootTag ...string) ([]byte, error)
  • 示例:
func ExampleJson_ToXmlIndent() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      xmlBytes, _ := j.ToXmlIndent()
      fmt.Println(string(xmlBytes))

      // Output:
      //<doc>
      //    <Age>18</Age>
      //    <Name>John</Name>
      //</doc>
}

ToXmlIndentString

  • 说明: ToXmlIndentString 返回类型为 string 的带缩进格式的 XML 内容。

  • 格式:

func (j *Json) ToXmlIndentString(rootTag ...string) (string, error)
  • 示例:
func ExampleJson_ToXmlIndentString() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      xmlStr, _ := j.ToXmlIndentString()
      fmt.Println(string(xmlStr))

      // Output:
      //<doc>
      //    <Age>18</Age>
      //    <Name>John</Name>
      //</doc>
}

MustToXml

  • 说明: MustToXml 返回类型为 []byte 格式为 XML 的内容。如果发生任何错误,会发生 panic。

  • 格式:

func (j *Json) MustToXml(rootTag ...string) []byte
  • 示例:
func ExampleJson_MustToXml() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      xmlBytes := j.MustToXml()
      fmt.Println(string(xmlBytes))

      // Output:
      // <doc><Age>18</Age><Name>John</Name></doc>
}

MustToXmlString

  • 说明: MustToXmlString 返回类型为 string 格式为 XML 的内容。如果发生任何错误,会发生 panic。

  • 格式:

func (j *Json) MustToXmlString(rootTag ...string) string
  • 示例:
func ExampleJson_MustToXmlString() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      xmlStr := j.MustToXmlString()
      fmt.Println(string(xmlStr))

      // Output:
      // <doc><Age>18</Age><Name>John</Name></doc>
}

MustToXmlIndent

  • 说明: MustToXmlStringIndent 返回类型为 []byte 带缩进格式的 XML 内容。如果发生任何错误,会发生 panic。

  • 格式:

func (j *Json) MustToXmlIndent(rootTag ...string) []byte
  • 示例:
func ExampleJson_MustToXmlIndent() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      xmlBytes := j.MustToXmlIndent()
      fmt.Println(string(xmlBytes))

      // Output:
      //<doc>
      //    <Age>18</Age>
      //    <Name>John</Name>
      //</doc>
}

MustToXmlIndentString

  • 说明: MustToXmlStringIndentString 返回类型为 string 带缩进格式的 XML 内容。如果发生任何错误,会发生 panic。

  • 格式:

func (j *Json) MustToXmlIndentString(rootTag ...string) string
  • 示例:
func ExampleJson_MustToXmlIndentString() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      xmlStr := j.MustToXmlIndentString()
      fmt.Println(string(xmlStr))

      // Output:
      //<doc>
      //    <Age>18</Age>
      //    <Name>John</Name>
      //</doc>
}

ToYaml

  • 说明: ToYaml 返回类型为 []byte 格式为 YAML 的内容。

  • 格式:

func (j *Json) ToYaml() ([]byte, error)
  • 示例:
func ExampleJson_ToYaml() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      YamlBytes, _ := j.ToYaml()
      fmt.Println(string(YamlBytes))

      // Output:
      //Age: 18
      //Name: John
}

ToYamlIndent

  • 说明: ToYamlIndent 返回类型为 []byte 带缩进格式的 YAML 内容。

  • 格式:

func (j *Json) ToYamlIndent(indent string) ([]byte, error)
  • 示例:
func ExampleJson_ToYamlIndent() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      YamlBytes, _ := j.ToYamlIndent("")
      fmt.Println(string(YamlBytes))

      // Output:
      //Age: 18
      //Name: John
}

ToYamlString

  • 说明: ToYamlString 返回类型为 string 格式为 YAML 的内容。

  • 格式:

func (j *Json) ToYamlString() (string, error)
  • 示例:
func ExampleJson_ToYamlString() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      YamlStr, _ := j.ToYamlString()
      fmt.Println(string(YamlStr))

      // Output:
      //Age: 18
      //Name: John
}

MustToYaml

  • 说明: MustToYaml 返回类型为 []byte 格式为 YAML 的内容。如果发生任何错误,会发生 panic。

  • 格式:

func (j *Json) MustToYaml() []byte
  • 示例:
func ExampleJson_MustToYaml() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      YamlBytes := j.MustToYaml()
      fmt.Println(string(YamlBytes))

      // Output:
      //Age: 18
      //Name: John
}

MustToYamlString

  • 说明: MustToYamlString 返回类型为 string 格式为 YAML 的内容。如果发生任何错误,会发生 panic。

  • 格式:

func (j *Json) MustToYamlString() string
  • 示例:
func ExampleJson_MustToYamlString() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      YamlStr := j.MustToYamlString()
      fmt.Println(string(YamlStr))

      // Output:
      //Age: 18
      //Name: John
}

ToToml

  • 说明: ToToml 返回类型为 []byte 格式为 TOML 的内容。

  • 格式:

func (j *Json) ToToml() ([]byte, error)
  • 示例:
func ExampleJson_ToToml() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      TomlBytes, _ := j.ToToml()
      fmt.Println(string(TomlBytes))

      // Output:
      //Age = 18
      //Name = "John"
}

ToTomlString

  • 说明: ToTomlString 返回类型为 string 格式为 TOML 的内容。

  • 格式:

func (j *Json) ToTomlString() (string, error)
  • 示例:
func ExampleJson_ToTomlString() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      TomlStr, _ := j.ToTomlString()
      fmt.Println(string(TomlStr))

      // Output:
      //Age = 18
      //Name = "John"
}

MustToToml

  • 说明: MustToToml 返回类型为 []byte 格式为 TOML 的内容。如果发生任何错误,会发生 panic。

  • 格式:

func (j *Json) MustToToml() []byte
  • 示例:
func ExampleJson_MustToToml() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      TomlBytes := j.MustToToml()
      fmt.Println(string(TomlBytes))

      // Output:
      //Age = 18
      //Name = "John"
}

MustToTomlString

  • 说明: MustToTomlString 返回类型为 string 格式为 TOML 的内容。如果发生任何错误,会发生 panic。

  • 格式:

func (j *Json) MustToTomlString() string
  • 示例:
func ExampleJson_MustToTomlString() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      TomlStr := j.MustToTomlString()
      fmt.Println(string(TomlStr))

      // Output:
      //Age = 18
      //Name = "John"
}

ToIni

  • 说明: ToIni 返回类型为 []byte 格式为 INI 的内容。

  • 格式:

func (j *Json) ToIni() ([]byte, error)
  • 示例:
func ExampleJson_ToIni() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      IniBytes, _ := j.ToIni()
      fmt.Println(string(IniBytes))

      // May Output:
      //Name=John
      //Age=18
}

ToIniString

  • 说明: ToIniString 返回类型为 string 格式为 INI 的内容。

  • 格式:

func (j *Json) ToIniString() (string, error)
  • 示例:
func ExampleJson_ToIniString() {
      type BaseInfo struct {
          Name string
      }

      info := BaseInfo{
          Name: "John",
      }

      j := gjson.New(info)
      IniStr, _ := j.ToIniString()
      fmt.Println(string(IniStr))

      // Output:
      //Name=John
}

MustToIni

  • 说明: MustToIni 返回类型为 []byte 格式为 INI 的内容。如果发生任何错误,会发生 panic。

  • 格式:

func (j *Json) MustToIni() []byte
  • 示例:
func ExampleJson_MustToIni() {
      type BaseInfo struct {
          Name string
      }

      info := BaseInfo{
          Name: "John",
      }

      j := gjson.New(info)
      IniBytes := j.MustToIni()
      fmt.Println(string(IniBytes))

      // Output:
      //Name=John
}

MustToIniString

  • 说明: MustToIniString 返回类型为 string 格式为 INI 的内容。如果发生任何错误,会发生 panic。

  • 格式:

func (j *Json) MustToIniString() string
  • 示例:
func ExampleJson_MustToIniString() {
      type BaseInfo struct {
          Name string
      }

      info := BaseInfo{
          Name: "John",
      }

      j := gjson.New(info)
      IniStr := j.MustToIniString()
      fmt.Println(string(IniStr))

      // Output:
      //Name=John
}

MarshalJSON

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

  • 格式:

func (j Json) MarshalJSON() ([]byte, error)
  • 示例:
func ExampleJson_MarshalJSON() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      jsonBytes, _ := j.MarshalJSON()
      fmt.Println(string(jsonBytes))

      // Output:
      // {"Age":18,"Name":"John"}
}

UnmarshalJSON

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

  • 格式:

func (j *Json) UnmarshalJSON(b []byte) error
  • 示例:
func ExampleJson_UnmarshalJSON() {
      jsonStr := `{"Age":18,"Name":"John"}`

      j := gjson.New("")
      j.UnmarshalJSON([]byte(jsonStr))
      fmt.Println(j.Map())

      // Output:
      // map[Age:18 Name:John]
}

UnmarshalValue

  • 说明:UnmarshalValue 是一个为 Json 设置任何类型的值的接口实现。

  • 格式:

func (j *Json) UnmarshalValue(value interface{}) error
  • 示例:
func ExampleJson_UnmarshalValue_Yaml() {
      yamlContent :=
          `base:
    name: john
    score: 100`

      j := gjson.New("")
      j.UnmarshalValue([]byte(yamlContent))
      fmt.Println(j.Var().String())

      // Output:
      // {"base":{"name":"john","score":100}}
}
func ExampleJson_UnmarshalValue_Xml() {
      xmlStr := `<?xml version="1.0" encoding="UTF-8"?><doc><name>john</name><score>100</score></doc>`

      j := gjson.New("")
      j.UnmarshalValue([]byte(xmlStr))
      fmt.Println(j.Var().String())

      // Output:
      // {"doc":{"name":"john","score":"100"}}
}

MapStrAny

  • 说明: MapStrAny 实现了接口方法 MapStrAny()。

  • 格式:

func (j *Json) MapStrAny() map[string]interface{}
  • 示例:
func ExampleJson_MapStrAny() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      fmt.Println(j.MapStrAny())

      // Output:
      // map[Age:18 Name:John]
}

Interfaces

  • 说明: Interfaces 实现了接口方法 Interfaces()。

  • 格式:

func (j *Json) Interfaces() []interface{}
  • 示例:
func ExampleJson_Interfaces() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      infoList := []BaseInfo{
          BaseInfo{
              Name: "John",
              Age:  18,
          },
          BaseInfo{
              Name: "Tom",
              Age:  20,
          },
      }

      j := gjson.New(infoList)
      fmt.Println(j.Interfaces())

      // Output:
      // [{John 18} {Tom 20}]
}

Interface

  • 说明: Interface 返回 Json 对象的值。

  • 格式:

func (j *Json) Interface() interface{}
  • 示例:
func ExampleJson_Interface() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      fmt.Println(j.Interface())

      var nilJ *gjson.Json = nil
      fmt.Println(nilJ.Interface())

      // Output:
      // map[Age:18 Name:John]
      // <nil>
}

Var

  • 说明: Var 返回类型为 *gvar.Var 的 Json 对象的值。

  • 格式:

func (j *Json) Var() *gvar.Var
  • 示例:
func ExampleJson_Var() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      fmt.Println(j.Var().String())
      fmt.Println(j.Var().Map())

      // Output:
      // {"Age":18,"Name":"John"}
      // map[Age:18 Name:John]
}

IsNil

  • 说明: IsNil 检查Json对象值是否为 nil。

  • 格式:

func (j *Json) IsNil() bool
  • 示例:
func ExampleJson_IsNil() {
      data1 := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
      data2 := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]`)

      j1, _ := gjson.LoadContent(data1)
      fmt.Println(j1.IsNil())

      j2, _ := gjson.LoadContent(data2)
      fmt.Println(j2.IsNil())

      // Output:
      // false
      // true
}

Get

  • 说明:Get 根据指定的 pattern 检索并返回值。如果 pattern 给的是 ".",将返回当前 Json 对象的所有值。没有 pattern 没有找到,则返回 nil。

  • 格式:

func (j *Json) Get(pattern string, def ...interface{}) *gvar.Var
  • 示例:
func ExampleJson_Get() {
      data :=
          `{
          "users" : {
              "count" : 1,
              "array" : ["John", "Ming"]
          }
      }`

      j, _ := gjson.LoadContent(data)
      fmt.Println(j.Get("."))
      fmt.Println(j.Get("users"))
      fmt.Println(j.Get("users.count"))
      fmt.Println(j.Get("users.array"))

      var nilJ *gjson.Json = nil
      fmt.Println(nilJ.Get("."))

      // Output:
      // {"users":{"array":["John","Ming"],"count":1}}
      // {"array":["John","Ming"],"count":1}
      // 1
      // ["John","Ming"]
}

GetJson

  • 说明:GetJson 通过指定的 pattern 获取值,并将其转换为一个非并发安全的 Json 对象。

  • 格式:

func (j *Json) GetJson(pattern string, def ...interface{}) *Json
  • 示例:
func ExampleJson_GetJson() {
      data :=
          `{
          "users" : {
              "count" : 1,
              "array" : ["John", "Ming"]
          }
      }`

      j, _ := gjson.LoadContent(data)

      fmt.Println(j.GetJson("users.array").Array())

      // Output:
      // [John Ming]
}

GetJsons

  • 说明:GetJsons 通过指定的 pattern 获取值,并将其转换为一个非并发安全的 Json 对象切片。

  • 格式:

func (j *Json) GetJsons(pattern string, def ...interface{}) []*Json
  • 示例:
func ExampleJson_GetJsons() {
      data :=
          `{
          "users" : {
              "count" : 3,
              "array" : [{"Age":18,"Name":"John"}, {"Age":20,"Name":"Tom"}]
          }
      }`

      j, _ := gjson.LoadContent(data)

      jsons := j.GetJsons("users.array")
      for _, json := range jsons {
          fmt.Println(json.Interface())
      }

      // Output:
      // map[Age:18 Name:John]
      // map[Age:20 Name:Tom]
}

GetJsonMap

  • 说明:GetJsonMap 通过指定的 pattern 获取值,并将其转换为一个非并发安全的 Json 对象的 map。

  • 格式:

func (j *Json) GetJsonMap(pattern string, def ...interface{}) map[string]*Json
  • 示例:
func ExampleJson_GetJsonMap() {
      data :=
          `{
          "users" : {
              "count" : 1,
              "array" : {
                  "info" : {"Age":18,"Name":"John"},
                  "addr" : {"City":"Chengdu","Company":"Tencent"}
              }
          }
      }`

      j, _ := gjson.LoadContent(data)

      jsonMap := j.GetJsonMap("users.array")

      for _, json := range jsonMap {
          fmt.Println(json.Interface())
      }

      // May Output:
      // map[City:Chengdu Company:Tencent]
      // map[Age:18 Name:John]
}

Set

  • 说明:Set 设置指定 pattern 的值。 它默认支持通过字符 '.' 进行数据层级访问。

  • 格式:

func (j *Json) Set(pattern string, value interface{}) error
  • 示例:
func ExampleJson_Set() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      j.Set("Addr", "ChengDu")
      j.Set("Friends.0", "Tom")
      fmt.Println(j.Var().String())

      // Output:
      // {"Addr":"ChengDu","Age":18,"Friends":["Tom"],"Name":"John"}
}

MustSet

  • 说明:MustSet 执行 Set,但如果有任何错误,会发生 panic。

  • 格式:

func (j *Json) MustSet(pattern string, value interface{})
  • 示例:
func ExampleJson_MustSet() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      j.MustSet("Addr", "ChengDu")
      fmt.Println(j.Var().String())

      // Output:
      // {"Addr":"ChengDu","Age":18,"Name":"John"}
}

Remove

  • 说明: Remove 删除指定 pattern 的值。 它默认支持通过字符 '.' 进行数据层级访问。

  • 格式:

func (j *Json) Remove(pattern string) error
  • 示例:
func ExampleJson_Remove() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      j.Remove("Age")
      fmt.Println(j.Var().String())

      // Output:
      // {"Name":"John"}
}

MustRemove

  • 说明:MustRemove 执行Remove,但如果有任何错误,会发生 panic。

  • 格式:

func (j *Json) MustRemove(pattern string)
  • 示例:
func ExampleJson_MustRemove() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      j.MustRemove("Age")
      fmt.Println(j.Var().String())

      // Output:
      // {"Name":"John"}
}

Contains

  • 说明: Contains 检查指定 pattern 的值是否存在。

  • 格式:

func (j *Json) Contains(pattern string) bool
  • 示例:
func ExampleJson_Contains() {
      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{
          Name: "John",
          Age:  18,
      }

      j := gjson.New(info)
      fmt.Println(j.Contains("Age"))
      fmt.Println(j.Contains("Addr"))

      // Output:
      // true
      // false
}

Len

  • 说明:Len 根据指定的 pattern 返回 值的长度/大小。 pattern 的值应该是 slice 或 map 的类型。 如果找不到目标值或类型无效,则返回 -1。

  • 格式:

func (j *Json) Len(pattern string) int
  • 示例:
func ExampleJson_Len() {
      data :=
          `{
          "users" : {
              "count" : 1,
              "nameArray" : ["Join", "Tom"],
              "infoMap" : {
                  "name" : "Join",
                  "age" : 18,
                  "addr" : "ChengDu"
              }
          }
      }`

      j, _ := gjson.LoadContent(data)

      fmt.Println(j.Len("users.nameArray"))
      fmt.Println(j.Len("users.infoMap"))

      // Output:
      // 2
      // 3
}

Append

  • 说明:Append 通过指定的 pattern 将值追加到 Json 对象中。 pattern 的值的类型应该是 slice。

  • 格式:

func (j *Json) Append(pattern string, value interface{}) error
  • 示例:
func ExampleJson_Append() {
      data :=
          `{
          "users" : {
              "count" : 1,
              "array" : ["John", "Ming"]
          }
      }`

      j, _ := gjson.LoadContent(data)

      j.Append("users.array", "Lily")

      fmt.Println(j.Get("users.array").Array())

      // Output:
      // [John Ming Lily]
}

MustAppend

  • 说明:MustAppend 执行 Append,但如果有任何错误,会发生 panic。

  • 格式:

func (j *Json) MustAppend(pattern string, value interface{})
  • 示例:
func ExampleJson_MustAppend() {
      data :=
          `{
          "users" : {
              "count" : 1,
              "array" : ["John", "Ming"]
          }
      }`

      j, _ := gjson.LoadContent(data)

      j.MustAppend("users.array", "Lily")

      fmt.Println(j.Get("users.array").Array())

      // Output:
      // [John Ming Lily]
}

Map

  • 说明: Map 将当前 Json 对象转换为 map[string]interface{}。 如果失败,则返回 nil。

  • 格式:

func (j *Json) Map() map[string]interface{}
  • 示例:
func ExampleJson_Map() {
      data :=
          `{
          "users" : {
              "count" : 1,
              "info" : {
                  "name" : "John",
                  "age" : 18,
                  "addr" : "ChengDu"
              }
          }
      }`

      j, _ := gjson.LoadContent(data)

      fmt.Println(j.Get("users.info").Map())

      // Output:
      // map[addr:ChengDu age:18 name:John]
}

Array

  • 说明:Array 将当前 Json 对象转换为 []interface{}。 如果失败,则返回 nil。

  • 格式:

func (j *Json) Array() []interface{}
  • 示例:
func ExampleJson_Array() {
      data :=
          `{
          "users" : {
              "count" : 1,
              "array" : ["John", "Ming"]
          }
      }`

      j, _ := gjson.LoadContent(data)

      fmt.Println(j.Get("users.array"))

      // Output:
      // ["John","Ming"]
}

Scan

  • 说明: Scan 自动调用 Struct 或 Structs 函数根据参数 pointer 的类型来进行转换。

  • 格式:

func (j *Json) Scan(pointer interface{}, mapping ...map[string]string) error
  • 示例:
func ExampleJson_Scan() {
      data := `{"name":"john","age":"18"}`

      type BaseInfo struct {
          Name string
          Age  int
      }

      info := BaseInfo{}

      j, _ := gjson.LoadContent(data)
      j.Scan(&info)

      fmt.Println(info)

      // May Output:
      // {john 18}
}

Dump

  • 说明: Dump 以可读性更高的方式打印 Json 对象。

  • 格式:

func (j *Json) Dump()
  • 示例:
func ExampleJson_Dump() {
      data := `{"name":"john","age":"18"}`

      j, _ := gjson.LoadContent(data)
      j.Dump()

      // May Output:
      //{
      //    "name": "john",
      //    "age":  "18",
      //}
}

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.