golang中的http服务器
生活随笔
收集整理的這篇文章主要介紹了
golang中的http服务器
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
簡(jiǎn)介
http WEB服務(wù)器:
1. 注冊(cè)回調(diào)函數(shù):http.HandleFunc("/", myHandler)func myHandler(w http.ResponseWriter, r *http.Request)w:給客戶端回發(fā)數(shù)據(jù)r:從客戶端讀到的數(shù)據(jù)type ResponseWriter interface {Header() Header Write([]byte) (int, error) WriteHeader(int) }type Request struct {Method string // 瀏覽器請(qǐng)求方法 GET、POST?URL *url.URL // 瀏覽器請(qǐng)求的訪問(wèn)路徑??Header Header // 請(qǐng)求頭部Body io.ReadCloser // 請(qǐng)求包體RemoteAddr string // 瀏覽器地址??ctx context.Context}2. 綁定服務(wù)器地址結(jié)構(gòu):http.ListenAndServe("127.0.0.1:8000", nil)參2:通常傳 ni 。 表示 讓服務(wù)端 調(diào)用默認(rèn)的 http.DefaultServeMux 進(jìn)行處理使用
func myHandler(w http.ResponseWriter, r *http.Request) {fmt.Println("r.Method = ", r.Method)fmt.Println("r.URL = ", r.URL)fmt.Println("r.Header = ", r.Header)fmt.Println("r.Body = ", r.Body)//回復(fù)io.WriteString(w, "hello world\n") }func main() {//第一個(gè)參數(shù)是url的http.HandleFunc("/", myHandler)//用于指定的tcp網(wǎng)絡(luò)地址監(jiān)聽(tīng)//第一個(gè)參數(shù)是監(jiān)聽(tīng)地址,第二個(gè)參數(shù)是服務(wù)端處理程序,通常為空,為空表示服務(wù)端調(diào)用http.DefaultServeMux處理err := http.ListenAndServe("127.0.0.1:8183", nil)if err != nil {fmt.Println("有錯(cuò)誤: ", err)} }默認(rèn) Handler
- NotFountHandler
- RedirectHandler
- FileServer
get
func myHandler(w http.ResponseWriter, r *http.Request) {//解析參數(shù),默認(rèn)是不會(huì)解析的r.ParseForm()fmt.Fprintf(w, "%v\n", r.Form)fmt.Fprintf(w, "path:%s\n", r.URL.Path)fmt.Fprintf(w, "schema:%s\n", r.URL.Scheme)//get查詢字符串fmt.Fprintf(w, "form:%s\n", r.Form)//控制臺(tái)打印for k, v := range r.Form {fmt.Println("key: ", k)fmt.Println("value: ", strings.Join(v, ""))}fmt.Fprintf(w, "hello world\n") }func main() {//第一個(gè)參數(shù)是url的http.HandleFunc("/", myHandler)//用于指定的tcp網(wǎng)絡(luò)地址監(jiān)聽(tīng)//第一個(gè)參數(shù)是監(jiān)聽(tīng)地址,第二個(gè)參數(shù)是服務(wù)端處理程序,通常為空,為空表示服務(wù)端調(diào)用http.DefaultServeMux處理err := http.ListenAndServe("127.0.0.1:8183", nil)if err != nil {fmt.Println("有錯(cuò)誤: ", err)} }post
func myHandler(w http.ResponseWriter, r *http.Request) {method := r.Methodif method == "GET" {t, err := template.ParseFiles("./index.html")if err != nil {fmt.Fprintf(w, "載入頁(yè)面錯(cuò)誤")return}t.Execute(w, nil)} else if method == "POST" {r.ParseForm()fmt.Printf("username: %s\n", r.FormValue("username"))fmt.Printf("password: %s\n", r.FormValue("password"))fmt.Fprintf(w, "username: %s, password: %s\n", r.FormValue("username"), r.FormValue("password"))} }func main() {//第一個(gè)參數(shù)是url的http.HandleFunc("/", myHandler)//用于指定的tcp網(wǎng)絡(luò)地址監(jiān)聽(tīng)//第一個(gè)參數(shù)是監(jiān)聽(tīng)地址,第二個(gè)參數(shù)是服務(wù)端處理程序,通常為空,為空表示服務(wù)端調(diào)用http.DefaultServeMux處理err := http.ListenAndServe("127.0.0.1:8183", nil)if err != nil {fmt.Println("有錯(cuò)誤: ", err)} }模板
"html/template" func myHandler(w http.ResponseWriter, r *http.Request) {t, err := template.ParseFiles("./index.html")if err != nil {fmt.Fprintf(w, "載入頁(yè)面錯(cuò)誤")return}//第二個(gè)參數(shù)就是模板那的t.Execute(w, "mary") }func main() {//第一個(gè)參數(shù)是url的http.HandleFunc("/", myHandler)//用于指定的tcp網(wǎng)絡(luò)地址監(jiān)聽(tīng)//第一個(gè)參數(shù)是監(jiān)聽(tīng)地址,第二個(gè)參數(shù)是服務(wù)端處理程序,通常為空,為空表示服務(wù)端調(diào)用http.DefaultServeMux處理err := http.ListenAndServe("127.0.0.1:8183", nil)if err != nil {fmt.Println("有錯(cuò)誤: ", err)} }頁(yè)面上
<h1>{{.}}</h1>模板中的結(jié)構(gòu)體
type UserInfo struct {Name stringSex stringAge int }func myHandler(w http.ResponseWriter, r *http.Request) {t, err := template.ParseFiles("./index.html")if err != nil {fmt.Fprintf(w, "載入頁(yè)面錯(cuò)誤")return}user := UserInfo{Name: "Mary",Sex: "男",Age: 18,}//第二個(gè)參數(shù)就是模板那的t.Execute(w, user) }html
<h1>{{.}}</h1> <h1>{{.Name}}</h1> <h1>{{.Sex}}</h1> <h1>{{.Age}}</h1>模板中的map
t, err := template.ParseFiles("./index.html") if err != nil {fmt.Fprintf(w, "載入頁(yè)面錯(cuò)誤")return } m := make(map[string]interface{}) m["name"] = "mary" m["sex"] = "男" m["age"] = 18 //第二個(gè)參數(shù)就是模板那的 t.Execute(w, m)html
<h1>{{.}}</h1> <h1>{{.name}}</h1> <h1>{{.sex}}</h1> <h1>{{.age}}</h1>判斷
{{if gt .age 18}}<p>hello, old man, {{.name}}</p> {{else}}<p>hello, young man, {{.name}}</p> {{end}}總結(jié)
以上是生活随笔為你收集整理的golang中的http服务器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: golang中的并发服务器
- 下一篇: golang中的http客户端