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

NewWithOption自定义错误创建

  • 说明:用于自定义配置的错误对象创建。

  • 格式:

    // NewWithOption creates and returns a custom error with Option.
    // It is the senior usage for creating error, which is often used internally in framework.
    NewWithOption(option Option) error
  • 示例:

    func ExampleNewWithOption() {
         err := gerror.NewWithOption(gerror.Option{
              Text: "this feature is disabled in this storage",
              Code: gcode.CodeNotSupported,
         })
    }

    其中,Option的定义如下:

    // Option is option for creating error.
    type Option struct {
         Error error      // Wrapped error if any.
         Stack bool       // Whether recording stack information into error.
         Text  string     // Error text, which is created by New* functions.
         Code  gcode.Code // Error code if necessary.
    }

fmt格式化

通过前面示例我们可以看到,通过%+v的打印格式可以打印出完整的堆栈信息,当然gerror.Error对象支持多种fmt格式:

格式符 输出内容
%v, %s 打印所有的层级错误信息,构成完成的字符串返回,多个层级使用 : 拼接。
%-v, %-s 打印当前层级的错误信息,返回字符串。
%+s 打印完整的堆栈信息列表。
%+v 打印所有的层级错误信息字符串,以及完整的堆栈信息,等同于 %s\n%+s。

使用示例:

package main

import (
    "errors"
    "fmt"
    "github.com/gogf/gf/v2/errors/gerror"
)

func main() {
    var err error
    err = errors.New("sql error")
    err = gerror.Wrap(err, "adding failed")
    err = gerror.Wrap(err, "api calling failed")
    fmt.Printf(" %%s: %s\n", err)
    fmt.Printf("%%-s: %-s\n", err)
    fmt.Println("%+s: ")
    fmt.Printf("%+s\n", err)
}

执行后,输出示例:

 %s: api calling failed: adding failed: sql error
%-s: api calling failed
%+s:
1. api calling failed
    1).  main.main
        /Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/other/test.go:14
2. adding failed
    1).  main.main
        /Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/other/test.go:13
3. sql error

日志组件支持

glog日志组件天然支持对gerror错误堆栈打印支持,这种支持不是强耦合性的,而是通过fmt格式化打印接口支持的。

使用示例:

package main

import (
    "errors"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/errors/gerror"
)

func main() {
    var err error
    err = errors.New("sql error")
    err = gerror.Wrap(err, "adding failed")
    err = gerror.Wrap(err, "api calling failed")
    g.Log().Printf("%+v", err)
}

执行后,输出示例:

2020-10-17 15:22:26.793 api calling failed: adding failed: sql error
1. api calling failed
    1).  main.main
        /Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/other/test.go:14
2. adding failed
    1).  main.main
        /Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/other/test.go:13
3. sql error

堆栈打印模式

该特性从框架v2.6.0版本开始提供。

错误组件在打印堆栈信息时,支持使用者通过环境变量(GF_GERROR_STACK_MODE)或者命令行启动参数(gf.gerror.stack.mode)指定堆栈打印信息模式:

堆栈模式 是否默认 说明
brief 是 简略模式。错误堆栈打印时,不会打印框架相关的堆栈。
detail 详情模式。错误堆栈打印时,会打印完整的框架组件代码调用链路。

在详情模式( detail)下,将会打印错误对象中完整的框架堆栈信息。 比如,在以下错误堆栈示例中,可以看到,大部分框架堆栈信息对于错误定位来说,其实是没有太大意义的,作为开发者,更多关心的自身代码层面的堆栈信息。

