<?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>Forem: Areesh Zafar</title>
    <description>The latest articles on Forem by Areesh Zafar (@areesh_zafar).</description>
    <link>https://forem.com/areesh_zafar</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%2F3865437%2F0d37b17b-485c-48da-b61c-2eca6ed9b02a.png</url>
      <title>Forem: Areesh Zafar</title>
      <link>https://forem.com/areesh_zafar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/areesh_zafar"/>
    <language>en</language>
    <item>
      <title>(Golang)You've Been Calling It a Handler Wrong All Along</title>
      <dc:creator>Areesh Zafar</dc:creator>
      <pubDate>Wed, 08 Apr 2026 12:00:01 +0000</pubDate>
      <link>https://forem.com/areesh_zafar/golangyouve-been-calling-it-a-handler-wrong-all-along-5ei0</link>
      <guid>https://forem.com/areesh_zafar/golangyouve-been-calling-it-a-handler-wrong-all-along-5ei0</guid>
      <description>&lt;p&gt;When routing in go, we attach a normal function to a router and call it a handler, for example :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;home&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

     &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello from Snippetbox"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="n"&gt;mux&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewServeMux&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;home&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Most of would just call that home is a handler without knowing how its orginally not a handler and is transformer into one.&lt;/p&gt;

&lt;p&gt;To understand this, first we need to understand what is a handler?&lt;/p&gt;

&lt;p&gt;A Handler is any object that has the &lt;strong&gt;ServeHTTP()&lt;/strong&gt; method, but why? Let's see in the source code of go, inside the net/http package, in &lt;em&gt;server.go&lt;/em&gt; we will see this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Handler&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ServeHTTP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which means, to be a Handler, that thing should contain the &lt;strong&gt;ServeHTTP()&lt;/strong&gt; method with the exact signature as above.&lt;/p&gt;

&lt;p&gt;But now the question arises, so &lt;strong&gt;&lt;em&gt;why does my routes work?&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Here's the answer :&lt;/p&gt;

&lt;p&gt;it works because when we register the route like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;home&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;HandleFunc()&lt;/strong&gt; secretly wraps the home function inside &lt;strong&gt;http.HandlerFunc()&lt;/strong&gt; which in turn attaches the &lt;strong&gt;ServeHTTP()&lt;/strong&gt; method to the home function and makes it indirectly a &lt;strong&gt;"Handler"&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;HandlerFunc&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;HandlerFunc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;ServeHTTP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// just calls the home function&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So to wrap this is the total flow:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6eh5r7musfwi9re1k1rb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6eh5r7musfwi9re1k1rb.png" alt="Image Showing the total flow " width="800" height="384"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You might also get it that we can directly call the HandlerFunc() instead of &lt;strong&gt;HandleFunc()&lt;/strong&gt;, yes we can but &lt;strong&gt;HandleFunc()&lt;/strong&gt; is easer to write and does the same work internally.&lt;br&gt;&lt;br&gt;
This is how we can directly call the &lt;strong&gt;HandlerFunc()&lt;/strong&gt; :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;mux&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewServeMux&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandlerFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;home&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Furthermore, &lt;strong&gt;ServeMux&lt;/strong&gt; itself is also a Handler, it has its own ServeHTTP() method, look at the source code :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh9fe7jw1bs403pm8xqaz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh9fe7jw1bs403pm8xqaz.png" alt="Image from server.go" width="792" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3wzzylrel0uybt1bjtee.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3wzzylrel0uybt1bjtee.png" alt="Image from server.go" width="800" height="381"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice(line 2847), the &lt;strong&gt;*ServeMux&lt;/strong&gt; in the receiver part of the function,that means &lt;strong&gt;ServerHTTP()&lt;/strong&gt; belongs to &lt;strong&gt;ServeMux&lt;/strong&gt;, making it a Handler too anddd that's why we can pass it directly to &lt;strong&gt;http.ListenAndServe()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkgc38dwgp1sfs70qakst.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkgc38dwgp1sfs70qakst.png" alt="Image from server.go" width="800" height="157"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;notice, the second parameter of the &lt;strong&gt;ListenAndServe()&lt;/strong&gt; function is a handler so the mux we pass in there to run our server is also a handler&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ListenAndServe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":4000"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="c"&gt;//mux is a handler!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next time you write &lt;strong&gt;mux.HandleFunc("/",home)&lt;/strong&gt; - you'll know that &lt;strong&gt;home&lt;/strong&gt; alone isn't really a handler, but &lt;strong&gt;http.HandlerFunc(home)&lt;/strong&gt; is and that's exactly what Go creates behind the scenes:)&lt;/p&gt;

&lt;p&gt;Thanks for reading till here, see you in the next post ;)&lt;/p&gt;

