<?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: Manu</title>
    <description>The latest articles on DEV Community by Manu (@manuraj17).</description>
    <link>https://dev.to/manuraj17</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%2F231035%2Fda7bc435-1bb5-49a0-82a1-f666297b98b7.png</url>
      <title>DEV Community: Manu</title>
      <link>https://dev.to/manuraj17</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/manuraj17"/>
    <language>en</language>
    <item>
      <title>Single responsibility methods in javascript</title>
      <dc:creator>Manu</dc:creator>
      <pubDate>Wed, 21 Oct 2020 11:26:20 +0000</pubDate>
      <link>https://dev.to/manuraj17/single-responsibility-methods-in-javascript-1an1</link>
      <guid>https://dev.to/manuraj17/single-responsibility-methods-in-javascript-1an1</guid>
      <description>&lt;p&gt;When working with a single file module which exports many methods, it becomes a pretty complex issue to maintain it in the long run as well as add tests. You can never know how the file is gonna grow, how many methods are going to get added eventually and how are we going to add tests for each of this with it's own stubs and mocks. &lt;/p&gt;

&lt;p&gt;Example for the type of situation I am talking about&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// app/services/order-service.js

const orderService = {};

orderService.serviceMethodA = () =&amp;gt; {};
orderService.serviceMethodB = () =&amp;gt; {};
orderService.serviceMethodC = () =&amp;gt; {};
orderService.serviceMethodD = () =&amp;gt; {};
...
...

module.exports = orderService;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I have actually stumbled upon such handles critical code and grew up to thousands of number of lines. &lt;/p&gt;

&lt;p&gt;Now, even for a method of ten lines, you may end up writing three test cases considering the edge cases involved. Now, imagine a file with tens of such methods. It can also happen that they may come with their own dependencies and external calls. So even if we want to write a test for &lt;code&gt;serviceMethodC&lt;/code&gt;, we will have to end up making sure the stubs and mocks are all in place for every other one so that the whole test doesn't fail.&lt;/p&gt;

&lt;p&gt;To counter this, a pattern that I recently started following was something of a sort of "Single Responsibilty Methods". Like the name suggests I started breaking down this file into multiple files, one for each method. All of them then imported into a single entry point file. &lt;/p&gt;

&lt;p&gt;Folder structure before change&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app
└── services
   └── order-service.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, after splitting up the methods into seperate files and adding an entry point file, the structure would look like below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app
└── services
   └── order-service
      ├── index.js
      ├── service-method-a.js
      ├── service-method-b.js
      ├── service-method-c.js
      └── service-method-d.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;index.js&lt;/code&gt; file looks like below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const orderService = {};

orderService.serviceMethodA = require('./service-method-a.js');
orderService.serviceMethodB = require('./service-method-b.js');
orderService.serviceMethodC = require('./service-method-c.js');
orderService.serviceMethodD = require('./service-method-d.js');

module.exports = orderService;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, to add tests, we can go ahead and add tests individually for each method, abstracted for it's own and not worried about the dependencies of other methods&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tests
└── services
   └── order-service
      ├── index.js
      ├── service-method-a-test.js
      ├── service-method-b-test.js
      ├── service-method-c-test.js
      └── service-method-d-test.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whole thing is much more clean, no more single large files and all mixed up dependencies etc. Also, collaboration is easier as if tasks are split each concerned person can work on their tests individually, no more merge conflicts!&lt;/p&gt;

&lt;p&gt;NOTE:&lt;br&gt;
One thing that I have personally noticed was that, when we write any service object, a repository object or a controller object, we append the object type to the end of the file name.&lt;br&gt;
Eg: order-service, order-repository, orders-controller&lt;br&gt;
Advantage being it clearly denotes what the file is and also aids in fuzzy searching it. &lt;br&gt;
With the current approach, we are breaking down such a file. It reduces the search-ability and it also could happen that there can be two similarly named methods under different contexts. Such breaking down also could prevent editors to navigate via "intellisense" to the method definition. &lt;br&gt;
Although, these are not big issues and can also be solved if we just be extra careful and look at the file path. In my case I could trade if for the advantages that I received. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>softwareengineering</category>
      <category>node</category>
    </item>
    <item>
      <title>Dynamic parameters in Go</title>
      <dc:creator>Manu</dc:creator>
      <pubDate>Sun, 23 Feb 2020 05:45:24 +0000</pubDate>
      <link>https://dev.to/manuraj17/dynamic-parameters-in-go-2akh</link>
      <guid>https://dev.to/manuraj17/dynamic-parameters-in-go-2akh</guid>
      <description>&lt;p&gt;Coming from a dynamic programming background it was a bit of getting used to, to the typed ecosystem of go especially with function signatures. &lt;/p&gt;

