<?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: Mark Karamyar</title>
    <description>The latest articles on DEV Community by Mark Karamyar (@devmarkpro).</description>
    <link>https://dev.to/devmarkpro</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%2F399976%2Fa72b78ce-c7c2-47d6-a422-56c02e361994.jpeg</url>
      <title>DEV Community: Mark Karamyar</title>
      <link>https://dev.to/devmarkpro</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devmarkpro"/>
    <language>en</language>
    <item>
      <title>Generic in Golang</title>
      <dc:creator>Mark Karamyar</dc:creator>
      <pubDate>Wed, 17 Mar 2021 15:57:57 +0000</pubDate>
      <link>https://dev.to/devmarkpro/generic-in-golang-4jj8</link>
      <guid>https://dev.to/devmarkpro/generic-in-golang-4jj8</guid>
      <description>&lt;p&gt;This year started with new news from the golang team, &lt;a href="https://blog.golang.org/generics-proposal"&gt;"Adding Generic!"&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you have familiar with languages like Java or C#, you already familiar with the concept of Generics. For example, let's talk about sorting an array.&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;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"fmt"&lt;/span&gt;
    &lt;span class="s"&gt;"sort"&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="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&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;msg&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;v&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&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;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;v&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;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&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="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;80&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="s"&gt;"before sort"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;sort&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Ints&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&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="s"&gt;"after sort"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&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;In the above code, we just used the &lt;code&gt;sort&lt;/code&gt; package to sorting a slice of integers and print them out.&lt;/p&gt;

&lt;p&gt;Everything looks good, right? So what if we wanted to sort a slice of other numeric types? The &lt;code&gt;sort&lt;/code&gt; package has another function called &lt;a href="https://golang.org/pkg/sort/#Float64s"&gt;Float64s&lt;/a&gt; (instead of &lt;code&gt;Ints&lt;/code&gt;) but it's definitely not enough because we might need to do some sorting operation on other numeric types like &lt;code&gt;int32&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So one approach is to define one function for each numeric type but we all know it's not the best idea! it's the place that &lt;code&gt;Generic&lt;/code&gt; comes to picture.&lt;/p&gt;

&lt;p&gt;Here's the definition of Generic in &lt;a href="https://en.wikipedia.org/wiki/Generic_programming"&gt;Wikipedia&lt;/a&gt; :&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Generic programming is a style of computer programming in which algorithms are written in terms of types to-be-specified-later that are then instantiated when needed for specific types provided as parameters.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So the point of generic is, specify the type of the variable later and provide it as a parameter. That's exactly what we need for example in our sort function. The only thing that we need is to make sure our input &lt;strong&gt;comply&lt;/strong&gt; our conditions. for example in the sorting scenario, it must be &lt;code&gt;numeric&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's see how we can use generic in golang:&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;func&lt;/span&gt; &lt;span class="n"&gt;Bored&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;any&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;T&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;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&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;Bored&lt;/code&gt; function gives a slice of &lt;code&gt;T&lt;/code&gt; as input and returns the first item in the slice as output. So WTH is &lt;code&gt;T&lt;/code&gt;? first thing first, let's see how to use our &lt;code&gt;Bored&lt;/code&gt; function:&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;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Bored&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;"Generics"&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;output&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;That's great, a generic function is used exactly like a normal function! so let's talk about &lt;code&gt;T&lt;/code&gt;: In this case, &lt;code&gt;T&lt;/code&gt; is &lt;code&gt;string&lt;/code&gt;. so what's the difference between generic and the old-fashioned go &lt;code&gt;interface{}&lt;/code&gt;? I'll explain it with an 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;func&lt;/span&gt; &lt;span class="n"&gt;Bored&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="k"&gt;interface&lt;/span&gt;&lt;span class="p"&gt;{})&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"I am string"&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="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Bored&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;1.1&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="k"&gt;struct&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;output&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;The difference is, &lt;code&gt;interface{}&lt;/code&gt; could be anything but in the first function the type of &lt;code&gt;T&lt;/code&gt; determined while passing the first element to it. if it's a string, the rest of them also must be a string. otherwise, you'll get an error while compiling the application.&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;func&lt;/span&gt; &lt;span class="n"&gt;Bored&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;any&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;T&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;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&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="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Bored&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;"Generics"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&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;output&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;output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;type &lt;/span&gt;checking failed &lt;span class="k"&gt;for &lt;/span&gt;main
prog.go2:11:39: default &lt;span class="nb"&gt;type &lt;/span&gt;int of 1 does not match inferred &lt;span class="nb"&gt;type &lt;/span&gt;string &lt;span class="k"&gt;for &lt;/span&gt;T

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

&lt;/div&gt;



&lt;p&gt;It's nice, isn't it? for Java or C# developers something like &lt;code&gt;Repository&amp;lt;T&amp;gt;&lt;/code&gt; is pretty familiar which &lt;code&gt;T&lt;/code&gt; is almost always one of the application's entities.&lt;/p&gt;

