Golang Web Programming: A simple session (using the sessions package)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. The website cannot determine the visitor's identity according to the visiting IP, and the IP plus port number is not acceptable. Generally, the user ID password to determine the user identity, but users can not access the page every time you manually enter a user name and password Ah, this is too tired.
One of the simplest ideas is to let the browser help us enter, but the HTTP protocol itself does not carry the user name password function, and sometimes our needs are not only the user name and password so simple, for example, we also want to pass the browser and Web server between the user ID, the last time to visit the site, etc. Each site's requirements are different, so-called tune.
Now, the browser allows the Web server to save a small piece of data in the browser, what data is OK, your own format to parse it. Specifically, when the browser accesses the server for the first time, the server responds with the data that needs the browser's request, the browser receives the server's response, and saves the data. When the browser accesses the server again, the browser contains the data in the request. Since this data is not our main business, it is just a little episode in our main business, so we call this data a cookie, a doughnut, a snack, to distinguish it from dinner.
Here we represent the process in a piece of code, to simplify, we only ask to save the user name, the whole process is:
1. The browser accesses the Web page, the server returns the request and asks the browser to save a user name. (This user name is usually an earlier stage requiring users to fill out a form to the server.)
2. The browser saves this user name.
3. The browser accesses this page again and takes this user name.
4. When the browser knows that this user is visiting, it prints a word of hello.

The code accesses two browsers during testing, does not appear on the first browser, and the second time shows hello.
The code is as follows:
?
www.usr.cc
0102030405060708091011121314151617181920212223 1package main 2 import ( 3"github.com/gorilla/sessions" 4"net/http" 5"io" 6 )  7 8 var store=sessions.NewCookieStore([]byte("something-very-secret")) 910 func pageHandler(w http.ResponseWriter,r *http.Request) {11session,_:=store.Get(r,"get_name_session")12name,ok:=session.Values["name"].(string)13session.Values["name"]="Dean"14session.Save(r,w)15 if ok {16io.WriteString(w,"hello, "+string(name));17}18 }192021 func main(){22http.HandleFunc("/",pageHandler)23 }

Extended Reading

    1. How do I try to generate a 500KHZ clock signal for 0809 using the 51 P2.4 port programmatically?
    2. c++ Programming ideas (PDF ebook)
    3. autodesk A programming interview question, everybody try it.
    4. DSP mixed programming
    5. Open publishing: Xu Xiwei "Go Language Programming" Sample "Object-oriented Programming"
    6. google Go language update Android platform work acceleration
    7. 6410 Interrupt control
    8. LLVM and Clang introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.