&lt;p&gt;One of the things that was boggling me was how to pass dynamic parameters to a function. &lt;br&gt;
In ruby we could&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;someFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arg1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;moreArgs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;moreArgs&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we could call &lt;code&gt;somefunc("hello", "george", "joseph")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It was all simple there, but coming to go, it wasn't that straightforward, although go provides a way to do this. Look at the below snippet&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;someFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arg1&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arg1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arg1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&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;Observing the snippet line by line, notice the last parameter of &lt;code&gt;someFunc&lt;/code&gt;, it is called &lt;code&gt;variadic parameter&lt;/code&gt;. From the go docs&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; If f is variadic with a final parameter p of type ...T, then within f the type of p is equivalent to type []T. If f is invoked with no actual arguments for p, the value passed to p is nil. Otherwise, the value passed is a new slice of type []T with a new underlying array whose successive elements are the actual arguments, which all must be assignable to T. The length and capacity of the slice is therefore the number of arguments bound to p and may differ for each call site.

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Okay, what it means is that if the final parameter of a method is of type ...T, then it is considered as type []T inside that method. What this means is that it works similar to &lt;code&gt;*moreArgs&lt;/code&gt; in ruby. If we need to pass multiple arguments of type T, we can and they all will be available in the slice []T.&lt;/p&gt;

&lt;p&gt;We could call the function as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight go"&gt;&lt;code&gt;    &lt;span class="n"&gt;someFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;someFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"apple"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;someFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"apple"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"orange"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>go</category>
    </item>
    <item>
      <title>Functional ruby</title>
      <dc:creator>Manu</dc:creator>
      <pubDate>Thu, 20 Feb 2020 10:32:36 +0000</pubDate>
      <link>https://dev.to/manuraj17/functional-ruby-460d</link>
      <guid>https://dev.to/manuraj17/functional-ruby-460d</guid>
      <description>&lt;p&gt;Having written elixir for a while I got curious to see what all can be done in&lt;br&gt;
ruby, in a functinoal way mostly in comparison to Elixir. Below are some of the&lt;br&gt;
ruby methodologies that I found. I am skipping lambdas and procs for now since&lt;br&gt;
they are widely discussed at many places. The snippets are self explanatory.&lt;/p&gt;
&lt;h2&gt;
  
  
  Splat &lt;code&gt;*&lt;/code&gt; and destructuring a list
&lt;/h2&gt;

&lt;p&gt;Never knew I could deconstruct an array like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  first, *rest = [1,2,3]
  # =&amp;gt; first == 1
  # =&amp;gt; rest == [2,3]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The above led me to reimagine &lt;code&gt;#map&lt;/code&gt; in a functional way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr = [1,2,3]
fn = -&amp;gt;(x) { |x| x*2 }

def remap(arr, fn)
  return if arr == []

  first, *rest = [1,2,3]

  first, *rest = arr
  [].push(fn.(first)).push(remap(rest, fn))
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Shorthand proc invocation
&lt;/h2&gt;

&lt;p&gt;Ruby has procs and lambdas, for this post we are not digging on those two topics&lt;br&gt;
much.&lt;/p&gt;

&lt;p&gt;Creating a proc&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;double = Proc.new { |x| x*2 }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Creating a lambda&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;double = -&amp;gt;(x) { x*2 }
lambda = lambda { "Lambda" }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Back to the current section, you would know this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def main
  [1,2,3].map(&amp;amp;:+)
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The above snippet retruns the sum of the elements.&lt;/p&gt;

&lt;p&gt;Let's decrypt &lt;code&gt;(&amp;amp;:+)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;amp;&lt;/code&gt; is shorthand for &lt;a href="http://ruby-doc.org/core-2.0.0/Symbol.html#method-i-to_proc"&gt;to_proc&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def to_proc
  proc { |obj, *args| obj.send(self, *args) }
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;:&lt;/code&gt; is just a symbol notation (Read on ruby symbols)&lt;br&gt;
&lt;code&gt;+&lt;/code&gt; the operator&lt;/p&gt;

&lt;p&gt;Now essnetially, you could look at it as &lt;code&gt;&amp;amp; :+&lt;/code&gt;, so a symbol &lt;code&gt;:+&lt;/code&gt; is passed.&lt;br&gt;
Now, as per the definition of to_proc the symbol is called upon the object&lt;br&gt;
itself.&lt;/p&gt;

&lt;p&gt;So if you pass a symbol, it is turned into a proc.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Symbol#to_proc&lt;/code&gt; was added to address this common pattern&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr.map do { |x| x.upcase }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This then becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr.map(&amp;amp;:upcase)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now in order to create a custom functions that we can pass to map we should use&lt;br&gt;
a lambda. Lambdas are essentially anonymous functions.&lt;/p&gt;

