社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
关于 net/http
包的几点总结:
net/http
本身基于goroutine实现, 通过新建协程处理新的连接任务;time_wait
与close_wait
:收到对方的fin请求, 内核会将连接置为close_wait
状态;主动发起关闭连接请求的一方,会将连接置为time_wait
状态;以下为net/http
的极简demo:
package main
import (
"fmt"
"log"
"net/http"
)
func HelloHandler(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "Hello, world!n")
}
func main() {
http.HandleFunc("/hello", HelloHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
其中开启服务监听的流程如下图所示:
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!