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核心组件模板引擎模板引擎-模板标签.md

模板引擎默认使用了{{和}}作为左右闭合标签,开发者可通过gview.SetDelimiters方法设置自定义的模板闭合标签。

使用.来访问当前对象的值(模板局部变量)。

使用$来引用当前模板根级的上下文(模板全局变量)。

使用$var来访问特定的模板变量。

模板中支持的go语言符号

{{"string"}}     // 一般 string
{{`raw string`}} // 原始 string
{{'c'}}          // byte
{{print nil}}    // nil 也被支持

模板中的 pipeline

可以是上下文的变量输出,也可以是函数通过管道传递的返回值

{{. | FuncA | FuncB | FuncC}}

当 pipeline 的值等于:

  • false 或 0
  • nil的指针 或 interface
  • 长度为 0 的 array, slice, map, string

那么这个 pipeline 被认为是空。 :::warning 需要注意:在 gf 模板引擎中,当模板中展示的指定变量不存在时,将会显示为空(标准库模板引擎会展示 <no value>)。 :::

if … else … end

{{if pipeline}}...{{end}}

if 判断时, pipeline 为空时,相当于判断为 false。

支持嵌套的循环

{{if .condition}}
    ...
{{else}}
    {{if .condition2}}
        ...
    {{end}}
{{end}}

也可以使用 else if 进行

{{if .condition}}
    ...
{{else if .condition2}}
    ...
{{else}}
    ...
{{end}}

range … end

{{range pipeline}} {{.}} {{end}}

pipeline 支持的类型为 slice, map, channel。

注意:在 range 循环内部, . 符号会被覆盖为以上类型的子元素(局部变量)。如果想在循环中访问外部变量(全局变量),请加上 $ 符号,如: {{$.Session.Name}}

此外,对应的值长度为 0 时, range 不会执行, . 不会改变。

例如,遍历 map:

{{range $key, $value := .MapContent}}
    {{$key}}:{{$value}}
{{end}}

例如,遍历 slice:

{{range $index, $elem := .SliceContent}}
    {{range $key, $value := $elem}}
        {{$key}}:{{$value}}
    {{end}}
{{end}}

with … end

{{with pipeline}}...{{end}}

with 用于重定向 pipeline

{{with .Field.NestField.SubField}}
    {{.Var}}
{{end}}

define

define 可以用来 自定义模板内容块(给一段模板内容定义一个模板名称),可用于模块定义和模板嵌套(使用在 template 标签中)。

{{define "loop"}}
    <li>{{.Name}}</li>
{{end}}

其中 loop 为该模板内容块的名称,随后可使用 template 标签调用模板:

<ul>
    {{range .Items}}
        {{template "loop" .}}
    {{end}}
</ul>

:::warning define 标签需要结合 template 标签一起使用,并且支持跨模板使用(在同一模板目录/子目录下有效,原理是使用的 ParseFiles 方法解析模板文件)。 :::

template

{{template "模板名称" pipeline}}

将对应的上下文 pipeline 传给模板,才可以在模板中调用。

注意: template 标签参数为 模板名称,而不是模板文件路径, template 标签不支持模板文件路径。 :::warning template 标签需要结合 define 标签一起使用,并且支持跨模板使用(在同一模板目录/子目录下有效,原理是使用的 ParseFiles 方法解析模板文件)。 :::

include

该标签为 gf 框架模板引擎新增标签

{{include "模板文件名(需要带完整文件名后缀)" pipeline}}

在模板中可以使用 include 标签载入其他模板(任意路径),模板文件名支持 相对路径 以及文件的系统 绝对路径。如果想要把当前模板的模板变量传递给子模板(嵌套模板),可以这样:

{{include "模板文件名(需要带完整文件名后缀)" .}}

与 template 标签的区别是: include 仅支持 文件路径,不支持 模板名称;而 tempalte 标签仅支持 模板名称,不支持 文件路径。

注释

允许多行文本注释,不允许嵌套。

{{/*
comment content
support new line
*/}}

删除空白符号

删除移除空格换行符

# 使用{{-语法去除模板内容左侧的所有空白符号, 使用-}}去除模板内容右侧的所有空白符号。

# 注意:-要紧挨{{和}},同时与模板值之间需要使用空格分隔。

{{- .Name -}}

{{- range $key, $value := .list}}
  "{{$value}}"
{{- end}}

更多使用说明

template package - html/template - Go Packages

template package - text/template - Go Packages

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.