Skip to content
This repository has been archived by the owner on Oct 29, 2021. It is now read-only.

Rican7/mediatype

Repository files navigation

mediatype

GoDoc Build Status

An [Internet] Media Type and MIME string parser and modeler for Go

This library expands on the builtin "mime" package to provide a model for an "Internet Media Type" normally specified by Content-Type HTTP and MIME header fields, and as specified in RFC 2045.

Install

go get github.com/Rican7/mediatype

Example

contentTypeString := "application/vnd.google-earth.kml+xml; charset=utf-8"

mediaType, _ := mediatype.Parse(contentTypeString)

mediaType.MainType()   // "application"
mediaType.SubType()    // "kml"
mediaType.Trees()      // ["vnd", "google-earth"]
mediaType.Prefix()     // "vnd"
mediaType.Suffix()     // "xml"
mediaType.Parameters() // ["charset": "utf-8"]

mediaType.FullType()   // "application/vnd.google-earth.kml+xml"
mediaType.String()     // "application/vnd.google-earth.kml+xml; charset=utf-8"

Mutability and Immutability

mutable := &mediatype.Mutable{
    Main: "application",
    Sub:  "json",
}

mutable.String()     // "application/json"

mutable.Sub    = "xhtml"
mutable.Suffix = "xml"
mutable.String()     // "application/xhtml+xml"


immutable := mutable.Immutable()
mutable.Main = "image"
immutable.String()     // "application/xhtml+xml"