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

前情提要:

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

hello world快速上手

1、创建go.mod

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

bash 复制代码
go mod init github.com/solenovex/web-tutorial

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

2、编写main.go

Go 复制代码
package main

import 	"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中的终端。输入命令:

bash 复制代码
go run main.go

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

http.Server:

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

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

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

其封装好的语句为如下:

Go 复制代码
http.ListenAndServe("localhost:8080", nil)

底层源码如下:

Go 复制代码
server := http.Server{
   Addr:    "localhost:8080",
   Handler: nil,
}
server.ListenAndServe()

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

handler:

handler 是一个接口(interface)

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

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

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

Go 复制代码
package main

import "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上的效果,从而达到一个路径对应一个请求,一个处理逻辑:

Go 复制代码
package main

import "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))

Go 复制代码
package main

import "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。

两者区别:

​​​​​​​

相关推荐
Fcy648几秒前
C++ 模版(进阶)(含array解析)
开发语言·c++·stl·array·模版
OKkankan6 分钟前
多态概念及使用
开发语言·数据结构·c++·算法
hudawei9966 分钟前
kotlin协程编译成Android字节码后是什么样的
android·开发语言·kotlin
小周码代码17 分钟前
js 数字金额转为大写 js 金额转大写
开发语言·前端·javascript·js工具
MacroZheng17 分钟前
取代Navicat!全新一代数据库管理工具来了,超级智能!
java·后端·mysql
行走在电子领域的工匠19 分钟前
台达ST:自定义串行通讯传送与接收指令COMRS程序范例五
开发语言·台达plc·st语言编程·串口自定义协议
w***i29420 分钟前
Spring Boot实现定时任务
java·spring boot·后端
William_cl25 分钟前
【ASP.NET进阶】Controller 层 Action 核心:异步 Action(async Task)全解析
后端·asp.net
BillKu28 分钟前
html2pdf.js使用与配置详解
开发语言·javascript·ecmascript
i***279532 分钟前
springboot集成onlyoffice(部署+开发)
java·spring boot·后端