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.

examplespracticesinjectionREADME.ZH.MD

≈1.2k tokens on demand. Your agent reads this file only when SKILL.md points to it.

依赖注入示例

简介

本示例展示了如何在使用GoFrame的应用程序中实现依赖注入。

主要演示:

  1. 使用大仓模式管理代码仓库
  2. 基础依赖注入设置
  3. 使用DI的服务层实现
  4. 集成gRPC的控制器层
  5. 使用模拟依赖的单元测试

实现重点在于使核心业务逻辑易于测试:

  • 使用依赖注入分离关注点
  • 为服务依赖提供清晰的接口
  • 每一层都可以独立执行单元测试,保障代码质量

环境要求

目录结构

injection/
├── app/
│   ├── gateway/         # API网关服务,调用user grpc服务实现HTTP接口
│   │   ├── api/         # API定义
│   │   ├── internal/    # 内部实现
│   │   │   ├── cmd/         # 命令行工具
│   │   │   ├── controller/  # 控制器
│   │   │   ├── model/       # 数据模型
│   │   │   └── service/     # 业务逻辑
│   │   └── manifest/   # 配置文件
│   └── user/           # 用户微服务
│       ├── api/        # API定义
│       │   ├── entity/ # 实体定义
│       │   └── user/   # 用户API pb
│       ├── internal/   # 内部实现
│       │   ├── cmd/         # 命令行工具
│       │   ├── controller/  # 控制器
│       │   ├── dao/         # 数据访问对象
│       │   ├── model/       # 数据模型
│       │   └── service/     # 业务逻辑
│       └── manifest/   # 配置文件
├── hack/               # 开发工具
└── utility/            # 通用工具
    ├── injection/      # DI工具
    └── mongohelper/    # MongoDB辅助工具

功能特性

  • 依赖注入使用
  • MongoDB和Redis集成
  • gRPC服务实现
  • 完整的单元测试
  • 清理资源的关闭处理
  • 命名依赖支持

安装设置

  1. 克隆仓库:

    git clone https://github.com/gogf/examples.git
    cd examples/practices/injection
  2. 安装依赖:

    go mod tidy
  3. 使用Docker启动所需服务:

    # 启动MongoDB
    docker run -d --name mongo -p 27017:27017 mongo:latest
    
    # 启动Redis
    docker run -d --name redis -p 6379:6379 redis:latest

使用方法

  1. 运行gRPC Server服务:

    cd examples/practices/injection/app/user
    go run main.go server
  2. 运行HTTP Server服务:

    cd examples/practices/injection/app/gateway
    go run main.go server
  3. (可选)运行异步守护进程,仅演示多命令功能,无实际逻辑:

    cd examples/practices/injection/app/gateway
    go run main.go worker
  4. (可选)运行测试:

    go test ./...

实现细节

依赖注入设置

  • 使用github.com/samber/do包进行依赖管理
  • 支持命名和未命名依赖管理
  • 提供常用操作的辅助函数

服务层

  • 清晰的关注点分离
  • 基于接口的设计
  • 易于使用模拟实现进行测试

控制器层

  • 支持DI的gRPC集成
  • 清晰的错误处理
  • 合理的资源管理

可测试性

  • 数据层(dao)、业务层(service)、接口层(controller)完整的单元测试
  • 依赖模拟,单元测试使用的数据库配置通过manifest/config管理
  • 通过配置文件管理微服务链接的服务地址或者域名,以便于控制依赖的服务模拟

注意事项

  • 依赖注入的注册时,需要在Shutdown方法中正确清理资源
  • 使用依赖注入时,如果需要同类型的多个实例时使用命名依赖

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.