libstdc++: Only use std::ios_base_library_init() for ELF [PR116159]
[official-gcc.git] / libgo / go / mime / example_test.go
blob8a96873e5dc6b77d34f69563e8fea4f9e40b6743
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.
5 package mime_test
7 import (
8 "bytes"
9 "fmt"
10 "io"
11 "mime"
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?=
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)
32 fmt.Println(header)
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
44 return bytes.NewReader(bytes.ToUpper(content)), nil
45 default:
46 return nil, fmt.Errorf("unhandled charset %q", charset)
49 header, err = dec.Decode("=?x-case?q?hello!?=")
50 if err != nil {
51 panic(err)
53 fmt.Println(header)
54 // Output:
55 // ¡Hola, señor!
56 // 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>")
62 if err != nil {
63 panic(err)
65 fmt.Println(header)
67 header, err = dec.DecodeHeader("=?utf-8?q?=C2=A1Hola,?= =?utf-8?q?_se=C3=B1or!?=")
68 if err != nil {
69 panic(err)
71 fmt.Println(header)
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
83 return bytes.NewReader(bytes.ToUpper(content)), nil
84 default:
85 return nil, fmt.Errorf("unhandled charset %q", charset)
88 header, err = dec.DecodeHeader("=?x-case?q?hello_?= =?x-case?q?world!?=")
89 if err != nil {
90 panic(err)
92 fmt.Println(header)
93 // Output:
94 // Éric <eric@example.org>, Anaïs <anais@example.org>
95 // ¡Hola, señor!
96 // HELLO WORLD!
99 func ExampleFormatMediaType() {
100 mediatype := "text/html"
101 params := map[string]string{
102 "charset": "utf-8",
105 result := mime.FormatMediaType(mediatype, params)
107 fmt.Println("result:", result)
108 // Output:
109 // result: text/html; charset=utf-8
112 func ExampleParseMediaType() {
113 mediatype, params, err := mime.ParseMediaType("text/html; charset=utf-8")
114 if err != nil {
115 panic(err)
118 fmt.Println("type:", mediatype)
119 fmt.Println("charset:", params["charset"])
120 // Output:
121 // type: text/html
122 // charset: utf-8