首页 > Web开发 > 详细

Go by Example: HTTP Servers

时间:2021-08-20 16:03:32      阅读:48      评论:0      收藏:0      [点我收藏+]

原文链接:https://gobyexample.com/http-servers

package main

import (
    "fmt"
    "net/http"
)

func hello(w http.ResponseWriter, req *http.Request) {
    fmt.Fprintf(w, "hello\n")
}

func headers(w http.ResponseWriter, req *http.Request) {
    for name, headers := range req.Header {
        for _, h := range headers {
            fmt.Fprintf(w, "%v: %v\n", name, h)
        }
    }
}

func main() {
    http.HandleFunc("/hello", hello)
    http.HandleFunc("/headers", headers)

    http.ListenAndServe(":8090", nil)
}

测试:

$ go run http-servers.go &

$ curl localhost:8090/hello
hello

 

Go by Example: HTTP Servers

原文:https://www.cnblogs.com/wangjq19920210/p/15166522.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!