mime
packagestandard libraryThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
Documentation¶
Overview¶
Package mime implements parts of the MIME spec.
Index¶
- Constants
- Variables
- func AddExtensionType(ext, typ string) error
- func ExtensionsByType(typ string) ([]string, error)
- func FormatMediaType(t string, param map[string]string) string
- func ParseMediaType(v string) (mediatype string, params map[string]string, err error)
- func TypeByExtension(ext string) string
- type WordDecoder
- type WordEncoder
Examples¶
Constants¶
const (// BEncoding represents Base64 encoding scheme as defined byRFC 2045.BEncoding =WordEncoder('b')// QEncoding represents the Q-encoding scheme as defined byRFC 2047.QEncoding =WordEncoder('q'))
Variables¶
var ErrInvalidMediaParameter =errors.New("mime: invalid media parameter")ErrInvalidMediaParameter is returned byParseMediaType ifthe media type value was found but there was an error parsingthe optional parameters
Functions¶
funcAddExtensionType¶
AddExtensionType sets the MIME type associated withthe extension ext to typ. The extension should begin witha leading dot, as in ".html".
funcExtensionsByType¶added ingo1.5
ExtensionsByType returns the extensions known to be associated with the MIMEtype typ. The returned extensions will each begin with a leading dot, as in".html". When typ has no associated extensions, ExtensionsByType returns annil slice.
funcFormatMediaType¶
FormatMediaType serializes mediatype t and the parametersparam as a media type conforming toRFC 2045 andRFC 2616.The type and parameter names are written in lower-case.When any of the arguments result in a standard violation thenFormatMediaType returns the empty string.
Example¶
package mainimport ("fmt""mime")func main() {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
funcParseMediaType¶
ParseMediaType parses a media type value and any optionalparameters, perRFC 1521. Media types are the values inContent-Type and Content-Disposition headers (RFC 2183).On success, ParseMediaType returns the media type convertedto 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 errorErrInvalidMediaParameter.The returned map, params, maps from the lowercaseattribute to the attribute value with its case preserved.
Example¶
package mainimport ("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"])}Output:type: text/htmlcharset: utf-8
funcTypeByExtension¶
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 localsystem's MIME-info database or mime.types file(s) if available under one ormore 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.
Types¶
typeWordDecoder¶added ingo1.5
type WordDecoder struct {// 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.CharsetReader func(charsetstring, inputio.Reader) (io.Reader,error)}A WordDecoder decodes MIME headers containingRFC 2047 encoded-words.
func (*WordDecoder)Decode¶added ingo1.5
func (d *WordDecoder) Decode(wordstring) (string,error)
Decode decodes anRFC 2047 encoded-word.
Example¶
package mainimport ("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-charsetcontent, err := io.ReadAll(input)if err != nil {return nil, err}return bytes.NewReader(bytes.ToUpper(content)), nildefault: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 (*WordDecoder)DecodeHeader¶added ingo1.5
func (d *WordDecoder) DecodeHeader(headerstring) (string,error)
DecodeHeader decodes all encoded-words of the given string. It returns anerror if and only if [WordDecoder.CharsetReader] of d returns an error.
Example¶
package mainimport ("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-charsetcontent, err := io.ReadAll(input)if err != nil {return nil, err}return bytes.NewReader(bytes.ToUpper(content)), nildefault: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!
typeWordEncoder¶added ingo1.5
type WordEncoderbyte
A WordEncoder is anRFC 2047 encoded-word encoder.
func (WordEncoder)Encode¶added ingo1.5
func (eWordEncoder) Encode(charset, sstring)string
Encode returns the encoded-word form of s. If s is ASCII without specialcharacters, it is returned unchanged. The provided charset is the IANAcharset name of s. It is case insensitive.
Example¶
package mainimport ("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"))}Output:=?utf-8?q?=C2=A1Hola,_se=C3=B1or!?=Hello!=?UTF-8?b?wqFIb2xhLCBzZcOxb3Ih?==?ISO-8859-1?q?Caf=E9?=
Directories¶
| Path | Synopsis |
|---|---|
Package multipart implements MIME multipart parsing, as defined in RFC 2046. | Package multipart implements MIME multipart parsing, as defined in RFC 2046. |
Package quotedprintable implements quoted-printable encoding as specified by RFC 2045. | Package quotedprintable implements quoted-printable encoding as specified by RFC 2045. |