&lt;p&gt;Back to &lt;code&gt;Golang&lt;/code&gt;, the other type (instead of &lt;code&gt;any&lt;/code&gt;) that introduced now, is &lt;code&gt;comparable&lt;/code&gt;. It means the item must have the capability of being in &lt;code&gt;equality operation&lt;/code&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;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"fmt"&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="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;Equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c"&gt;// true&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;Equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c"&gt;// false&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;Equal&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;comparable&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;input2&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;bool&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;input1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;input2&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;and same previous example we got a compile-time if pass multiple data type to this function:&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;func&lt;/span&gt; &lt;span class="n"&gt;main&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;Equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1.1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c"&gt;/*
output: type checking failed for main
prog.go2:8:25: default type int of 1 does not match inferred type float64 for T
*/&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;I think the golang team provides some ways for developers to define the restriction of the type &lt;code&gt;T&lt;/code&gt; themselves. for now, I just have seen &lt;code&gt;any&lt;/code&gt; and &lt;code&gt;comparable&lt;/code&gt;, if you know more typed parameter please share them in the comment section;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Working with big files in golang</title>
      <dc:creator>Mark Karamyar</dc:creator>
      <pubDate>Sat, 13 Mar 2021 23:00:00 +0000</pubDate>
      <link>https://dev.to/devmarkpro/working-with-big-files-in-golang-3817</link>
      <guid>https://dev.to/devmarkpro/working-with-big-files-in-golang-3817</guid>
      <description>&lt;p&gt;Today, I am going to show you how to read files in golang &lt;strong&gt;line-by-line&lt;/strong&gt;. Let's imagine that have a &lt;code&gt;jsonl&lt;/code&gt; file. What's &lt;a href="https://jsonlines.org/"&gt;jsonl&lt;/a&gt;? it's &lt;strong&gt;json lines&lt;/strong&gt; in a simple term, it's a file that each line of it represents a valid &lt;code&gt;json&lt;/code&gt; object. So if we read the file line by line, we can Marshal/Unmarshal each line of it separately. Here's an example of a &lt;code&gt;jsonl&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Each line of this file represents the data of a world cup.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{"year":"2018","host":"Russia","winner":"France"}
{"year":"2014","host":"Brazil","winner":"Germany"}
{"year":"2010","host":"South Africa","winner":"Spain"}
{"year":"2006","host":"Germany","winner":"Italy"}

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Working with bufio.Scanner
&lt;/h2&gt;

&lt;p&gt;So let’s read this file line by line most easily and conveniently. the easiest way to read a file (at least for me) is using the &lt;a href="https://golang.org/pkg/bufio/#Scanner"&gt;scanner&lt;/a&gt; from the &lt;strong&gt;bufio&lt;/strong&gt; package in the standard library. First, we need to create an instance with the &lt;code&gt;NewScanner&lt;/code&gt; function which is a very familiar way for constructing the structs in golang. this function accepts a Reader interface as input, and the good news is &lt;code&gt;os.File&lt;/code&gt; implemented this interface. It means, we can open a file and pass the pointer of the file to bufio.NewScanner. Let’s see it in action.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// first open the file
file, err := os.Open("/Users/Mark/fifa-winners.jsonl")
if err != nil {
    log.Fatalf("could not open the file: %v", err)
}
// don't forget to close the file.
defer file.Close()
// finally, we can have our scanner
scanner := bufio.NewScanner(file)

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

&lt;/div&gt;



&lt;p&gt;So, we have the scanner, we are ready to go... scanner has a function named &lt;strong&gt;Scan&lt;/strong&gt;. this function moves the scanner to the next token. I'll tell you what it means but for now, let's say each time &lt;strong&gt;Scan&lt;/strong&gt; called, we read one line of our file. So if we want to move the scanner all through the file, we'd call the scan function in an infinite loop! The question is &lt;strong&gt;How do we know, we can break the loop?&lt;/strong&gt; It's easy! Scan returns true as the return unless it meets the end of the file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for {
    if scanner.Scan() {
        // we have a new line in each iteration
        continue
    }
    // we are done let's break the loop
    break
}
// the rest of our spaghetti

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

&lt;/div&gt;



&lt;p&gt;This code works, but have you know we can say golang to keep a loop running until met a specific condition. All the code above can be as simple as the code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for scanner.Scan() {
    // we have a new line in each iteration
}
// the rest of our spaghetti

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

&lt;/div&gt;



&lt;p&gt;Well, Let's get back to the point! we can have our &lt;strong&gt;bytes&lt;/strong&gt; or &lt;strong&gt;string&lt;/strong&gt; in each line easily by calling &lt;code&gt;Bytes()&lt;/code&gt; and &lt;code&gt;Text()&lt;/code&gt; functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for scanner.Scan() {
  // b is an array of bytes ([]byte)
  b := scanner.Bytes()
  // s is string
  s := scanner.Text()
}

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

&lt;/div&gt;



&lt;p&gt;Frankly, These functions are the same! for example &lt;code&gt;string(scanner.Bytes())&lt;/code&gt; will give you the same result and that's what exactly happens in the &lt;code&gt;Text()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;We read our file, so is the mission completed? &lt;strong&gt;Not exactly&lt;/strong&gt; , because we didn't handle any error yet.&lt;/p&gt;

&lt;p&gt;the scanner has another function called &lt;code&gt;Err()&lt;/code&gt;. This function gives you the &lt;strong&gt;first&lt;/strong&gt; error that happened during the scan process. It means, when the scanner trying to move through the file, the &lt;em&gt;Scan&lt;/em&gt; function returns false, if something bad happened. So our loop instantly breaks and we will out of the loop. Now we can get that error and deal with it.&lt;/p&gt;

&lt;p&gt;If we want to know in each line of the file that error happened, we should use a traditional way, we know the scanner starts from the beginning of the file (line number 1) so we can define a variable outside of the loop that represents the line number and increase it in each iteration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lineNumber := 0
for scanner.Scan() {
    lineNumber++
        fmt.Println(scanner.Text())
}
// the rest of our spaghetti
if err := scanner.Err(); err != nil {
    log.Fatalf("something bad happened in the line %v: %v", lineNumber, err)
}

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

&lt;/div&gt;



&lt;p&gt;another thing that we should consider about the &lt;code&gt;Err()&lt;/code&gt; function is it ignores the &lt;code&gt;io.EOF&lt;/code&gt; so if we will give an error, it's a REAL one!&lt;/p&gt;

&lt;p&gt;Let's have a run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;➜ big-files (main) ✗ go run main.go
{"year":"2018","host":"Russia","winner":"France"}
{"year":"2014","host":"Brazil","winner":"Germany"}
{"year":"2010","host":"South Africa","winner":"Spain"}
{"year":"2006","host":"Germany","winner":"Italy"}

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

