<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: sistoi</title>
    <description>The latest articles on DEV Community by sistoi (@sistoi).</description>
    <link>https://dev.to/sistoi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F321008%2F0d5706ad-8056-4fce-9d4d-19f086428f14.png</url>
      <title>DEV Community: sistoi</title>
      <link>https://dev.to/sistoi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sistoi"/>
    <language>en</language>
    <item>
      <title>Golang MIME type handling</title>
      <dc:creator>sistoi</dc:creator>
      <pubDate>Mon, 20 Jan 2020 22:30:57 +0000</pubDate>
      <link>https://dev.to/sistoi/golang-mime-type-handling-3fnd</link>
      <guid>https://dev.to/sistoi/golang-mime-type-handling-3fnd</guid>
      <description>&lt;p&gt;Media types, also known as MIME types, are a standard that indicates the nature and format of some data. You most likely met media types in HTTP requests, send to and received from the webserver as HTTP headers, ex: &lt;code&gt;Content-Type: application/javascript&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Golang standard library provides support for MIME types through two APIs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;the &lt;code&gt;mime&lt;/code&gt; package, which has functions for retrieving the MIME type given a file extension:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fmt.Println(mime.TypeByExtension(".txt"))
// Output: text/plain; charset=utf-8
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;the &lt;code&gt;http.DetectContentType&lt;/code&gt; function which, given a fragment of a file, returns its MIME type:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fmt.Println(http.DetectContentType([]byte("some text content of a file")))
// Output: text/plain; charset=utf-8
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even if they work as intended, these two approaches have their limitations. &lt;code&gt;mime&lt;/code&gt; can be easily tricked: anyone can change the extension of a file from .exe to .jpg. &lt;code&gt;http.DetectContentType&lt;/code&gt; on the other hand is  a more careful option, as it looks at the actual data in a file. However it suffers from a limited number of detected MIME types, with notable missing entries for office documents, audio files and some archives.&lt;/p&gt;

&lt;p&gt;Beyond the standard library is the land of user libraries where Go has three candidates for MIME types libraries:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/gabriel-vasile/mimetype"&gt;github.com/gabriel-vasile/mimetype&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/rakyll/magicmime"&gt;github.com/rakyll/magicmime&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/h2non/filetype"&gt;github.com/h2non/filetype&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Out of the three, magicmime is the most accurate, but it requires C cross compilation and libmagic-dev installed. The other two implemented the detection algorithms in pure Go, so they can just be imported. Out of them, mimetype features a bigger list of supported MIME types, but most importantly the identification is deterministic, unlike filetype which &lt;a href="https://github.com/h2non/filetype/issues/38#issuecomment-515245746"&gt;returns different results when testing the same data&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Reference experiment: &lt;a href="https://github.com/ppai-plivo/go-mime-lib-compare"&gt;https://github.com/ppai-plivo/go-mime-lib-compare&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>todayilearned</category>
    </item>
  </channel>
</rss>
