這篇文章主要介紹“Golang net/http中Cookie的使用方法”,在日常操作中,相信很多人在Golang net/http中Cookie的使用方法問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Golang net/http中Cookie的使用方法”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
威海網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),威海網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為威海1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的威海做網(wǎng)站的公司定做!
net/http Cookie的使用方法:
package main import ( "strings" "fmt" "io" "net/http" ) //cookie設(shè)置方法一 func Cookie(w http.ResponseWriter,r *http.Request){ ck:=&http.Cookie{ Name:"myCookie", Value:"hello", Path:"/", Domain:"localhost", MaxAge:120, } http.SetCookie(w,ck) ck2,err:=r.Cookie("myCookie") if err!=nil{ io.WriteString(w,err.Error()) return } io.WriteString(w,ck2.Value) } //cookie設(shè)置方法二 func Cookie2(w http.ResponseWriter,r *http.Request){ ck:=&http.Cookie{ Name:"myCookie2", Value:"hello world", Path:"/", Domain:"localhost", MaxAge:120, } w.Header().Set("Set-Cookie",strings.Replace(ck.String()," ","%20",-1)) //http包中將空格視為非法,所以需要在此處添加空格 ck2,err:=r.Cookie("myCookie2") if err!=nil{ io.WriteString(w,err.Error()) return } io.WriteString(w,ck2.Value) } func main(){ http.HandleFunc("/",Cookie) http.HandleFunc("/2",Cookie2) fmt.Println("listen......") http.ListenAndServe(":8081",nil) }
到此,關(guān)于“Golang net/http中Cookie的使用方法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
當(dāng)前名稱:Golangnet/http中Cookie的使用方法
本文URL:http://chinadenli.net/article22/geoscc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開(kāi)發(fā)、品牌網(wǎng)站制作、網(wǎng)站建設(shè)、面包屑導(dǎo)航、響應(yīng)式網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)