2022-10-08 21:07:00.751 [ERRO] {328d1204e2191c179a09086890c857b8} request done, cost: 3 ms, code: -1, message: "", detail: <nil>, error: GetParams failed: {ResourceId:tdxxxx-a2c378bd Component: Version:0}: rpc error: code = NotFound desc = cluster.khaos.tencent.com "tdxxxx-a2c378bd" not found
1. GetParams failed: {ResourceId:tdxxxx-a2c378bd Component: Version:0}
   1).  github.com/khaos/eros/app/khaos-oss/internal/logic/params.(*sParams).doGetParamsJson
        /root/workspace/khaos/eros/app/khaos-oss/internal/logic/params/params.go:66
   2).  github.com/khaos/eros/app/khaos-oss/internal/logic/params.(*sParams).GetParams
        /root/workspace/khaos/eros/app/khaos-oss/internal/logic/params/params.go:36
   3).  github.com/khaos/eros/app/khaos-oss/internal/controller.(*cParams).GetOne
        /root/workspace/khaos/eros/app/khaos-oss/internal/controller/params.go:21
   4).  github.com/gogf/gf/v2/net/ghttp.(*middleware).callHandlerFunc.func1
        /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:152
   5).  github.com/gogf/gf/v2/net/ghttp.niceCallFunc
        /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_func.go:55
   6).  github.com/gogf/gf/v2/net/ghttp.(*middleware).callHandlerFunc
        /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:129
   7).  github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1
        /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:75
   8).  github.com/gogf/gf/v2/util/gutil.TryCatch
        /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/util/gutil/gutil.go:56
   9).  github.com/gogf/gf/v2/net/ghttp.(*middleware).Next
        /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:49
   10). github.com/khaos/eros/app/khaos-oss/internal/logic/middleware.(*sMiddleware).CheckLimit
        /root/workspace/khaos/eros/app/khaos-oss/internal/logic/middleware/middleware.go:27
   11). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1.5
        /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:96
   12). github.com/gogf/gf/v2/net/ghttp.niceCallFunc
        /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_func.go:55
   13). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1
        /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:95
   14). github.com/gogf/gf/v2/util/gutil.TryCatch
        /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/util/gutil/gutil.go:56
   15). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next
        /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:49
   16). github.com/khaos/eros/utility/server.MiddlewareCommonResponse
        /root/workspace/khaos/eros/utility/server/server_common.go:14
   17). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1.5
        /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:96
   18). github.com/gogf/gf/v2/net/ghttp.niceCallFunc
        /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_func.go:55
   19). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1
        /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:95
   20). github.com/gogf/gf/v2/util/gutil.TryCatch
        /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/util/gutil/gutil.go:56
   21). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next
        /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:49
   22). github.com/khaos/eros/utility/server.MiddlewareLogging
        /root/workspace/khaos/eros/utility/server/server.go:46
   23). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1.5
        /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:96
   24). github.com/gogf/gf/v2/net/ghttp.niceCallFunc
        /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_func.go:55
   25). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1
        /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:95
   26). github.com/gogf/gf/v2/util/gutil.TryCatch
        /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/util/gutil/gutil.go:56
   27). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next
        /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:49
   28). github.com/gogf/gf/v2/net/ghttp.MiddlewareCORS
        /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_middleware_cors.go:12
   29). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1.5
        /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:96
   30). github.com/gogf/gf/v2/net/ghttp.niceCallFunc
        /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_func.go:55
2. rpc error: code = NotFound desc = cluster.khaos.tencent.com "tdxxxx-a2c378bd" not found

而在简略模式( brief)下,堆栈信息中将不会打印框架的堆栈信息。例如:

2022-10-08 21:07:00.751 [ERRO] {328d1204e2191c179a09086890c857b8} request done, cost: 3 ms, code: -1, message: "", detail: <nil>, error: GetParams failed: {ResourceId:tdxxxx-a2c378bd Component: Version:0}: rpc error: code = NotFound desc = cluster.khaos.tencent.com "tdxxxx-a2c378bd" not found
1. GetParams failed: {ResourceId:tdxxxx-a2c378bd Component: Version:0}
   1).  github.com/khaos/eros/app/khaos-oss/internal/logic/params.(*sParams).doGetParamsJson
        /root/workspace/khaos/eros/app/khaos-oss/internal/logic/params/params.go:66
   2).  github.com/khaos/eros/app/khaos-oss/internal/logic/params.(*sParams).GetParams
        /root/workspace/khaos/eros/app/khaos-oss/internal/logic/params/params.go:36
   3).  github.com/khaos/eros/app/khaos-oss/internal/controller.(*cParams).GetOne
        /root/workspace/khaos/eros/app/khaos-oss/internal/controller/params.go:21
2. rpc error: code = NotFound desc = cluster.khaos.tencent.com "tdxxxx-a2c378bd" not found

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.