Code Examples
package main
import (
"fmt"
"mime"
)
func main() {
mediatype := "text/html"
params := map[string]string{
"charset": "utf-8",
}
result := mime.FormatMediaType(mediatype, params)
fmt.Println("result:", result)
}
package main
import (
"fmt"
"mime"
)
func main() {
mediatype, params, err := mime.ParseMediaType("text/html; charset=utf-8")
if err != nil {
panic(err)
}
fmt.Println("type:", mediatype)
fmt.Println("charset:", params["charset"])
}
package main
import (
"bytes"
"fmt"
"io"
"mime"
)
func main() {
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)
}
package main
import (
"bytes"
"fmt"
"io"
"mime"
)
func main() {
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)
}
package main
import (
"fmt"
"mime"
)
func main() {
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"))
}
Package-Level Type Names (total 2, both are exported)
/* sort exporteds by: | */
A WordDecoder decodes MIME headers containing RFC 2047 encoded-words. CharsetReader, if non-nil, defines a function to generate
charset-conversion readers, converting from the provided
charset into UTF-8.
Charsets are always lower-case. utf-8, iso-8859-1 and us-ascii charsets
are handled by default.
One of the CharsetReader's result values must be non-nil. Decode decodes an RFC 2047 encoded-word. DecodeHeader decodes all encoded-words of the given string. It returns an
error if and only if CharsetReader of d returns an error.(*WordDecoder) convert(buf *strings.Builder, charset string, content []byte) error
A WordEncoder is an RFC 2047 encoded-word encoder. Encode returns the encoded-word form of s. If s is ASCII without special
characters, it is returned unchanged. The provided charset is the IANA
charset name of s. It is case insensitive. bEncode encodes s using base64 encoding and writes it to buf. encodeWord encodes a string into an encoded-word. openWord writes the beginning of an encoded-word into buf. qEncode encodes s using Q encoding and writes it to buf. It splits the
encoded-words when necessary. splitWord closes the current encoded-word and opens a new one.
const BEncoding
const QEncoding
Package-Level Functions (total 35, in which 5 are exported)
AddExtensionType sets the MIME type associated with
the extension ext to typ. The extension should begin with
a leading dot, as in ".html".
ExtensionsByType returns the extensions known to be associated with the MIME
type typ. The returned extensions will each begin with a leading dot, as in
".html". When typ has no associated extensions, ExtensionsByType returns an
nil slice.
FormatMediaType serializes mediatype t and the parameters
param as a media type conforming to RFC 2045 and RFC 2616.
The type and parameter names are written in lower-case.
When any of the arguments result in a standard violation then
FormatMediaType returns the empty string.
ParseMediaType parses a media type value and any optional
parameters, per RFC 1521. Media types are the values in
Content-Type and Content-Disposition headers (RFC 2183).
On success, ParseMediaType returns the media type converted
to lowercase and trimmed of white space and a non-nil map.
If there is an error parsing the optional parameter,
the media type will be returned along with the error
ErrInvalidMediaParameter.
The returned map, params, maps from the lowercase
attribute to the attribute value with its case preserved.
TypeByExtension returns the MIME type associated with the file extension ext.
The extension ext should begin with a leading dot, as in ".html".
When ext has no associated type, TypeByExtension returns "".
Extensions are looked up first case-sensitively, then case-insensitively.
The built-in table is small but on unix it is augmented by the local
system's MIME-info database or mime.types file(s) if available under one or
more of these names:
/usr/local/share/mime/globs2
/usr/share/mime/globs2
/etc/mime.types
/etc/apache2/mime.types
/etc/apache/mime.types
On Windows, MIME types are extracted from the registry.
Text types have the charset parameter set to "utf-8" by default.
consumeToken consumes a token from the beginning of provided
string, per RFC 2045 section 5.1 (referenced from 2183), and return
the token consumed and the rest of the string. Returns ("", v) on
failure to consume at least one character.
consumeValue consumes a "value" per RFC 2045, where a value is
either a 'token' or a 'quoted-string'. On success, consumeValue
returns the value consumed (and de-quoted/escaped, if a
quoted-string) and the rest of the string. On failure, returns
("", v).
See https://specifications.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-0.21.html
for the FreeDesktop Shared MIME-info Database specification.
The pages are generated with Goldsv0.6.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.