当前位置:网站首页>使用Go语言构建Web服务器
使用Go语言构建Web服务器
2022-04-23 02:26:00 【田土豆】
先看一个简单的例子,如何通过Go语言搭建一个简易的Web服务,再由浅入深,介绍实现细节
一个简单的Web服务器
package main
import (
"fmt"
"log"
"net/http"
)
func HelloWorld(w http.ResponseWriter, r *http.Request) {
_, err := fmt.Fprintf(w, "Hello World!")
if err != nil {
log.Panic(err)
}
}
func main() {
//设置访问路由,这里是当url中的路径为/hello时,服务器调用HelloWorld函数(可统一称为handler函数)
http.HandleFunc("/hello", HelloWorld)
//web监听的端口是8080
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Panic(err)
}
}
这里web监听的端口是8080,运行一下程序。然后打开浏览器,输入http://127.0.0.1:8080/hello,会得到如下结果

Web服务器的工作原理
1、首先看HandleFunc函数,看注释大概知道HandleFunc就是用来注册handler函数(也可以叫app,对应之前的HelloWorld函数,用来处理用户请求,并返回结果),两个入参,一个pattern(简单理解为url里的路径),一个handler函数
// HandleFunc registers the handler function for the given pattern
// in the DefaultServeMux.
// The documentation for ServeMux explains how patterns are matched.
func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
DefaultServeMux.HandleFunc(pattern, handler)
}
2、DefaultServerMux是一个ServeMux结构体实例,主要就是用来存储pattern和handler之间的对应关系
type ServeMux struct {
mu sync.RWMutex
m map[string]muxEntry
es []muxEntry // slice of entries sorted from longest to shortest.
hosts bool // whether any patterns contain hostnames
}
type muxEntry struct {
h Handler
pattern string
}
// DefaultServeMux is the default ServeMux used by Serve.
var DefaultServeMux = &defaultServeMux
var defaultServeMux ServeMux
3、HandleFunc函数调用了ServeMux实现的方法HandleFunc
// HandleFunc registers the handler function for the given pattern
// in the DefaultServeMux.
// The documentation for ServeMux explains how patterns are matched.
func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
DefaultServeMux.HandleFunc(pattern, handler)
}
func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
if handler == nil {
panic("http: nil handler")
}
mux.Handle(pattern, HandlerFunc(handler))
}
注:mux.Handle(pattern, HandlerFunc(handler))这行里的HandlerFunc是一个函数类型,将handler强制类型转换
// The HandlerFunc type is an adapter to allow the use of
// ordinary functions as HTTP handlers. If f is a function
// with the appropriate signature, HandlerFunc(f) is a
// Handler that calls f.
type HandlerFunc func(ResponseWriter, *Request)
4、ServeMux实现的Handle方法,主要就是利用字段m(map[string]muxEntry)存储pattern和handler之间的关系,键是pattern,值是handler
// Handle registers the handler for the given pattern.
// If a handler already exists for pattern, Handle panics.
func (mux *ServeMux) Handle(pattern string, handler Handler) {
mux.mu.Lock()
defer mux.mu.Unlock()
if pattern == "" {
panic("http: invalid pattern")
}
if handler == nil {
panic("http: nil handler")
}
if _, exist := mux.m[pattern]; exist {
panic("http: multiple registrations for " + pattern)
}
if mux.m == nil {
mux.m = make(map[string]muxEntry)
}
e := muxEntry{
h: handler, pattern: pattern}
mux.m[pattern] = e
if pattern[len(pattern)-1] == '/' {
mux.es = appendSorted(mux.es, e)
}
if pattern[0] != '/' {
mux.hosts = true
}
}
5、ListenAndServe函数就是监听并响应请求
func ListenAndServe(addr string, handler Handler) error {
server := &Server{
Addr: addr, Handler: handler}
return server.ListenAndServe()
}
入参有两个,一个是监听地址,一个是handler,注意这里的handler是指mux(路由器,用来存储pattern和app之间的关系)。如果handler为nil,说明默认使用DefautServeMux,下面我们也可以自己构造一个mux
package main
import (
"fmt"
"log"
"net/http"
)
func HelloWorld(w http.ResponseWriter, r *http.Request) {
_, err := fmt.Fprintf(w, "Hello World!")
if err != nil {
log.Panic(err)
}
}
func main() {
//自定义一个mux
mux := new(http.ServeMux)
//注册函数
mux.HandleFunc("/hello", HelloWorld)
if err := http.ListenAndServe(":8080", mux); err != nil {
log.Panic(err)
}
}
运行一下程序,然后打开浏览器,输入http://127.0.0.1:8080/hello,同样会得到预期结果

版权声明
本文为[田土豆]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_42216109/article/details/123538893
边栏推荐
- [nk]牛客月赛48 D
- [assembly language] understand "stack" from the lowest point of view
- Latin goat (20204-2022) - daily question 1
- 【Chrome扩展程序】content_script的跨域问题
- Flink real-time data warehouse project - Design and implementation of DWS layer
- leetcode:27. Remove element [count remove]
- 006_redis_SortedSet类型
- Dynamic memory management
- Why is one plus one equal to two
- OJ daily practice - Finish
猜你喜欢

Day18 -- stack queue

Thinkphp内核开发盲盒商城源码v2.0 对接易支付/阿里云短信/七牛云存储

RT_Thread自问自答

How does Axure set the content of the text box to the current date when the page is loaded

MySQL C language connection

从开源爱好者到 Apache 董事,一共分几步?

全局、独享、局部路由守卫
![PTA: 浪漫倒影 [二叉树重建] [深度优先遍历]](/img/ae/a6681df6c3992c7fd588334623901c.png)
PTA: 浪漫倒影 [二叉树重建] [深度优先遍历]

002_Redis_String类型常见的操作命令

009_ Redis_ Getting started with redistemplate
随机推荐
WordPress calls the specified page content. 2 get_ children()
ThinkPHP kernel development blind box mall source code v2 0 docking easy payment / Alibaba cloud SMS / qiniu cloud storage
[assembly language] understand "stack" from the lowest point of view
Easyswool environment configuration
wordpress 调用指定页面内容详解2 get_children()
关于局域网浅谈
007_Redis_Jedis连接池
How to call out services in idea and display the startup class in services
Develop a chrome plug-in from 0 (2)
Redis memory recycling strategy
Dynamic batch processing and static batch processing of unity
How many steps are there from open source enthusiasts to Apache directors?
IAR嵌入式开发STM32f103c8t6之点亮LED灯
012_ Access denied for user ‘root‘@‘localhost‘ (using password: YES)
假如404页面是这样的 | 每日趣闻
Applet reads files
中金财富跟中金公司是一家公司吗,安全吗
002_Redis_String类型常见的操作命令
PTA: praise the crazy devil
JDBC cannot connect to MySQL, and the error is access denied for user 'root' @ '* * *' (using password: Yes)