&lt;/div&gt;



&lt;p&gt;It worked, So what's next?&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix bufio.Scanner: token too long error
&lt;/h2&gt;

&lt;p&gt;We said, each line of a jsonl file represents a valid json, so it could be too long. We also said the &lt;code&gt;Scan&lt;/code&gt; function moves the scanner to the next token but the question is where is the next token!?&lt;/p&gt;

&lt;p&gt;The scanner function has another method that is not as famous as its siblings, &lt;code&gt;Buffer&lt;/code&gt; and it needs a buffer and an integer as input and you can set the maximum size of the buffer with this function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;bufio&lt;/strong&gt; package has a maximum token size which equals &lt;code&gt;64 * 1024&lt;/code&gt; (~65.6kb). So if one line of our lines is bigger than this size, we got this error &lt;code&gt;token too long error&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We found the answer to our question: The next token is where the scanner reaches max size (default 65kb) &lt;strong&gt;OR&lt;/strong&gt; the end of the line.&lt;/p&gt;

&lt;h3&gt;
  
  
  Approach 1: Bigger buffer size
&lt;/h3&gt;

&lt;p&gt;The first approach to tackling this problem is to increase the buffer size. Actually, the name &lt;code&gt;bufio.MaxScanTokenSize&lt;/code&gt; is a little misleading because it's not the actual maximum it's &lt;strong&gt;THE DEFAULT MAXIMUM&lt;/strong&gt; size. so we can increase it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;buf := []byte{}
scanner := bufio.NewScanner(file)
// increase the buffer size to 2Mb
scanner.Buffer(buf, 2048*1024)

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

&lt;/div&gt;



&lt;p&gt;Now we can process &lt;code&gt;jsonl&lt;/code&gt; files with lines up to 2Mb. It's good but what if we need more? We can increase this number as much as we want (probably) but if our file has &lt;em&gt;5.000.000&lt;/em&gt; rows and just one of them is 100Mb we need to increase our scanner to this size just for one line, or use another approach!&lt;/p&gt;

&lt;h3&gt;
  
  
  Approach2
&lt;/h3&gt;

&lt;p&gt;the next way to read such a tough file! is using another function! Bufio (buffer-io) gave us way more than a simple scanner to work with files and we have to choose one of them based on our needs and requirements. in this case, Scanner cannot satisfy what we need so let's take a look at &lt;code&gt;ReadLine&lt;/code&gt; function of &lt;code&gt;bufio.Reader&lt;/code&gt;. It's a little bit lower level than the scanner. generally speaking, when you hear the word &lt;code&gt;lower-level&lt;/code&gt; you should do more for simple things but you have more access and power!&lt;/p&gt;

&lt;p&gt;So let's get started. First we need a reader:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reader := bufio.NewReader(file)

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

&lt;/div&gt;



&lt;p&gt;reader, has &lt;strong&gt;ReadLine&lt;/strong&gt; function which &lt;em&gt;tries&lt;/em&gt; to read the entire line. Just like the scanner, we need to call this function in a for loop but since we are at the &lt;em&gt;lower level&lt;/em&gt;! we don't have a nice-easy boolean in return anymore to know that we can break the loop.&lt;/p&gt;

&lt;p&gt;the other difference is the error that we will give from the &lt;code&gt;ReadLine&lt;/code&gt; function, which can also be &lt;code&gt;io.EOF&lt;/code&gt;. It's not going to be a real error for us, so we have to handle it too.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reader := bufio.NewReader(file)
for {
    line, _, err := reader.ReadLine()
    if err != nil {
        if err == io.EOF {
            break
        }
        log.Fatalf("a real error happened here: %v\n", err)
    }
    fmt.Println(string(line))
}

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

&lt;/div&gt;



&lt;p&gt;As you probably already know, We just read the file so far, we did actually solve the problem that we had with the gigantic lines.&lt;/p&gt;

&lt;p&gt;we ignore the second parameter that we gave from the ReadLine function and that one is what we exactly need to solve our problem. It's a boolean named &lt;code&gt;isPrefix&lt;/code&gt;. If the line is too long and &lt;code&gt;ReadLine&lt;/code&gt; cannot put all of its content in the buffer, It returns the filled buffer and set isPrefix to true which means we will give the &lt;strong&gt;next part&lt;/strong&gt; of the line in the next call of the &lt;code&gt;ReadLine&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;So we just need to call the ReadLine function until &lt;code&gt;isPrefix&lt;/code&gt; becomes &lt;code&gt;false&lt;/code&gt; then we can go for the next line of our file. You probably already noticed that we are talking about a recursive function. First I define the function that we want to call recursively.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func read(r *bufio.Reader) ([]byte, error) {
    var (
        isPrefix = true
        err error
        line, ln []byte
    )

    for isPrefix &amp;amp;&amp;amp; err == nil {
        line, isPrefix, err = r.ReadLine()
        ln = append(ln, line...)
    }

    return ln, err
}

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

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;isPrefix&lt;/code&gt; is true at the first place and error is also nil so we make sure the for loop will run at least one time. It behaves like the do-while loop. We re-assign variables inside the loop so we call &lt;code&gt;r.ReadLine&lt;/code&gt; unless we got an error OR &lt;code&gt;isPrefix&lt;/code&gt; is false. in each iteration, we append the bytes that we get from &lt;code&gt;r.ReadLine()&lt;/code&gt; to another variable. Now it's time to call this function inside the main function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reader := bufio.NewReader(file)
for {
    line, err := read(reader)
    if err != nil {
        if err == io.EOF {
            break
        }
        log.Fatalf("a real error happened here: %v\n", err)
    }
    fmt.Println(string(line))
}

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

&lt;/div&gt;



&lt;p&gt;That's it! We solve the problem. here's the complete code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    "bufio"
    "fmt"
    "io"
    "log"
    "os"
)

