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快速开始Hello World.md

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

GoFrame 是一款基础设施建设比较完善的模块化框架, Web Server 模块是其中比较核心的模块,我们这里将 Web 服务开发作为框架入门的选择,便于大家更容易学习和理解。

Hello World

我们先来开发一个简单的Web Server程序。

  • 新建main.go文件
    package main
    
    import (
        "github.com/gogf/gf/v2/frame/g"
        "github.com/gogf/gf/v2/net/ghttp"
    )
    
    func main() {
        s := g.Server()
        s.BindHandler("/", func(r *ghttp.Request) {
            r.Response.Write("Hello World!")
        })
        s.SetPort(8000)
        s.Run()
    }
  • 配置go mod并安装依赖
    go mod init main
    go mod tidy

我们来看看这段代码:

  • 任何时候,您都可以通过 g.Server() 方法获得一个默认的 Server 对象,该方法采用单例模式设计, 也就是说,多次调用该方法,返回的是同一个 Server 对象。其中的g组件是框架提供的一个耦合组件,封装和初始化一些常用的组件对象,为业务项目提供便捷化的使用方式。
  • 通过Server对象的BindHandler方法绑定路由以及路由函数。在本示例中,我们绑定了/路由,并指定路由函数返回Hello World。
  • 在路由函数中,输入参数为当前请求对象r *ghttp.Request,该对象包含当前请求的上下文信息。在本示例中,我们通过r.Response返回对象直接Write返回结果信息。
  • 通过SetPort方法设置当前Server监听端口。在本示例中,我们监听8000端口,如果在没有设置端口的情况下,它默认会监听一个随机的端口。
  • 通过 Run() 方法阻塞执行 Server 的监听运行。

执行结果

运行该程序,您将在终端看到类似以下日志信息:

$ go run main.go
2024-10-27 21:30:39.412 [INFO] pid[58889]: http server started listening on [:8000]
2024-10-27 21:30:39.412 [INFO] {08a0b0086e5202184111100658330800} openapi specification is disabled

  ADDRESS | METHOD | ROUTE |     HANDLER     | MIDDLEWARE  
----------|--------|-------|-----------------|-------------
  :8000   | ALL    | /     | main.main.func1 |             
----------|--------|-------|-----------------|-------------

在默认的日志打印中包含以下信息:

  • 当前进程号58889,以及监听的地址:8000(表示监听本机所有IP地址的8000端口)。

  • 由于框架带有自动接口文档生成功能,本示例中未启用,因此提示openapi specification is disabled。 关于接口文档的自动生成,在开发手册中对应章节会详细讲解,本示例不作介绍。

  • 最后会打印当前Server的路由列表。由于我们只监听了/路由,那么这里只打印了一个路由信息。在路由信息表中:

    路由字段 字段描述
    ADDRESS 表示该路由的监听地址,同一个进程可以同时运行多个Server,不同的Server可以监听不同的地址。
    METHOD 表示路由监听的HTTP Method信息,比如GET/POST/PUT/DELETE等。这里的ALL标识监听所有的HTTP Method。
    ROUTE 表示监听的具体路由地址信息。
    HANDLER 表示路由函数的名称。由于本示例使用的是闭包函数,因此看到的是一个临时函数名称main.main.func1。
    MIDDLEWARE 表示绑定到当前路由的中间件函数名称,中间件是Server中一种经典的拦截器,后续章节中会有详细讲解,这里暂不做介绍。

运行后,我们尝试访问 http://127.0.0.1:8000/ 您将在页面中看到输出

学习小结

太棒了!您使用GoFrame框架开发第一个Web Server程序!

下一步,我们将会学习如何在Web Server中获取客户端提交的参数数据。

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.