example_test.go 2.9 KB

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