func main() {
    // first open the file
    file, err := os.Open("./fifa-winners.jsonl")
    if err != nil {
        log.Fatalf("could not open the file: %v", err)
    }
    defer file.Close()
    log.Println(" *******************READ WITH SCANNER*******************")
    readWithScanner(file)
    log.Println("*******************READ WITH READLINE()*******************")

    // we just reset the offset. because we read this file once
    // imagine the cursor is in the end of the file so we have to get back to the first line and read it again 
    file.Seek(0, 0)
    readWithReadLine(file)

    log.Println("we read a file twice!")
}

// Read with simple scanner

func readWithScanner(file *os.File) {
    // first open the file
    file, err := os.Open("./fifa-winners.jsonl")
    if err != nil {
        log.Fatalf("could not open the file: %v", err)
    }
    // finally, we can have our scanner
    buf := []byte{}
    scanner := bufio.NewScanner(file)
    scanner.Buffer(buf, 2048*1024)
    lineNumber := 1
    for scanner.Scan() {
        fmt.Println(scanner.Text())
        lineNumber++
    }
    // the rest of our spaghetti
    if err := scanner.Err(); err != nil {
        log.Fatalf("something bad happened in the line %v: %v", lineNumber, err)
    }
}

// Read with Readline function

func read(r *bufio.Reader) ([]byte, error) {
    var (
        isPrefix = true
        err error
        line, ln []byte
    )

    for isPrefix &amp;amp;&amp;amp; err == nil {
        line, isPrefix, err = r.ReadLine()
        ln = append(ln, line...)
    }

    return ln, err
}

func readWithReadLine(file *os.File) {
    reader := bufio.NewReader(file)
    for {
        line, err := read(reader)
        if err != nil {
            if err == io.EOF {
                break
            }
            log.Fatalf("a real error happened here: %v\n", err)
        }
        fmt.Println(string(line))
    }
}

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

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>How to embed the content of a file to a variable in Golang</title>
      <dc:creator>Mark Karamyar</dc:creator>
      <pubDate>Thu, 11 Mar 2021 23:00:00 +0000</pubDate>
      <link>https://dev.to/devmarkpro/how-to-embed-the-content-of-a-file-to-a-variable-in-golang-hof</link>
      <guid>https://dev.to/devmarkpro/how-to-embed-the-content-of-a-file-to-a-variable-in-golang-hof</guid>
      <description>&lt;p&gt;In &lt;a href="https://devmarkpro.com/working-big-files-golang/"&gt;this article&lt;/a&gt;, we were talking about how to deal with a large file in Golang. But have you ever seen yourself in a situation that just wanted to read a file and load its entire content to a variable and work with it? I have been in the same situation a lot. For example for reading a specific configuration from a file and somehow, this file is always there! I mean you know that you need that file to build/run your application. Let's talk about it in code, Imagine we have a file named &lt;code&gt;config.txt&lt;/code&gt; with the content below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;I am a file that you need me ;)

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

&lt;/div&gt;



&lt;p&gt;And wanted to read this file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "os"
)

func main() {
    file, err := os.Open("config.txt")
    if err != nil {
        log.Fatalf("missing config file: %v", err)
    }
    content, err := ioutil.ReadAll(file)
    if err != nil {
        log.Fatalf("could not read config file: %v", err)
    }
    fmt.Println(string(content))
}
// output: I am a file that you need me ;)

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

&lt;/div&gt;



&lt;p&gt;if the code above sounds familiar to you, I have great news for you, Go offers a better, nice, and shorter way to do that. There is a package called &lt;a href="https://golang.org/pkg/embed/"&gt;embed&lt;/a&gt; do all of it for you in a single line. All you need is just pass the file path in a directive above your variable&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    _ "embed"
    "fmt"
)

//go:embed config.txt
var content string

func main() {
    fmt.Print(content)
}
// output: I am a file that you need me ;)

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

&lt;/div&gt;



&lt;p&gt;That's it! you have the content of the file in the content variable! do you need to have it in &lt;code&gt;[]byte&lt;/code&gt;? you just need to change your variable type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    _ "embed"
    "fmt"
)

//go:embed config.txt
var content []byte

func main() {
    fmt.Print(content)
}
// output: [73 32 97 109 32 97 32 102 105 108 101 32 116 104 97 116 32 121 111 117 32 110 101 101 100 32 109 101 32 59 41]

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

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Error handling in goroutines with errgroup</title>
      <dc:creator>Mark Karamyar</dc:creator>
      <pubDate>Mon, 11 Jan 2021 16:53:52 +0000</pubDate>
      <link>https://dev.to/devmarkpro/error-handling-in-goroutines-with-errgroup-5gjo</link>
      <guid>https://dev.to/devmarkpro/error-handling-in-goroutines-with-errgroup-5gjo</guid>
      <description>&lt;p&gt;&lt;a href="https://golang.org/"&gt;Golang&lt;/a&gt; made life easier with &lt;strong&gt;goroutine&lt;/strong&gt; , however, sometimes it's difficult to handle errors that happened inside a goroutine effectively. For example, imagine you have an array of some kind of actions and wanted to run a specific function on each one of them. On the other hand, in case of an error, you want to propagate that error to the higher function.&lt;/p&gt;

&lt;p&gt;Let's explain it in the code, Imagine that we have a set of &lt;code&gt;runners&lt;/code&gt; and each runner has a &lt;code&gt;Handle&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type HandlerFunc func(input string) error
type Runner struct {
    Name string
    Handle HandlerFunc
}

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

&lt;/div&gt;



&lt;p&gt;I also like to define another type named &lt;code&gt;Runners&lt;/code&gt;. It's just a simple wrapper around arrays of runners&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Runners []Runner

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

&lt;/div&gt;



&lt;p&gt;hence I can define a function that runs through all runners like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func (r Runners) Execute() error {
    for _, runner := range r {
        if err := runner.Handle(runner.Name); err != nil {
            return err
        }
    }
    return nil
}

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

