HTTP Server
HTTP Server
原文:https://gobyexample.com/http-server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| // Note:
// This code is from https://gobyexample.com.
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)
}
|
1
2
3
4
5
6
| PS D:\Dev\Go\byExample> curl localhost:8090/hello
hello
PS D:\Dev\Go\byExample> curl localhost:8090/headers
User-Agent: curl/8.0.1
Accept: */*
PS D:\Dev\Go\byExample>
|