</description>
      <category>go</category>
      <category>backenddevelopment</category>
      <category>webdev</category>
      <category>architecture</category>
    </item>
    <item>
      <title>How Go's Server Multiplexer Actually Works</title>
      <dc:creator>Areesh Zafar</dc:creator>
      <pubDate>Tue, 07 Apr 2026 10:08:34 +0000</pubDate>
      <link>https://forem.com/areesh_zafar/how-gos-server-multiplexer-actually-works-4le3</link>
      <guid>https://forem.com/areesh_zafar/how-gos-server-multiplexer-actually-works-4le3</guid>
      <description>&lt;h2&gt;
  
  
  What is ServeMux in Go?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;ServeMux&lt;/strong&gt; is a request router that decides which function should handle an incoming request, it matches the URL of an incoming http request to a set of registered patterns which we define like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewServeMux&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;home&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/add"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;addNum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;when a user hits &lt;strong&gt;/&lt;/strong&gt; url then the &lt;strong&gt;home&lt;/strong&gt; function is run, similarly when a user hits the "/add" url the &lt;strong&gt;addNum&lt;/strong&gt; function will be run.&lt;/p&gt;

&lt;p&gt;Simple right?&lt;/p&gt;

&lt;p&gt;But there's a catch, go's servemux supports two kinds of URL patterns:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Fixed Paths&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Subtree Paths&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Fixed Paths-&lt;/strong&gt; These URL patterns do not have a trailing slash(a slash at end) like "/add" in the above example.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Subtree Paths-&lt;/strong&gt; These URL patterns end with a trailing slash like "/add/" or simply "/" as in the above example.&lt;/p&gt;

&lt;p&gt;What happens is that fixed path patterns are only matched when the URL path &lt;strong&gt;exactly matches&lt;/strong&gt; the pattern we define like &lt;strong&gt;"/add"&lt;/strong&gt;, so if we go to the URL &lt;strong&gt;"/add/"&lt;/strong&gt; the &lt;strong&gt;addNum&lt;/strong&gt; function won't run because in the&lt;br&gt;&lt;br&gt;
&lt;strong&gt;mux.HandleFunc ("/add",addNum)&lt;/strong&gt; function,&lt;br&gt;&lt;br&gt;
we are specifying that only run the &lt;strong&gt;addNum&lt;/strong&gt; function if the URL matches exactly to &lt;strong&gt;"/add"&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In contrast, the "/" is a &lt;strong&gt;subtree path&lt;/strong&gt; (because it ends with a slash /) so if we go to any URL starting with the &lt;strong&gt;/&lt;/strong&gt; (other than the "/add"), the &lt;strong&gt;home&lt;/strong&gt; function will run as we defined &lt;strong&gt;mux.HandleFunc ("/", home)&lt;/strong&gt;, so any URL path starting with &lt;strong&gt;/&lt;/strong&gt; will run the function &lt;strong&gt;home&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Another example of a subtree path would be &lt;strong&gt;"/multiply/"&lt;/strong&gt;, and let's say its defined as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/multiply/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;mulNum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so if a user goes to the URL &lt;strong&gt;"/multiply/abcd"&lt;/strong&gt;, the servemux automatically redirects to &lt;strong&gt;"/multiply/"&lt;/strong&gt; and runs the &lt;strong&gt;mulNum&lt;/strong&gt; function, and even if the user goes to &lt;strong&gt;"/multiply"&lt;/strong&gt; (no trailing slash), the &lt;strong&gt;ServeMux&lt;/strong&gt; automatically adds a trailing slash and runs the &lt;strong&gt;mulNum&lt;/strong&gt; function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Longer URL wins
&lt;/h2&gt;

&lt;p&gt;In go's ServeMux, the longer URL always takes precedence over the shorter ones, no matter the order in which the routes are defined in the code,&lt;/p&gt;

&lt;p&gt;for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;home&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/message"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;viewMessage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/message/create"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;createMessage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;suppose a user goes to &lt;strong&gt;"/message/create"&lt;/strong&gt;, what happens is:&lt;br&gt;&lt;br&gt;
the &lt;strong&gt;"/"&lt;/strong&gt; pattern matches ✅&lt;br&gt;&lt;br&gt;
the &lt;strong&gt;"/message"&lt;/strong&gt; pattern also matches✅&lt;br&gt;&lt;br&gt;
the &lt;strong&gt;"/message/create"&lt;/strong&gt; pattern also matches ✅&lt;br&gt;&lt;br&gt;
but the pattern which is longest will be selected and its corresponding function will be run, i.e. in this case: &lt;strong&gt;createMessage&lt;/strong&gt; function will be run.&lt;/p&gt;

&lt;p&gt;So this gives a really cool side effect- we can register routes in any order and go's ServeMux will always match the URL with the longest matching pattern:&lt;/p&gt;

&lt;p&gt;(you can register the routes in any order ):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/message"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;viewMessage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;home&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/message/create"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;createMessage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While this might seem like a small thing but if you are coming from &lt;strong&gt;nodejs&lt;/strong&gt; and &lt;strong&gt;express&lt;/strong&gt;, you know the pain, route order matters there and getting it wrong can cause bugs. Go's ServeMux just handles it automatically.&lt;/p&gt;

</description>
      <category>backend</category>
      <category>go</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