&lt;/div&gt;



&lt;p&gt;and finally, define some runners and execute them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func main() {
    runners := Runners{
        Runner{
            Name: "1",
            Handle: func(input string) error {
                fmt.Printf("runner %s is running\n", input)
                return nil
            },
        },
        Runner{
            Name: "2",
            Handle: func(input string) error {
                return fmt.Errorf("something bad happened in runner [%s]", input)
            },
        },
        Runner{
            Name: "3",
            Handle: func(input string) error {
                fmt.Printf("runner %s is running\n", input)
                return nil
            },
        },
    }

    err := runners.Execute()
    if err != nil {
        fmt.Printf("execution failed: %v", err)
    }  
}

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

&lt;/div&gt;



&lt;p&gt;By running this piece of code I get this output: &lt;code&gt;runner 1 is running&lt;/code&gt;. The problem is we didn't run the third runner. So in this case we print errors and continue running the Execution but we cannot propagate the error to the higher function!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func (r Runners) Execute() error {
    for _, runner := range r {
        if err := runner.Handle(runner.Name); err != nil {
      fmt.Printf("error happened in runner [%s]: %v", runner.Name, err)
        }
    }
    return nil
}

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

&lt;/div&gt;



&lt;p&gt;Now, What if we want to run each runner in a different &lt;strong&gt;goroutine&lt;/strong&gt; with the exact same scenario? Let's change a code a little bit to see if it works.&lt;/p&gt;

&lt;p&gt;First thing first we add a &lt;strong&gt;go&lt;/strong&gt; behind the function call. So our &lt;code&gt;Execute&lt;/code&gt; function should be something like it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func (r Runners) Execute() error {
    for _, runner := range r {
        go func(runner Runner) error {
            if err := runner.Handle(runner.Name); err != nil {
                return err
            }
            return nil
        }(runner)
    }
    return nil
}

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

&lt;/div&gt;



&lt;p&gt;However, As you probably know if we run the program again, there will be nothing in output, because we never said the application to wait until the &lt;strong&gt;goroutines&lt;/strong&gt; finish their work. for sake of simplicity, let's just add a &lt;code&gt;Sleep&lt;/code&gt; to the &lt;code&gt;main&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;err := runners.Execute()
time.Sleep(3 * time.Second)
if err != nil {
    fmt.Printf("execution failed: %v", err)
}

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

&lt;/div&gt;



&lt;p&gt;Now, let's run it again. this time the output should be something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;runner 1 is running
runner 3 is running

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

&lt;/div&gt;



&lt;p&gt;OK, it's not OK! The execution worked well because we could execute runners 1 and 3, however, we still didn't do anything about the error.&lt;/p&gt;

&lt;h3&gt;
  
  
  Welcome to errgroup
&lt;/h3&gt;

&lt;p&gt;Now, it's time to solve the problem with &lt;a href="https://godoc.org/golang.org/x/sync/errgroup"&gt;errgroup&lt;/a&gt;. It's &lt;strong&gt;REALLY&lt;/strong&gt; simple and easy. It works like &lt;a href="https://gobyexample.com/waitgroups"&gt;waitgroups&lt;/a&gt; under the sync package. Honestly, it's using wait groups behind the scene but since the mentioned scenario is quite common, &lt;strong&gt;errgroup&lt;/strong&gt; make life easier for us!&lt;/p&gt;

&lt;p&gt;If you're familiar with wait group, You should know what &lt;code&gt;wg.Done()&lt;/code&gt; and &lt;code&gt;wg.Wait()&lt;/code&gt; mean. &lt;code&gt;errgroup&lt;/code&gt; offeres same thing. Let's make things clear in code. First, we declare an &lt;code&gt;g&lt;/code&gt; variable&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;g := new(errgroup.Group)

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

&lt;/div&gt;



&lt;p&gt;and run our execute function inside a &lt;strong&gt;goroutine&lt;/strong&gt; with &lt;code&gt;Go&lt;/code&gt; func. so our &lt;code&gt;Execute&lt;/code&gt; function turns to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func (r Runners) Execute() {
    for _, runner := range r {
        rx := runner
        g.Go(func() error {
            return rx.Handle(rx.Name)
        })
    }
}

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

&lt;/div&gt;



&lt;p&gt;the &lt;code&gt;Go&lt;/code&gt; function gives and &lt;code&gt;func&lt;/code&gt; which returns an error, if this error is not &lt;code&gt;nil&lt;/code&gt; you will have that in the returns of the &lt;code&gt;Wait&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;Here's the &lt;code&gt;Wait&lt;/code&gt; func:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;runners.Execute()
err := g.Wait()
if err != nil {
    fmt.Printf("execution failed: %v", err)
}

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

&lt;/div&gt;



&lt;p&gt;As you see, we don't need &lt;code&gt;time.Sleep()&lt;/code&gt; anymore, because the Wait func, waits until the last &lt;strong&gt;goroutine&lt;/strong&gt; gets the result and returns the &lt;strong&gt;first&lt;/strong&gt; error that happened. If all functions run without error, it returns &lt;code&gt;nil&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;the output is something like it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;runner 3 is running
runner 1 is running
execution failed: something bad happened in runner [2]

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

&lt;/div&gt;



&lt;p&gt;and here's the complete code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    "fmt"

    "golang.org/x/sync/errgroup"
)

var g errgroup.Group

type HandlerFunc func(input string) error

type Runner struct {
    Name string
    Handle HandlerFunc
}

type Runners []Runner

func (r Runners) Execute() {

    for _, runner := range r {
        rx := runner
        g.Go(func() error {
            return rx.Handle(rx.Name)
        })
    }
}

