Golang Small examples of cookies, JSON, base64

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

In today's project to deposit a map into a cookie, elephant brother studied JSON and Base64 and wrote a small example

A. Deposit a map into a cookie

Mapyour to deposit the cookie: = map[string]string{}your["IsUser"] = isuseryour["username"] = usernameyour["password"] = d. STRTOMD5 (password)//convert map to JSON  conversion is []byte, after the string (Your_byte) is the JSON your_byte, _: = json. Marshal (your)//JSON base64 a bit b64: = base64. Newencoding ("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/") your_string: = b64. Encodetostring (Your_byte) fmt. PRINTLN (your_string)//The second base64 way your_string = base64. Stdencoding.encodetostring (Your_byte) fmt. PRINTLN (your_string)//Storage cookiethis. Ctx.setcookie ("Your", your_string)



Second, read the map in the cookie

Read the variable in the cookie your_string: = this. Ctx.Input.Cookie ("your")//Parse into jsonb64: = base64. Newencoding ("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/") Your_to_byte, _: = B64. Decodestring (your_string) Your: = String (your_to_byte) fmt.println (Your)



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.