日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

golang中的http服务器

發(fā)布時(shí)間:2025/6/15 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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
http.ListenAndServe(":8081", http.NotFoundHandler())
  • RedirectHandler
http.ListenAndServe(":8081", http.RedirectHandler("/test", 302))
  • FileServer
http.ListenAndServe(":8081", http.FileServer(http.Dir("./")))

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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。