Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Cookie in Golang net/http

2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "the use of Cookie in Golang net/http". In the daily operation, I believe that many people have doubts about the use of Cookie in Golang net/http. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "the use of Cookie in Golang net/http". Next, please follow the editor to study!

How to use net/http Cookie:

Package mainimport ("strings"fmt"io"net/http") / / cookie setting method-func Cookie (w http.ResponseWriter,r * http.Request) {ck:=&http.Cookie {Name: "myCookie", Value: "hello", Path: "/", Domain: "localhost" MaxAge:120,} http.SetCookie (wMagnck) ck2,err:=r.Cookie ("myCookie") if erratic error nil {io.WriteString (wmae err.Error () return} io.WriteString (wjinck2.value)} / / cookie setting method II 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)) / / spaces are considered illegal in the http package So you need to add the space ck2,err:=r.Cookie ("myCookie2") if erratic error nil {io.WriteString (wre err.Error ()) return} io.WriteString (wrecock2.value)} func main () {http.HandleFunc ("/", Cookie) http.HandleFunc ("/ 2") Cookie2) fmt.Println ("listen.") Http.ListenAndServe (": 8081", nil)} at this point, the study on "how to use Cookie in Golang net/http" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 204

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report