原文链接: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

 

相关文章:

  • 2021-07-11
  • 2021-09-14
  • 2022-01-27
  • 2022-03-03
  • 2022-12-23
  • 2021-07-22
  • 2021-06-15
猜你喜欢
  • 2022-12-23
  • 2021-07-06
  • 2021-10-12
  • 2021-07-12
  • 2021-09-29
  • 2021-11-24
  • 2021-04-23
相关资源
相似解决方案