package main
import (
"net/http"
)
func main() {
http.HandleFunc("/", func (w http.ResponseWriter, r *http.Request){
w.Header().Set("name", "my name is smallsoup")
w.WriteHeader(500)
w.Write([]byte("hello world\n"))
})
http.ListenAndServe(":8080", nil)
}
客户端:
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
myHttpGet()
}
func myHttpGet() {
rsp, err := http.Get("http://localhost:8080")
if err != nil {
fmt.Println("myHttpGet error is ", err)
return
}
defer rsp.Body.Close()
body, err := ioutil.ReadAll(rsp.Body)
if err != nil {
fmt.Println("myHttpGet error is ", err)
return
}
fmt.Println("response statuscode is ", rsp.StatusCode,
"\nhead[name]=", rsp.Header["Name"],
"\nbody is ", string(body))
}