第一章节 GoFrame
是一款基础设施建设比较完善的模块化框架
GoFrame
是一款基础设施建设比较完善的模块化框架, Web Server
模块是其中比较核心的模块,我们这里将 Web
服务开发作为框架入门的选择,便于大家更容易学习和理解。
用GOland编写代码
go.mod
module goframeProjectgo 1.24require github.com/gogf/gf/v2 v2.9.0require (github.com/BurntSushi/toml v1.4.0 // indirectgithub.com/clbanning/mxj/v2 v2.7.0 // indirectgithub.com/emirpasic/gods v1.18.1 // indirectgithub.com/fatih/color v1.18.0 // indirectgithub.com/fsnotify/fsnotify v1.7.0 // indirectgithub.com/go-logr/logr v1.4.2 // indirectgithub.com/go-logr/stdr v1.2.2 // indirectgithub.com/google/uuid v1.6.0 // indirectgithub.com/gorilla/websocket v1.5.3 // indirectgithub.com/grokify/html-strip-tags-go v0.1.0 // indirectgithub.com/kr/text v0.2.0 // indirectgithub.com/magiconair/properties v1.8.9 // indirectgithub.com/mattn/go-colorable v0.1.13 // indirectgithub.com/mattn/go-isatty v0.0.20 // indirectgithub.com/mattn/go-runewidth v0.0.16 // indirectgithub.com/olekukonko/tablewriter v0.0.5 // indirectgithub.com/rivo/uniseg v0.4.7 // indirectgo.opentelemetry.io/otel v1.32.0 // indirectgo.opentelemetry.io/otel/metric v1.32.0 // indirectgo.opentelemetry.io/otel/sdk v1.32.0 // indirectgo.opentelemetry.io/otel/trace v1.32.0 // indirectgolang.org/x/net v0.32.0 // indirectgolang.org/x/sys v0.28.0 // indirectgolang.org/x/text v0.21.0 // indirectgopkg.in/yaml.v3 v3.0.1 // indirect
)
我们先来开发一个简单的Web Server
程序。
- 新建
main.go
文件main.go
package mainimport ("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 existsD:\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 disabledADDRESS | 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
中一种经典的拦截器,后续章节中会有详细讲解,这里暂不做介绍。
运行后,我们尝试访问 <