Go by Example: HTTP Servers

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

上一篇:安装php报错


下一篇:Mac 系统安装多版本 PHP,低版本 PHP