func main() {
    runners := Runners{
        Runner{
            Name: "1",
            Handle: func(input string) error {
                fmt.Printf("runner %s is running\n", input)
                return nil
            },
        },
        Runner{
            Name: "2",
            Handle: func(input string) error {
                return fmt.Errorf("something bad happened in runner [%s]", input)
            },
        },
        Runner{
            Name: "3",
            Handle: func(input string) error {
                fmt.Printf("runner %s is running\n", input)
                return nil
            },
        },
    }
    runners.Execute()
    err := g.Wait()
    if err != nil {
        fmt.Printf("execution failed: %v", err)
    }
}

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

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>chromedp: How to get the URL of the current page</title>
      <dc:creator>Mark Karamyar</dc:creator>
      <pubDate>Mon, 04 Jan 2021 19:05:16 +0000</pubDate>
      <link>https://dev.to/devmarkpro/chromedp-how-to-get-the-url-of-the-current-page-2lk8</link>
      <guid>https://dev.to/devmarkpro/chromedp-how-to-get-the-url-of-the-current-page-2lk8</guid>
      <description>&lt;p&gt;In the &lt;a href="https://devmarkpro.com/chromedp-get-started/"&gt;previous article&lt;/a&gt;, we developed a simple application and open google.com with chromedp. in this article we are going to take a look and one of the chromedp functionalities to deal with the page and URL.&lt;/p&gt;

&lt;p&gt;The way that the developers decided to use to do that is a very popular way along with the golang developers, Passing the reference of a variable to the function. I see the same pattern a lot in go libraries, for example, &lt;a href="https://gorm.io/index.html"&gt;GORM&lt;/a&gt; also using the same pattern.&lt;/p&gt;

&lt;p&gt;In today’s scenario, we want to open the chromedp Github page and go to one of the files inside the repository and print the URL of the page. Simple and easy! so let’s get started!!!&lt;/p&gt;

&lt;p&gt;For this purpose, we need to select the element that we want to click on it. Remember we just want to make things that we do manually, automatically. it means if we want to open the file named util_test.go, first, we need to go to the URL &lt;a href="https://github.com/chromedp/chromedp"&gt;https://github.com/chromedp/chromedp&lt;/a&gt; then find the file inside the list and then click on it. For finding the elements inside the page we can simply use &lt;a href="https://www.w3schools.com/cssref/css_selectors.asp"&gt;CSS Selectors&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// our selector to find the file inside the page.
selector := "a[title='util_test.go']"
tasks := chromedp.Tasks{
  // go to the page
    chromedp.Navigate("https://github.com/chromedp/chromedp"),
  // wait until the elemnt is visible and available inside the page
    chromedp.WaitEnabled(selector),
  // do a click on it
    chromedp.Click(selector),
}

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

&lt;/div&gt;



&lt;p&gt;so we already go to the page that we wanted the only step that still remains is getting the page URL. For this purpose, we define a string variable and pass it to the Location function of chromedp.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var u string
...
chromedp.Location(&amp;amp;u),

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

&lt;/div&gt;



&lt;p&gt;and here's the complete code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    "context"
    "fmt"

    "github.com/chromedp/chromedp"
)

func main() {
    var u string
    opts := append(
        // select all the elements after the third element
        chromedp.DefaultExecAllocatorOptions[3:],
        chromedp.NoFirstRun,
        chromedp.NoDefaultBrowserCheck,
    )
    // create chromedp's context
    parentCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
    defer cancel()

    selector := "a[title='util_test.go']"
    tasks := chromedp.Tasks{
        chromedp.Navigate("https://github.com/chromedp/chromedp"),
        chromedp.WaitEnabled(selector),
        chromedp.Click(selector),
        chromedp.Location(&amp;amp;u),
    }
    ctx, cancel := chromedp.NewContext(parentCtx)
    defer cancel()
    if err := chromedp.Run(ctx, tasks); err != nil {
        panic(err)
    }
    fmt.Printf("URL =&amp;gt; %s\n", u)
}

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

&lt;/div&gt;



&lt;p&gt;the output is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;URL =&amp;gt; https://github.com/chromedp/chromedp/blob/master/util_test.go

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

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>chromedp: Working with nodes and tabs</title>
      <dc:creator>Mark Karamyar</dc:creator>
      <pubDate>Sun, 03 Jan 2021 22:27:01 +0000</pubDate>
      <link>https://dev.to/devmarkpro/chromedp-working-with-nodes-and-tabs-3pcm</link>
      <guid>https://dev.to/devmarkpro/chromedp-working-with-nodes-and-tabs-3pcm</guid>
      <description>&lt;p&gt;Today we are going to have some fun with &lt;a href="https://github.com/chromedp/chromedp"&gt;chromedp&lt;/a&gt;. Today's scenario is gathering all the links on a page and loop through them. It should be simple, easy, and fun. let's get started.&lt;/p&gt;

&lt;p&gt;As we &lt;a href="https://www.devmarkpro.com/chromedp-get-current-page-url/"&gt;discussed before&lt;/a&gt;, for doing this kind of actions in chromedp, we need to pass the pointer of our variable to the corresponding function.&lt;/p&gt;

&lt;p&gt;We wanted to go to the &lt;a href="https://notepad-plus-plus.org/downloads/"&gt;Notpad++&lt;/a&gt; website, print all links and open them in different tab. So first we need to define our pointer to the array of nodes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var nodes []*cdp.Node

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

&lt;/div&gt;



&lt;p&gt;and then fill it with the &lt;code&gt;Nodes&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;selector := "#main ul li a"
pageURL := "https://notepad-plus-plus.org/downloads/"
chromedp.Run(ctx, chromedp.Tasks{
    chromedp.Navigate(pageURL),
    chromedp.WaitReady(selector),
    chromedp.Nodes(selector, &amp;amp;nodes),
})

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

&lt;/div&gt;



&lt;p&gt;so let's take a look at the nodes and see what we have on them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for _, n := range nodes {
  u := n.AttributeValue("href")
    fmt.Printf("node: %s | href = %s\n", n.LocalName, u)
}

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

