github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/mime/example_test.go (about) 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 "github.com/shogo82148/std/bytes" 9 "github.com/shogo82148/std/fmt" 10 "github.com/shogo82148/std/io" 11 "github.com/shogo82148/std/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 // 例のためのフェイクキャラクターセット。 38 // 実際の使用では、code.google.com/p/go-charsetのようなパッケージと統合します。 39 content, err := io.ReadAll(input) 40 if err != nil { 41 return nil, err 42 } 43 return bytes.NewReader(bytes.ToUpper(content)), nil 44 default: 45 return nil, fmt.Errorf("unhandled charset %q", charset) 46 } 47 } 48 header, err = dec.Decode("=?x-case?q?hello!?=") 49 if err != nil { 50 panic(err) 51 } 52 fmt.Println(header) 53 // Output: 54 // ¡Hola, señor! 55 // HELLO! 56 } 57 58 func ExampleWordDecoder_DecodeHeader() { 59 dec := new(mime.WordDecoder) 60 header, err := dec.DecodeHeader("=?utf-8?q?=C3=89ric?= <eric@example.org>, =?utf-8?q?Ana=C3=AFs?= <anais@example.org>") 61 if err != nil { 62 panic(err) 63 } 64 fmt.Println(header) 65 66 header, err = dec.DecodeHeader("=?utf-8?q?=C2=A1Hola,?= =?utf-8?q?_se=C3=B1or!?=") 67 if err != nil { 68 panic(err) 69 } 70 fmt.Println(header) 71 72 dec.CharsetReader = func(charset string, input io.Reader) (io.Reader, error) { 73 switch charset { 74 case "x-case": 75 // 例のためのフェイクキャラクターセット。 76 // 実際の使用では、code.google.com/p/go-charsetのようなパッケージと統合します。 77 content, err := io.ReadAll(input) 78 if err != nil { 79 return nil, err 80 } 81 return bytes.NewReader(bytes.ToUpper(content)), nil 82 default: 83 return nil, fmt.Errorf("unhandled charset %q", charset) 84 } 85 } 86 header, err = dec.DecodeHeader("=?x-case?q?hello_?= =?x-case?q?world!?=") 87 if err != nil { 88 panic(err) 89 } 90 fmt.Println(header) 91 // Output: 92 // Éric <eric@example.org>, Anaïs <anais@example.org> 93 // ¡Hola, señor! 94 // HELLO WORLD! 95 } 96 97 func ExampleFormatMediaType() { 98 mediatype := "text/html" 99 params := map[string]string{ 100 "charset": "utf-8", 101 } 102 103 result := mime.FormatMediaType(mediatype, params) 104 105 fmt.Println("result:", result) 106 // Output: 107 // result: text/html; charset=utf-8 108 } 109 110 func ExampleParseMediaType() { 111 mediatype, params, err := mime.ParseMediaType("text/html; charset=utf-8") 112 if err != nil { 113 panic(err) 114 } 115 116 fmt.Println("type:", mediatype) 117 fmt.Println("charset:", params["charset"]) 118 // Output: 119 // type: text/html 120 // charset: utf-8 121 }