// Copyright 2010 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 implements parts of the MIME spec.
package mimeimport ()var (mimeTypessync.Map// map[string]string; ".Z" => "application/x-compress"mimeTypesLowersync.Map// map[string]string; ".z" => "application/x-compress"// extensions maps from MIME type to list of lowercase file // extensions: "image/jpeg" => [".jpg", ".jpeg"]extensionsMusync.Mutex// Guards stores (but not loads) on extensions.extensionssync.Map// map[string][]string; slice values are append-only.)func ( *sync.Map) { .Range(func(, any) bool { .Delete()returntrue })}// setMimeTypes is used by initMime's non-test path, and by tests.func (, map[string]string) {clearSyncMap(&mimeTypes)clearSyncMap(&mimeTypesLower)clearSyncMap(&extensions)for , := range {mimeTypesLower.Store(, ) }for , := range {mimeTypes.Store(, ) }extensionsMu.Lock()deferextensionsMu.Unlock()for , := range { , , := ParseMediaType()if != nil {panic() }var []stringif , := extensions.Load(); { = .([]string) }extensions.Store(, append(, )) }}varbuiltinTypesLower = map[string]string{".avif": "image/avif",".css": "text/css; charset=utf-8",".gif": "image/gif",".htm": "text/html; charset=utf-8",".html": "text/html; charset=utf-8",".jpeg": "image/jpeg",".jpg": "image/jpeg",".js": "text/javascript; charset=utf-8",".json": "application/json",".mjs": "text/javascript; charset=utf-8",".pdf": "application/pdf",".png": "image/png",".svg": "image/svg+xml",".wasm": "application/wasm",".webp": "image/webp",".xml": "text/xml; charset=utf-8",}varoncesync.Once// guards initMimevartestInitMime, osInitMimefunc()func () {if := testInitMime; != nil { () } else {setMimeTypes(builtinTypesLower, builtinTypesLower)osInitMime() }}// 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.func ( string) string {once.Do(initMime)// Case-sensitive lookup.if , := mimeTypes.Load(); {return .(string) }// Case-insensitive lookup. // Optimistically assume a short ASCII extension and be // allocation-free in that case.var [10]byte := [:0]const = 0x80// from utf8 package, but not importing it.for := 0; < len(); ++ { := []if >= {// Slow path. , := mimeTypesLower.Load(strings.ToLower()) , := .(string)return }if'A' <= && <= 'Z' { = append(, +('a'-'A')) } else { = append(, ) } } , := mimeTypesLower.Load(string()) , := .(string)return}// 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.func ( string) ([]string, error) { , , := ParseMediaType()if != nil {returnnil, }once.Do(initMime) , := extensions.Load()if ! {returnnil, nil } := append([]string(nil), .([]string)...)sort.Strings()return , nil}// AddExtensionType sets the MIME type associated with// the extension ext to typ. The extension should begin with// a leading dot, as in ".html".func (, string) error {if !strings.HasPrefix(, ".") {returnfmt.Errorf("mime: extension %q missing leading dot", ) }once.Do(initMime)returnsetExtensionType(, )}func (, string) error { , , := ParseMediaType()if != nil {return }ifstrings.HasPrefix(, "text/") && ["charset"] == "" { ["charset"] = "utf-8" = FormatMediaType(, ) } := strings.ToLower()mimeTypes.Store(, )mimeTypesLower.Store(, )extensionsMu.Lock()deferextensionsMu.Unlock()var []stringif , := extensions.Load(); { = .([]string) }for , := range {if == {returnnil } }extensions.Store(, append(, ))returnnil}
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.