&lt;/div&gt;



&lt;p&gt;the output is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node: a | href = https://notepad-plus-plus.org/downloads/v7.9.2/
node: a | href = https://notepad-plus-plus.org/downloads/v7.9.1/
node: a | href = https://notepad-plus-plus.org/downloads/v7.9/
node: a | href = https://notepad-plus-plus.org/downloads/v7.8.9/
node: a | href = https://notepad-plus-plus.org/downloads/v7.8.8/
node: a | href = https://notepad-plus-plus.org/downloads/v7.8.7/
node: a | href = https://notepad-plus-plus.org/downloads/v7.8.6/
node: a | href = https://notepad-plus-plus.org/downloads/v7.8.5/
node: a | href = https://notepad-plus-plus.org/downloads/v7.8.4/
node: a | href = https://notepad-plus-plus.org/downloads/v7.8.3/
node: a | href = https://notepad-plus-plus.org/downloads/v7.8.2/
...

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

&lt;/div&gt;



&lt;p&gt;so we grab all links, let's store them in an array, and then open them in a new tab. for opening a new tab in chromedp we need to create a new context from the context that we already have. We have a function name NewContext that does it for us.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;clone, cancel := chromedp.NewContext(ctx)
defer cancel()

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

&lt;/div&gt;



&lt;p&gt;from now on, the clone context will do everything in a new tab. the rest of the process is exactly the same so we can easily run chromedp tasks in our new context.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for _, n := range nodes {
    u := n.AttributeValue("href")
    clone, cancel := chromedp.NewContext(ctx)
    defer cancel()
    chromedp.Run(clone, chromedp.Navigate(u))
}

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

&lt;/div&gt;



&lt;p&gt;if you &lt;a href="https://devmarkpro.com/posts/chromedp-get-started/"&gt;disable headless mode&lt;/a&gt; and run the project, you see that URLs are opening one by one in a new tab. however, we can open all of them in a manner of second by using goroutine&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f := func(ctx context.Context, url string) {
    clone, cancel := chromedp.NewContext(ctx)
    defer cancel()
    chromedp.Run(clone, chromedp.Navigate(url))
}
for _, n := range nodes {
    u := n.AttributeValue("href")
    go f(ctx, u)
}

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

&lt;/div&gt;



&lt;p&gt;that's it, and here's the complete code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    "context"
    "fmt"
    "log"

    "github.com/chromedp/cdproto/cdp"
    "github.com/chromedp/chromedp"
)

func main() {
    ctx, cancel := chromedp.NewContext(context.Background(), chromedp.WithErrorf(log.Printf))
    defer cancel()
    var nodes []*cdp.Node
    selector := "#main ul li a"
    pageURL := "https://notepad-plus-plus.org/downloads/"
    if err := chromedp.Run(ctx, chromedp.Tasks{
        chromedp.Navigate(pageURL),
        chromedp.WaitReady(selector),
        chromedp.Nodes(selector, &amp;amp;nodes),
    }); err != nil {
        panic(err)
    }
    f := func(ctx context.Context, url string) {
        clone, cancel := chromedp.NewContext(ctx)
        defer cancel()
        fmt.Printf("%s is opening in a new tab\n", url)

        if err := chromedp.Run(clone, chromedp.Navigate(url)); err != nil {
            // do something nice with you errors!
            panic(err)
        }
    }
    for _, n := range nodes {
        u := n.AttributeValue("href")
        go f(ctx, u)
    }
}

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

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>chromedp: Get started to surfing the web with Go</title>
      <dc:creator>Mark Karamyar</dc:creator>
      <pubDate>Sat, 02 Jan 2021 18:30:12 +0000</pubDate>
      <link>https://dev.to/devmarkpro/chromedp-get-started-to-surfing-the-web-with-go-1k5g</link>
      <guid>https://dev.to/devmarkpro/chromedp-get-started-to-surfing-the-web-with-go-1k5g</guid>
      <description>&lt;p&gt;&lt;a href="https://github.com/chromedp/chromedp"&gt;chromedp&lt;/a&gt; helps you to flip through! the web &lt;strong&gt;fast&lt;/strong&gt; , &lt;em&gt;easy&lt;/em&gt;, and &lt;strong&gt;&lt;em&gt;programmatically&lt;/em&gt;&lt;/strong&gt; in Golang. If you have ever worked with &lt;a href="https://www.selenium.dev/"&gt;Selenium&lt;/a&gt; or &lt;a href="https://phantomjs.org/"&gt;PhantomJS&lt;/a&gt; or other similar tools, this concept is familiar to you.&lt;/p&gt;

&lt;p&gt;In this article we are going to get start working with &lt;a href="https://github.com/chromedp/chromedp"&gt;chromedp&lt;/a&gt; and do some simple tasks with it. Let's get started.&lt;/p&gt;

&lt;p&gt;First thing first you have to add chromedp to your project's dependency. It can done just like other &lt;code&gt;golang&lt;/code&gt; packages that you used before:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go get -u github.com/chromedp/chromedp

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

&lt;/div&gt;



&lt;p&gt;for starting work with chromedp, you do not need to do a lot! the only thing that you need to do is creating a &lt;code&gt;context&lt;/code&gt; and start working with it;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ctx, cancel := chromedp.NewContext(context.Background())
defer cancel()
chromedp.Run(ctx, chromedp.Navigate("https://www.google.com"))

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

&lt;/div&gt;



