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核心组件数据库ORMORM结果处理ORM结果处理-为空判断.md

使用 GoFrame ORM 对返回结果为空判断非常简便,大部分场景下直接判断返回的数据是否为 nil 或者长度为 0,或者使用 IsEmpty/IsNil 方法。

一、数据集合(多条)

r, err := g.Model("order").Where("status", 1).All()
if err != nil {
    return err
}
if len(r) == 0 {
    // 结果为空
}

也可以使用 IsEmpty 方法:

r, err := g.Model("order").Where("status", 1).All()
if err != nil {
    return err
}
if r.IsEmpty() {
    // 结果为空
}

二、数据记录(单条)

r, err := g.Model("order").Where("status", 1).One()
if err != nil {
    return err
}
if len(r) == 0 {
    // 结果为空
}

也可以使用 IsEmpty 方法:

r, err := g.Model("order").Where("status", 1).One()
if err != nil {
    return err
}
if r.IsEmpty() {
    // 结果为空
}

三、数据字段值

返回的是一个"泛型"变量,这个只能使用 IsEmpty 来判断是否为空了。

r, err := g.Model("order").Where("status", 1).Value()
if err != nil {
    return err
}
if r.IsEmpty() {
    // 结果为空
}

四、字段值数组

查询返回字段值数组本身类型为 []gdb.Value 类型,因此直接判断长度是否为 0 即可。

// Array/FindArray
r, err := g.Model("order").Fields("id").Where("status", 1).Array()
if err != nil {
    return err
}
if len(r) == 0 {
    // 结果为空
}

五、 Struct 对象(🔥注意🔥)

关于 Struct 转换对象来说 会有一点不一样,我们直接看例子吧。

当传递的对象 本身就是一个空指针时,如果查询到数据,那么会在内部 自动创建这个对象;如果没有查询到数据,那么这个空指针仍旧是一个空指针,内部并不会做任何处理。

var user *User
err := g.Model("order").Where("status", 1).Scan(&user)
if err != nil {
    return err
}
if user == nil {
    // 结果为空
}

当传递的对象 本身已经是一个初始化的对象,如果查询到数据,那么会在内部将数据赋值给这个对象; 如果没有查询到数据,那么此时就没办法将对象做 nil 判断空结果。因此 ORM 会返回一个 sql.ErrNoRows 错误,提醒开发者没有查询到任何数据并且对象没有做任何赋值,对象的所有属性还是给定的初始化数值,以便开发者可以做进一步的空结果判断。

var user = new(User)
err := g.Model("order").Where("status", 1).Scan(&user)
if err != nil && err != sql.ErrNoRows {
    return err
}
if err == sql.ErrNoRows {
    // 结果为空
}

:::tip 所以我们推荐开发者不要传递一个初始化过后的对象给 ORM,而是直接传递一个对象的指针的指针类型( **struct 类型), ORM 内部会根据查询结果智能地做自动初始化。 :::

六、 Struct 数组

当传递的对象数组本身是一个空数组(长度为 0),如果查询到数据,那么会在内部自动赋值给数组;如果没有查询到数据,那么这个空数组仍旧是一个空数组,内部并不会做任何处理。

var users []*User
err := g.Model("order").Where("status", 1).Scan(&users)
if err != nil {
    return err
}
if len(users) == 0 {
    // 结果为空
}

当传递的对象数组本身不是空数组,如果查询到数据,那么会在内部自动从索引 0 位置覆盖到数组上;如果没有查询到数据,那么此时就没办法将数组做长度为 0 判断空结果。因此 ORM 会返回一个 sql.ErrNoRows 错误,提醒开发者没有查询到任何数据并且数组没有做任何赋值,以便开发者可以做进一步的空结果判断。

var users = make([]*User, 100)
err := g.Model("order").Where("status", 1).Scan(&users)
if err != nil {
    return err
}
if err == sql.ErrNoRows {
    // 结果为空
}

:::warning 由于 struct 转换利用了 Golang 反射特性,执行性能会有一定的损耗。如果您涉及到大量查询结果数据的 struct 数组对象转换,并且需要提高转换性能,请参考自定义实现对应 struct 的 UnmarshalValue 方法: 类型转换-UnmarshalValue :::

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.