GoFrame 奉孝学习笔记

第一章节 GoFrame

是一款基础设施建设比较完善的模块化框架

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

用GOland编写代码

go.mod

module goframeProject

go 1.24

require github.com/gogf/gf/v2 v2.9.0

require (
	github.com/BurntSushi/toml v1.4.0 // indirect
	github.com/clbanning/mxj/v2 v2.7.0 // indirect
	github.com/emirpasic/gods v1.18.1 // indirect
	github.com/fatih/color v1.18.0 // indirect
	github.com/fsnotify/fsnotify v1.7.0 // indirect
	github.com/go-logr/logr v1.4.2 // indirect
	github.com/go-logr/stdr v1.2.2 // indirect
	github.com/google/uuid v1.6.0 // indirect
	github.com/gorilla/websocket v1.5.3 // indirect
	github.com/grokify/html-strip-tags-go v0.1.0 // indirect
	github.com/kr/text v0.2.0 // indirect
	github.com/magiconair/properties v1.8.9 // indirect
	github.com/mattn/go-colorable v0.1.13 // indirect
	github.com/mattn/go-isatty v0.0.20 // indirect
	github.com/mattn/go-runewidth v0.0.16 // indirect
	github.com/olekukonko/tablewriter v0.0.5 // indirect
	github.com/rivo/uniseg v0.4.7 // indirect
	go.opentelemetry.io/otel v1.32.0 // indirect
	go.opentelemetry.io/otel/metric v1.32.0 // indirect
	go.opentelemetry.io/otel/sdk v1.32.0 // indirect
	go.opentelemetry.io/otel/trace v1.32.0 // indirect
	golang.org/x/net v0.32.0 // indirect
	golang.org/x/sys v0.28.0 // indirect
	golang.org/x/text v0.21.0 // indirect
	gopkg.in/yaml.v3 v3.0.1 // indirect
)

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

  • 新建main.go文件

    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 Use goframeV2!")
        })
        s.SetPort(8000) //如果端口冲突,可以修改一下端口地址8088等
        s.Run()
    }
    

  • 配置go mod并安装依赖
    go mod init main
    go mod tidy
    

     可以看出执行后会进行下载依赖

  • go mod init main
    go: D:\GolandProjects\goframeProject\go.mod already exists
    
    D:\GolandProjects\goframeProject>go mod tidy
    go: downloading github.com/fatih/color v1.18.0
    go: downloading go.opentelemetry.io/otel v1.32.0
    go: downloading github.com/gorilla/websocket v1.5.3
    go: downloading go.opentelemetry.io/otel/trace v1.32.0
    go: downloading github.com/olekukonko/tablewriter v0.0.5
    go: downloading golang.org/x/net v0.32.0
    go: downloading github.com/grokify/html-strip-tags-go v0.1.0
    go: downloading go.opentelemetry.io/otel/sdk v1.32.0
    go: downloading github.com/emirpasic/gods v1.18.1
    go: downloading github.com/clbanning/mxj/v2 v2.7.0
    go: downloading github.com/fsnotify/fsnotify v1.7.0
    go: downloading github.com/mattn/go-colorable v0.1.13
    go: downloading golang.org/x/sys v0.28.0
    go: downloading github.com/mattn/go-runewidth v0.0.16
    go: downloading github.com/rogpeppe/go-internal v1.13.1
    go: downloading github.com/rivo/uniseg v0.4.7
    go: downloading go.opentelemetry.io/otel/metric v1.32.0
    go: downloading github.com/go-logr/logr v1.4.2
    go: downloading github.com/go-logr/stdr v1.2.2
    go: finding module for package github.com/kr/text
    go: found github.com/kr/text in github.com/kr/text v0.2.0
    

我们来看看这段代码:

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

执行结果

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

windows环境会提示需要访问外网。点击确定就OK。

$ 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中一种经典的拦截器,后续章节中会有详细讲解,这里暂不做介绍。

运行后,我们尝试访问 <

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值