| 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. |
| 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 |
// Output: |
| 20 |
// =?utf-8?q?=C2=A1Hola,_se=C3=B1or!?= |
| 21 |
// Hello! |
| 22 |
// =?UTF-8?b?wqFIb2xhLCBzZcOxb3Ih?= |
| 23 |
// =?ISO-8859-1?q?Caf=E9?= |
| 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 |
// 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) |
| 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 |
// Output: |
| 55 |
// ¡Hola, señor! |
| 56 |
// HELLO! |
| 57 |
} |
| 58 |
|
| 59 |
func ExampleWordDecoder_DecodeHeader() { |
| 60 |
dec := new(mime.WordDecoder) |
| 61 |
header, err := dec.DecodeHeader("=?utf-8?q?=C3=89ric?= <[email protected]>, =?utf-8?q?Ana=C3=AFs?= <[email protected]>") |
| 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 |
// 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) |
| 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 |
// Output: |
| 94 |
// Éric <[email protected]>, Anaïs <[email protected]> |
| 95 |
// ¡Hola, señor! |
| 96 |
// HELLO WORLD! |
| 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 |
// Output: |
| 109 |
// result: text/html; charset=utf-8 |
| 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 |
// Output: |
| 121 |
// type: text/html |
| 122 |
// charset: utf-8 |
| 123 |
} |