• 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

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

    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)

    底层源码如下:

    1. server := http.Server{
    2. Addr: "localhost:8080",
    3. Handler: nil,
    4. }
    5. server.ListenAndServe()

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

    handler:

    handler 是一个接口(interface)

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

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

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

    1. package main
    2. import "net/http"
    3. type myHandler struct{}
    4. func (m *myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    5. w.Write([]byte("hello world"))
    6. }
    7. func main() {
    8. mh := myHandler{}
    9. //以下五行就和http.ListenAndServe("localhost:8080", &mh)效果一样,是其源码。
    10. server := http.Server{
    11. Addr: "localhost:8080",
    12. Handler: &mh,
    13. }
    14. server.ListenAndServe()
    15. }

    http.Handle: 

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

    1. package main
    2. import "net/http"
    3. type helloHandler struct{}
    4. func (m *helloHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    5. w.Write([]byte("hello world"))
    6. }
    7. type aboutHandler struct{}
    8. func (m *aboutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    9. w.Write([]byte("about"))
    10. }
    11. func main() {
    12. mh := helloHandler{}
    13. ah := aboutHandler{}
    14. server := http.Server{
    15. Addr: "localhost:8080",
    16. Handler: nil,
    17. }
    18. http.Handle("/hello", &mh)
    19. http.Handle("/about", &ah)
    20. server.ListenAndServe()
    21. }

     

     

    http.HandleFunc

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

    1. package main
    2. import "net/http"
    3. func welcome(w http.ResponseWriter, r *http.Request) {
    4. w.Write([]byte("welcome"))
    5. }
    6. func main() {
    7. server := http.Server{
    8. Addr: "localhost:8080",
    9. Handler: nil,
    10. }
    11. http.HandleFunc("/home", func(w http.ResponseWriter, r *http.Request) {
    12. w.Write([]byte("home"))
    13. })
    14. http.HandleFunc("/welcome", welcome)
    15. //相当于http.Handle("/welcome", http.HandlerFunc(welcome))
    16. server.ListenAndServe()
    17. }

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

    两者区别:


    ​​​​​​​

  • 相关阅读:
    五年了,我在 CSDN 的两个一百万。
    armbian 安裝配置教程
    线程池如何优雅关闭
    HBase truncate的问题导致数据写入慢
    mysql配置所有人可连接_mysql配置允许外界连接
    阿里云认证是什么?报考要什么条件?
    AS巧用IDEA注释,提高协作/开发效率
    MataDoor 模块化后门:先发现后解除
    linux操作sshd
    npm install 使用了不同版本的node,jenkins打包vue项目,cnpm打包超时,node-sass安装失败
  • 原文地址:https://blog.csdn.net/zhiaidaidai/article/details/132463482