Source file
src/mime/example_test.go
1
2
3
4
5 package mime_test
6
7 import (
8 "bytes"
9 "fmt"
10 "io"
11 "mime"
12 )
13
14 func ExampleWordEncoder_Encode() {
15 fmt.Println(mime.QEncoding.Encode("utf-8", "¡Hola, señor!"))
16 fmt.Println(mime.QEncoding.Encode("utf-8", "Hello!"))
17 fmt.Println(mime.BEncoding.Encode("UTF-8", "¡Hola, señor!"))
18 fmt.Println(mime.QEncoding.Encode("ISO-8859-1", "Caf\xE9"))
19
20
21
22
23
24 }
25
26 func ExampleWordDecoder_Decode() {
27 dec := new(mime.WordDecoder)
28 header, err := dec.Decode("=?utf-8?q?=C2=A1Hola,_se=C3=B1or!?=")
29 if err != nil {
30 panic(err)
31 }
32 fmt.Println(header)
33
34 dec.CharsetReader = func(charset string, input io.Reader) (io.Reader, error) {
35 switch charset {
36 case "x-case":
37
38
39
40 content, err := io.ReadAll(input)
41 if err != nil {
42 return nil, err
43 }
44 return bytes.NewReader(bytes.ToUpper(content)), nil
45 default:
46 return nil, fmt.Errorf("unhandled charset %q", charset)
47 }
48 }
49 header, err = dec.Decode("=?x-case?q?hello!?=")
50 if err != nil {
51 panic(err)
52 }
53 fmt.Println(header)
54
55
56
57 }
58
59 func ExampleWordDecoder_DecodeHeader() {
60 dec := new(mime.WordDecoder)
61 header, err := dec.DecodeHeader("=?utf-8?q?=C3=89ric?= <eric@example.org>, =?utf-8?q?Ana=C3=AFs?= <anais@example.org>")
62 if err != nil {
63 panic(err)
64 }
65 fmt.Println(header)
66
67 header, err = dec.DecodeHeader("=?utf-8?q?=C2=A1Hola,?= =?utf-8?q?_se=C3=B1or!?=")
68 if err != nil {
69 panic(err)
70 }
71 fmt.Println(header)
72
73 dec.CharsetReader = func(charset string, input io.Reader) (io.Reader, error) {
74 switch charset {
75 case "x-case":
76
77
78
79 content, err := io.ReadAll(input)
80 if err != nil {
81 return nil, err
82 }
83 return bytes.NewReader(bytes.ToUpper(content)), nil
84 default:
85 return nil, fmt.Errorf("unhandled charset %q", charset)
86 }
87 }
88 header, err = dec.DecodeHeader("=?x-case?q?hello_?= =?x-case?q?world!?=")
89 if err != nil {
90 panic(err)
91 }
92 fmt.Println(header)
93
94
95
96
97 }
98
99 func ExampleFormatMediaType() {
100 mediatype := "text/html"
101 params := map[string]string{
102 "charset": "utf-8",
103 }
104
105 result := mime.FormatMediaType(mediatype, params)
106
107 fmt.Println("result:", result)
108
109
110 }
111
112 func ExampleParseMediaType() {
113 mediatype, params, err := mime.ParseMediaType("text/html; charset=utf-8")
114 if err != nil {
115 panic(err)
116 }
117
118 fmt.Println("type:", mediatype)
119 fmt.Println("charset:", params["charset"])
120
121
122
123 }
124
View as plain text