1
2
3
4
5
6
7 package base64_test
8
9 import (
10 "encoding/base64"
11 "fmt"
12 "os"
13 )
14
15 func Example() {
16 msg := "Hello, 世界"
17 encoded := base64.StdEncoding.EncodeToString([]byte(msg))
18 fmt.Println(encoded)
19 decoded, err := base64.StdEncoding.DecodeString(encoded)
20 if err != nil {
21 fmt.Println("decode error:", err)
22 return
23 }
24 fmt.Println(string(decoded))
25
26
27
28 }
29
30 func ExampleEncoding_EncodeToString() {
31 data := []byte("any + old & data")
32 str := base64.StdEncoding.EncodeToString(data)
33 fmt.Println(str)
34
35
36 }
37
38 func ExampleEncoding_Encode() {
39 data := []byte("Hello, world!")
40 dst := make([]byte, base64.StdEncoding.EncodedLen(len(data)))
41 base64.StdEncoding.Encode(dst, data)
42 fmt.Println(string(dst))
43
44
45 }
46
47 func ExampleEncoding_DecodeString() {
48 str := "c29tZSBkYXRhIHdpdGggACBhbmQg77u/"
49 data, err := base64.StdEncoding.DecodeString(str)
50 if err != nil {
51 fmt.Println("error:", err)
52 return
53 }
54 fmt.Printf("%q\n", data)
55
56
57 }
58
59 func ExampleEncoding_Decode() {
60 str := "SGVsbG8sIHdvcmxkIQ=="
61 dst := make([]byte, base64.StdEncoding.DecodedLen(len(str)))
62 n, err := base64.StdEncoding.Decode(dst, []byte(str))
63 if err != nil {
64 fmt.Println("decode error:", err)
65 return
66 }
67 dst = dst[:n]
68 fmt.Printf("%q\n", dst)
69
70
71 }
72
73 func ExampleNewEncoder() {
74 input := []byte("foo\x00bar")
75 encoder := base64.NewEncoder(base64.StdEncoding, os.Stdout)
76 encoder.Write(input)
77
78
79
80 encoder.Close()
81
82
83 }
84
View as plain text