-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path55_base64_encoding.go
More file actions
27 lines (22 loc) · 860 Bytes
/
55_base64_encoding.go
File metadata and controls
27 lines (22 loc) · 860 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package gobyexample
import b64 "encoding/base64"
import "fmt"
// Base64EncodingDemo - demonstrates base64 encoding/decoding
func Base64EncodingDemo() {
data := "abc123!?$*&()'-=@~"
// Go supports both standard and URL-compatible base64.
// Here's an example of encoding using the standard encoder.
// The encoder requires a []byte so we cast our string to that.
sEnc := b64.StdEncoding.EncodeToString([]byte(data))
fmt.Println(sEnc)
// Decoding may return an error, which you can check if you don't
// already know the error to be well-formed.
sDec, _ := b64.StdEncoding.DecodeString(sEnc)
fmt.Println(string(sDec))
fmt.Println()
// This encodes / decodes using a URL-compatible base64 format.
uEnc := b64.URLEncoding.EncodeToString([]byte(data))
fmt.Println(uEnc)
uDec, _ := b64.URLEncoding.DecodeString(uEnc)
fmt.Println(string(uDec))
}