mirror of
https://github.com/golang/go
synced 2024-11-18 19:34:41 -07:00
1b09d43067
The old ioutil references are still valid, but update our code to reflect best practices and get used to the new locations. Code compiled with the bootstrap toolchain (cmd/asm, cmd/dist, cmd/compile, debug/elf) must remain Go 1.4-compatible and is excluded. Also excluded vendored code. For #41190. Change-Id: I6d86f2bf7bc37a9d904b6cee3fe0c7af6d94d5b1 Reviewed-on: https://go-review.googlesource.com/c/go/+/263142 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
124 lines
2.9 KiB
Go
124 lines
2.9 KiB
Go
// Copyright 2015 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package mime_test
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"mime"
|
|
)
|
|
|
|
func ExampleWordEncoder_Encode() {
|
|
fmt.Println(mime.QEncoding.Encode("utf-8", "¡Hola, señor!"))
|
|
fmt.Println(mime.QEncoding.Encode("utf-8", "Hello!"))
|
|
fmt.Println(mime.BEncoding.Encode("UTF-8", "¡Hola, señor!"))
|
|
fmt.Println(mime.QEncoding.Encode("ISO-8859-1", "Caf\xE9"))
|
|
// Output:
|
|
// =?utf-8?q?=C2=A1Hola,_se=C3=B1or!?=
|
|
// Hello!
|
|
// =?UTF-8?b?wqFIb2xhLCBzZcOxb3Ih?=
|
|
// =?ISO-8859-1?q?Caf=E9?=
|
|
}
|
|
|
|
func ExampleWordDecoder_Decode() {
|
|
dec := new(mime.WordDecoder)
|
|
header, err := dec.Decode("=?utf-8?q?=C2=A1Hola,_se=C3=B1or!?=")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Println(header)
|
|
|
|
dec.CharsetReader = func(charset string, input io.Reader) (io.Reader, error) {
|
|
switch charset {
|
|
case "x-case":
|
|
// Fake character set for example.
|
|
// Real use would integrate with packages such
|
|
// as code.google.com/p/go-charset
|
|
content, err := io.ReadAll(input)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return bytes.NewReader(bytes.ToUpper(content)), nil
|
|
default:
|
|
return nil, fmt.Errorf("unhandled charset %q", charset)
|
|
}
|
|
}
|
|
header, err = dec.Decode("=?x-case?q?hello!?=")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Println(header)
|
|
// Output:
|
|
// ¡Hola, señor!
|
|
// HELLO!
|
|
}
|
|
|
|
func ExampleWordDecoder_DecodeHeader() {
|
|
dec := new(mime.WordDecoder)
|
|
header, err := dec.DecodeHeader("=?utf-8?q?=C3=89ric?= <eric@example.org>, =?utf-8?q?Ana=C3=AFs?= <anais@example.org>")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Println(header)
|
|
|
|
header, err = dec.DecodeHeader("=?utf-8?q?=C2=A1Hola,?= =?utf-8?q?_se=C3=B1or!?=")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Println(header)
|
|
|
|
dec.CharsetReader = func(charset string, input io.Reader) (io.Reader, error) {
|
|
switch charset {
|
|
case "x-case":
|
|
// Fake character set for example.
|
|
// Real use would integrate with packages such
|
|
// as code.google.com/p/go-charset
|
|
content, err := io.ReadAll(input)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return bytes.NewReader(bytes.ToUpper(content)), nil
|
|
default:
|
|
return nil, fmt.Errorf("unhandled charset %q", charset)
|
|
}
|
|
}
|
|
header, err = dec.DecodeHeader("=?x-case?q?hello_?= =?x-case?q?world!?=")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Println(header)
|
|
// Output:
|
|
// Éric <eric@example.org>, Anaïs <anais@example.org>
|
|
// ¡Hola, señor!
|
|
// HELLO WORLD!
|
|
}
|
|
|
|
func ExampleFormatMediaType() {
|
|
mediatype := "text/html"
|
|
params := map[string]string{
|
|
"charset": "utf-8",
|
|
}
|
|
|
|
result := mime.FormatMediaType(mediatype, params)
|
|
|
|
fmt.Println("result:", result)
|
|
// Output:
|
|
// result: text/html; charset=utf-8
|
|
}
|
|
|
|
func ExampleParseMediaType() {
|
|
mediatype, params, err := mime.ParseMediaType("text/html; charset=utf-8")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Println("type:", mediatype)
|
|
fmt.Println("charset:", params["charset"])
|
|
// Output:
|
|
// type: text/html
|
|
// charset: utf-8
|
|
}
|