&lt;p&gt;If you are familiar with javascript, you would have used lot of anonymous&lt;br&gt;
functions for defining callbacks. On a similar anology we can proceed with a&lt;br&gt;
squaring function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;irb&lt;span class="o"&gt;(&lt;/span&gt;main&lt;span class="o"&gt;)&lt;/span&gt;:004:0&amp;gt; &lt;span class="o"&gt;[&lt;/span&gt;1,2,3].map&lt;span class="o"&gt;(&lt;/span&gt;&amp;amp;&lt;span class="o"&gt;(&lt;/span&gt;-&amp;gt;&lt;span class="o"&gt;(&lt;/span&gt;x&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; x&lt;span class="k"&gt;*&lt;/span&gt;x&lt;span class="o"&gt;}))&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;1, 4, 9]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Looks cryptic but you can see that we created an anonymous function and passed&lt;br&gt;
it in with &lt;code&gt;&amp;amp;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We could take it out and define the function somewhere else giving&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;irb&lt;span class="o"&gt;(&lt;/span&gt;main&lt;span class="o"&gt;)&lt;/span&gt;:005:0&amp;gt; fn &lt;span class="o"&gt;=&lt;/span&gt; -&amp;gt;&lt;span class="o"&gt;(&lt;/span&gt;x&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; x&lt;span class="k"&gt;*&lt;/span&gt;x &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="c"&gt;#&amp;lt;Proc:0x00007f89f19b3cf0@(irb):5 (lambda)&amp;gt;&lt;/span&gt;
irb&lt;span class="o"&gt;(&lt;/span&gt;main&lt;span class="o"&gt;)&lt;/span&gt;:006:0&amp;gt; &lt;span class="o"&gt;[&lt;/span&gt;1,2,3].map&lt;span class="o"&gt;(&lt;/span&gt;&amp;amp;fn&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;1, 4, 9]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's take a step back now, we just discussed &lt;code&gt;&amp;amp;&lt;/code&gt; essentially calls &lt;code&gt;#to_proc&lt;/code&gt;&lt;br&gt;
on it.&lt;/p&gt;

&lt;p&gt;Another important thing to remember, if we use a symbol the corresponding method&lt;br&gt;
is called on the object itself i.e&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;irb&lt;span class="o"&gt;(&lt;/span&gt;main&lt;span class="o"&gt;)&lt;/span&gt;:009:0&amp;gt; &lt;span class="o"&gt;[&lt;/span&gt;1,2,3].map&lt;span class="o"&gt;(&lt;/span&gt;&amp;amp;:fn&lt;span class="o"&gt;)&lt;/span&gt;
Traceback &lt;span class="o"&gt;(&lt;/span&gt;most recent call last&lt;span class="o"&gt;)&lt;/span&gt;:
        5: from /Users/manu/.rbenv/versions/2.6.3/bin/irb:23:in &lt;span class="sb"&gt;`&lt;/span&gt;&amp;lt;main&amp;gt;&lt;span class="s1"&gt;'
        4: from /Users/manu/.rbenv/versions/2.6.3/bin/irb:23:in `load'&lt;/span&gt;
        3: from /Users/manu/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in &lt;span class="sb"&gt;`&lt;/span&gt;&amp;lt;top &lt;span class="o"&gt;(&lt;/span&gt;required&lt;span class="o"&gt;)&amp;gt;&lt;/span&gt;&lt;span class="s1"&gt;'
        2: from (irb):9
        1: from (irb):9:in `map'&lt;/span&gt;
NoMethodError &lt;span class="o"&gt;(&lt;/span&gt;undefined method &lt;span class="sb"&gt;`&lt;/span&gt;fn&lt;span class="s1"&gt;' for 1:Integer)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Since &lt;code&gt;Integer&lt;/code&gt; doesn't have a method &lt;code&gt;fn&lt;/code&gt;, it failed.&lt;/p&gt;

&lt;p&gt;Hence,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;irb(main):011:0&amp;gt; [1,2,3].map(&amp;amp;:odd?)
=&amp;gt; [true, false, true]
irb(main):012:0&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That's it!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>functional</category>
    </item>
    <item>
      <title>Formatting error messages in Go</title>
      <dc:creator>Manu</dc:creator>
      <pubDate>Wed, 18 Sep 2019 05:14:46 +0000</pubDate>
      <link>https://dev.to/manuraj17/formatting-error-messages-in-go-345f</link>
      <guid>https://dev.to/manuraj17/formatting-error-messages-in-go-345f</guid>
      <description>&lt;p&gt;In go errors are values, and hence error management is an explicit process. There is no exception handling or bubbling them up, you get an error and you decide what do with it often leading to verbose scenarios as below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;someFunc&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&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;While writing a tool recently, one of my use cases had a method invoking some system functions. This led me to a scenario to return back formatted error messages, and to do that I ended up with &lt;code&gt;fmt.Errorf()&lt;/code&gt;[1]&lt;/p&gt;

&lt;p&gt;It allows us to return formatted errors like below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;externalFunc&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Errorf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Calling external function failed: %s"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&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;&lt;code&gt;errors.New()&lt;/code&gt; doesn't allow us to format text, but only pass a string. Otherwise we would have to use &lt;code&gt;Sprintf&lt;/code&gt; or equivalent to create a formatted string and then pass to &lt;code&gt;errors.New()&lt;/code&gt;. Although, guess what, that's what the method internally does. &lt;/p&gt;

&lt;p&gt;References:&lt;br&gt;
[1] &lt;a href="https://godoc.org/fmt#Errorf"&gt;https://godoc.org/fmt#Errorf&lt;/a&gt;&lt;/p&gt;

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