当前位置: 首页 > news >正文

GoFrame 奉孝学习笔记

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

运行后,我们尝试访问 <

相关文章:

  • PyTorch_指定运算设备 (包含安装 GPU 的 PyTorch)
  • HybridCLR 详解:Unity 全平台原生 C# 热更新方案
  • 【言语理解】中心理解题目之结构分析
  • 安卓基础(MediaProjection)
  • 基于Springboot旅游网站系统【附源码】
  • rails 8 CSS不起效问题解决
  • 华为云Flexus+DeepSeek征文|DeepSeek-V3商用服务开通教程
  • GCD 深入解析:从使用到底层实现
  • 数据库=====
  • 数字信号处理学习笔记--Chapter 0 数字信号处理概述
  • 【深度学习】典型的 CNN 网络
  • 力扣-字符串-165 比较版本号
  • P4552 [Poetize6] IncDec Sequence 题解
  • 玩转Docker | 使用Docker部署AI证件照工具
  • ARM 算数指令
  • 鼠标悬浮特效:常见6种背景类悬浮特效
  • 如何在 CentOS 7 命令行连接 Wi-Fi?如何在 Linux 命令行连接 Wi-Fi?
  • 蟋蟀的叫声,大自然的温度计
  • 网络Tips20-002
  • 多多铃声 7.4| 拥有丰富的铃声曲库,满足不同用户的个性化需求,支持一键设置手机铃声
  • 民生访谈|摆摊设点、公园搭帐篷、行道树飘絮,管理难题怎么解?
  • 金融政策支持稳市场稳预期发布会即将召开,潘功胜、李云泽、吴清将出席
  • 微软通讯软件Skype正式停止运营:斥资85亿美元购入,月活用户曾超3亿
  • 怎样正确看待体脂率数据?或许并不需要太“执着”
  • 南方地区强降雨或致部分河流发生超警洪水,水利部部署防范
  • 中国首位、亚洲首位!赵心童夺得斯诺克世锦赛冠军