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

go web之一:hello world快速上手+handle(http.Handle和http.HandleFunc的区别与联系)

前情提要:

需要安装好go的环境和VSCode的go插件。

hello world快速上手

1、创建go.mod

在项目根目录下打开命令行,或者直接用VSCode中的终端。输入命令

go mod init github.com/solenovex/web-tutorial

然后就能看到项目结构中多了一个go.mod

2、编写main.go

package mainimport 	"net/http"func main() {//HandleFunc有两个参数,第一个参数相当于一个路由地址。写“/”表示监听的根地址//第二个参数是个回调函数。函数内也有两个参数,第一个参数w是用来写响应的//第二个参数r会把传入请求的所有信息都包裹在里面。http.HandleFunc("/", func (w http.ResponseWriter, r *http.Request)  {w.Write([]byte("hello world"))})//监听请求的地址和端口 相当于一个路由器http.ListenAndServe("localhost:8080", nil)
}

3、go run main.go运行项目

在项目根目录下打开命令行,或者直接用VSCode中的终端。输入命令:

go run main.go

 然后打开浏览器输入相应地址,可以看到hello world。

http.Server:

http.Server 这是一个 struct,里面有若干字段。以下是常用字段

Addr 字段表示网络地址 如果为“”,那么就是所有网络接口的 80 端口

Handler 字段 如果为 nil,那么就是 DefaultServeMux ListenAndServe() 函数。

其封装好的语句为如下:

http.ListenAndServe("localhost:8080", nil)

底层源码如下:

server := http.Server{Addr:    "localhost:8080",Handler: nil,
}
server.ListenAndServe()

所以这两种代码是等效的,第一种更简洁,第二种灵活性更强。

handler:

handler 是一个接口(interface)

handler 里面只定义了一个方法 ServeHTTP()

参数一:HTTPResponseWriter 参数二:指向 Request 这个 struct 的指针  

源码:   type Handler interface {ServeHTTP(ResponseWriter, *Request)   }

package mainimport "net/http"type myHandler struct{}func (m *myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {w.Write([]byte("hello world"))
}func main() {mh := myHandler{}//以下五行就和http.ListenAndServe("localhost:8080", &mh)效果一样,是其源码。server := http.Server{Addr:    "localhost:8080",Handler: &mh,}server.ListenAndServe()}

http.Handle: 

该函数可以实现多个handler注册到DefaultServeMux上的效果,从而达到一个路径对应一个请求,一个处理逻辑:

package mainimport "net/http"type helloHandler struct{}func (m *helloHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {w.Write([]byte("hello world"))
}type aboutHandler struct{}func (m *aboutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {w.Write([]byte("about"))
}func main() {mh := helloHandler{}ah := aboutHandler{}server := http.Server{Addr:    "localhost:8080",Handler: nil,}http.Handle("/hello", &mh)http.Handle("/about", &ah)server.ListenAndServe()
}

 

 

http.HandleFunc

func HandleFunc(pattern string, handler func(ResponseWriter, *Request))

package mainimport "net/http"func welcome(w http.ResponseWriter, r *http.Request) {w.Write([]byte("welcome"))
}func main() {server := http.Server{Addr:    "localhost:8080",Handler: nil,}http.HandleFunc("/home", func(w http.ResponseWriter, r *http.Request) {w.Write([]byte("home"))})http.HandleFunc("/welcome", welcome)//相当于http.Handle("/welcome", http.HandlerFunc(welcome))server.ListenAndServe()
}

ps:Go 有一个函数类型:HandlerFunc。可以将某个具有适当签名的函数 f,适配成为一个 Handler,而这个 Handler 具有方法 f。

两者区别:


​​​​​​​

http://www.lryc.cn/news/149735.html

相关文章:

  • 【Postman】postman生成测试报告完整步骤(包含命令与newman安装教程链接)
  • 一、C#—概述环境安装(1)
  • C# 实现ComboBox下拉框控件
  • leetcode做题笔记119. 杨辉三角 II
  • Dolphin for Mac(Wii游戏模拟器)配置指南
  • Java,Linux,Mysql小白入门
  • 代码随想录算法训练营第二十四天|理论基础 77. 组合
  • macos安装zsh
  • 【Unity】预制体材质变(Clone)克隆体问题
  • python“魂牵”京东商品历史价格数据接口(含代码示例)
  • 密码算法、密钥体系---安全行业基础篇1
  • Java工具类记录
  • DVWA靶场搭建
  • Uniapp笔记(二)uniapp语法1
  • 【1day】PHPOK cms SQL注入学习
  • 线程同步与互斥
  • 电子词典dictionary
  • 【python爬虫】10.指挥浏览器自动工作(selenium)
  • QT文件对话框,将标签内容保存至指定文件
  • C#,《小白学程序》第十一课:阶乘(Factorial)的计算方法与代码
  • MySQL 数据库常用命令大全(完整版)
  • 【数学】【书籍阅读笔记】【概率论】应用随机过程概率论模型导论 by Sheldon M.Ross 第一章 概率论引总结与习题题解 【更新中】
  • posexplode函数实战总结
  • QTday3(对话框、发布软件、事件处理核心机制)
  • el-date-picker限制选择的时间范围
  • Scala中的Actor模型
  • Java使用pdfbox将pdf转图片
  • 大规模场景下对Istio的性能优化
  • 数字化新零售平台系统提供商,门店商品信息智慧管理-亿发进销存
  • postgresql-窗口函数