&lt;p&gt;That's it. Trust me, chromedp just open google even if you don't see anything yet! the reason that you didn't see the browser is by default it runs in headless mode and as I expected we can change the behavior. Actually, there are a list of default behaviors that passed to the library by default&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// DefaultExecAllocatorOptions are the ExecAllocator options used by NewContext
// if the given parent context doesn't have an allocator set up. Do not modify
// this global; instead, use NewExecAllocator. See ExampleExecAllocator.
var DefaultExecAllocatorOptions = [...]ExecAllocatorOption{
    NoFirstRun,
    NoDefaultBrowserCheck,
    Headless,

    // After Puppeteer's default behavior.
    Flag("disable-background-networking", true),
    Flag("enable-features", "NetworkService,NetworkServiceInProcess"),
    Flag("disable-background-timer-throttling", true),
    Flag("disable-backgrounding-occluded-windows", true),
    Flag("disable-breakpad", true),
    Flag("disable-client-side-phishing-detection", true),
    Flag("disable-default-apps", true),
    Flag("disable-dev-shm-usage", true),
    Flag("disable-extensions", true),
    Flag("disable-features", "site-per-process,TranslateUI,BlinkGenPropertyTrees"),
    Flag("disable-hang-monitor", true),
    Flag("disable-ipc-flooding-protection", true),
    Flag("disable-popup-blocking", true),
    Flag("disable-prompt-on-repost", true),
    Flag("disable-renderer-backgrounding", true),
    Flag("disable-sync", true),
    Flag("force-color-profile", "srgb"),
    Flag("metrics-recording-only", true),
    Flag("safebrowsing-disable-auto-update", true),
    Flag("enable-automation", true),
    Flag("password-store", "basic"),
    Flag("use-mock-keychain", true),
}

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

&lt;/div&gt;



&lt;p&gt;As you can see, the third item in the array is Headless so we can modify it. Let's create our configs for creating a new context.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;opts := append(
  // select all the elements after the third element
    chromedp.DefaultExecAllocatorOptions[3:],
    chromedp.NoFirstRun,
    chromedp.NoDefaultBrowserCheck,
)

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

&lt;/div&gt;



&lt;p&gt;the latest step is to create an ExecAllocator and pass it as our parent context.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;parentCtx, _ := chromedp.NewExecAllocator(context.Background(), opts...)
ctx, _ := chromedp.NewContext(parentCtx)

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

&lt;/div&gt;



&lt;p&gt;Now if you run the project you will see the browser open and navigates to the google website. here's the complete code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    "context"
    "fmt"

    "github.com/chromedp/chromedp"
)

func main() {
    opts := append(
        // select all the elements after the third element
        chromedp.DefaultExecAllocatorOptions[3:],
        chromedp.NoFirstRun,
        chromedp.NoDefaultBrowserCheck,
    )
    // create chromedp's context
    parentCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
    defer cancel()

    ctx, cancel := chromedp.NewContext(parentCtx)
    defer cancel()

    if err := chromedp.Run(ctx, chromedp.Navigate("https://www.google.com")); err != nil {
        panic(err)
    }

    fmt.Println("I've just saw Google!!!")
}

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

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>&lt;bits/stdc++.h&gt; in MacOS</title>
      <dc:creator>Mark Karamyar</dc:creator>
      <pubDate>Thu, 31 Dec 2020 23:00:00 +0000</pubDate>
      <link>https://dev.to/devmarkpro/bits-stdc-h-in-macos-50o2</link>
      <guid>https://dev.to/devmarkpro/bits-stdc-h-in-macos-50o2</guid>
      <description>&lt;p&gt;The &lt;code&gt;stdc++.h&lt;/code&gt; file is just a file with a bunch of &lt;code&gt;include&lt;/code&gt;. the only thing that you have to do is to put this file in the default location of the c++ import files. Here's a quick way to add it in MacOS.&lt;/p&gt;

&lt;h4&gt;
  
  
  With XCode installed
&lt;/h4&gt;

&lt;p&gt;create a file named &lt;code&gt;stdc++.h&lt;/code&gt; in this path: (you should create &lt;code&gt;bits&lt;/code&gt; folder first)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/bits&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Without XCode
&lt;/h4&gt;

&lt;p&gt;create a file named &lt;code&gt;stdc++.h&lt;/code&gt; in this path: (you should create &lt;code&gt;bits&lt;/code&gt; folder first)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/usr/local/include/bits&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, simply fill the created file with the content below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// C++ includes used for precompiling -*- C++ -*-

// Copyright (C) 2003-2013 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// &amp;lt;http://www.gnu.org/licenses/&amp;gt;.

/** @file stdc++.h
 * This is an implementation file for a precompiled header.
 */

// 17.4.1.2 Headers

// C
#ifndef _GLIBCXX_NO_ASSERT
#include &amp;lt;cassert&amp;gt;
#endif
#include &amp;lt;cctype&amp;gt;
#include &amp;lt;cerrno&amp;gt;
#include &amp;lt;cfloat&amp;gt;
#include &amp;lt;ciso646&amp;gt;
#include &amp;lt;climits&amp;gt;
#include &amp;lt;clocale&amp;gt;
#include &amp;lt;cmath&amp;gt;
#include &amp;lt;csetjmp&amp;gt;
#include &amp;lt;csignal&amp;gt;
#include &amp;lt;cstdarg&amp;gt;
#include &amp;lt;cstddef&amp;gt;
#include &amp;lt;cstdio&amp;gt;
#include &amp;lt;cstdlib&amp;gt;
#include &amp;lt;cstring&amp;gt;
#include &amp;lt;ctime&amp;gt;

#if __cplusplus &amp;gt;= 201103L
#include &amp;lt;ccomplex&amp;gt;
#include &amp;lt;cfenv&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;or you can download the file and put it the right folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir /usr/local/include/bits
curl https://gist.githubusercontent.com/devmarkpro/94d0b20a87bd0cc5f094a9b98336aa6d/raw/724c53e3210029f7bfd5dfa6254d124791547153/stdc++.h &amp;gt; /usr/local/include/bits/stdc++.h

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

&lt;/div&gt;



&lt;p&gt;NOTE: You have to repeat these steps each time that you update the &lt;code&gt;XCode Command-line Tools&lt;/code&gt; so if you know a permanent solution for this problem, please share it in the comment section.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
