第一章節 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
中一種經典的攔截器,后續章節中會有詳細講解,這里暫不做介紹。
運行后,我們嘗試訪問 <