Brother even go language training tutorials take you on the Go Case (6) Go BASE64 encoding

Source: Internet
Author: User
Brother and Go language training course system design architecture includes block language basic language Go language, blockchain back-end technology system, blockchain public chain, blockchain distributed application development, etc., and the final interview guidance Actual combat with the project. The course was carefully crafted by the Tsinghua Microsoft Google Masters team and developed over the course of half a year.

GoBase64 encoding

Go provides built-in support for base64 encoding and decoding

Packagemain

/ / This import package syntax will default to the base64 from an alias b64, so

/ / We can directly use b64 to represent this package below, save the input

Importb64"encoding/base64"

Import"fmt"

Funcmain(){

//This is the string we used to demonstrate encoding and decoding.

Data:="abc123!?$*&()'-=@~"

//Go supports base64 encoding for standard and compatible URLs.

//We use standard base64 encoding here, this

/ / Function requires a [] byte parameter, so this will

/ / Convert a string to a byte array

sEnc:=b64.StdEncoding.EncodeToString([]byte(data))

fmt.Println(sEnc)

/ / Decoding a base64 encoding may return an error,

//If you don't know if the input is correct base64

// coding, you need to detect some decoding errors

sDec, _:=b64.StdEncoding.DecodeString(sEnc)

fmt.Println(string(sDec))

fmt.Println()

//Base64 encoding and decoding using compatible URLs

uEnc:=b64.URLEncoding.EncodeToString([]byte(data))

fmt.Println(uEnc)

uDec, _:=b64.URLEncoding.DecodeString(uEnc)

fmt.Println(string(uDec))

}

  operation result

YWJjMTIzIT8kKiYoKSctPUB+

Abc123!?$*&()'-=@~

YWJjMTIzIT8kKiYoKSctPUB-

Abc123!?$*&()'-=@~

Both methods encode the original data into base64 encoding, the difference being that the standard encoding is followed by +, and the encoding of the compatible URL is followed by

  Yes-.
Related Article

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.