1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
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"))
20 // =?utf-8?q?=C2=A1Hola,_se=C3=B1or!?=
22 // =?UTF-8?b?wqFIb2xhLCBzZcOxb3Ih?=
23 // =?ISO-8859-1?q?Caf=E9?=
26 func ExampleWordDecoder_Decode() {
27 dec
:= new(mime
.WordDecoder
)
28 header
, err
:= dec
.Decode("=?utf-8?q?=C2=A1Hola,_se=C3=B1or!?=")
34 dec
.CharsetReader
= func(charset
string, input io
.Reader
) (io
.Reader
, error
) {
37 // Fake character set for example.
38 // Real use would integrate with packages such
39 // as code.google.com/p/go-charset
40 content
, err
:= io
.ReadAll(input
)
44 return bytes
.NewReader(bytes
.ToUpper(content
)), nil
46 return nil, fmt
.Errorf("unhandled charset %q", charset
)
49 header
, err
= dec
.Decode("=?x-case?q?hello!?=")
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>")
67 header
, err
= dec
.DecodeHeader("=?utf-8?q?=C2=A1Hola,?= =?utf-8?q?_se=C3=B1or!?=")
73 dec
.CharsetReader
= func(charset
string, input io
.Reader
) (io
.Reader
, error
) {
76 // Fake character set for example.
77 // Real use would integrate with packages such
78 // as code.google.com/p/go-charset
79 content
, err
:= io
.ReadAll(input
)
83 return bytes
.NewReader(bytes
.ToUpper(content
)), nil
85 return nil, fmt
.Errorf("unhandled charset %q", charset
)
88 header
, err
= dec
.DecodeHeader("=?x-case?q?hello_?= =?x-case?q?world!?=")
94 // Éric <eric@example.org>, Anaïs <anais@example.org>
99 func ExampleFormatMediaType() {
100 mediatype
:= "text/html"
101 params
:= map[string]string{
105 result
:= mime
.FormatMediaType(mediatype
, params
)
107 fmt
.Println("result:", result
)
109 // result: text/html; charset=utf-8
112 func ExampleParseMediaType() {
113 mediatype
, params
, err
:= mime
.ParseMediaType("text/html; charset=utf-8")
118 fmt
.Println("type:", mediatype
)
119 fmt
.Println("charset:", params
["charset"])