<?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: Wassim Ben Jdida</title>
    <description>The latest articles on Forem by Wassim Ben Jdida (@wassimbj).</description>
    <link>https://forem.com/wassimbj</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%2F148884%2F07f45149-f2d9-407c-8f0a-094c06125a97.jpg</url>
      <title>Forem: Wassim Ben Jdida</title>
      <link>https://forem.com/wassimbj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/wassimbj"/>
    <language>en</language>
    <item>
      <title>How to upload files in Golang ?</title>
      <dc:creator>Wassim Ben Jdida</dc:creator>
      <pubDate>Fri, 02 Jun 2023 11:08:00 +0000</pubDate>
      <link>https://forem.com/wassimbj/how-to-upload-files-in-golang--10p5</link>
      <guid>https://forem.com/wassimbj/how-to-upload-files-in-golang--10p5</guid>
      <description>&lt;p&gt;In this blog I will be using the &lt;strong&gt;native Golang http package&lt;/strong&gt; to upload files, no third party packages. We will see how to upload single and multiple files.&lt;/p&gt;

&lt;h2&gt;
  
  
  The HTML form
&lt;/h2&gt;

&lt;p&gt;The form is simple and straight forward. The only thing to note is the &lt;code&gt;enctype="multipart/form-data"&lt;/code&gt; attribute. This is required for uploading files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;"/upload"&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"POST"&lt;/span&gt; &lt;span class="na"&gt;enctype=&lt;/span&gt;&lt;span class="s"&gt;"multipart/form-data"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;placeholder=&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"ok"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"checkbox"&lt;/span&gt; &lt;span class="na"&gt;placeholder=&lt;/span&gt;&lt;span class="s"&gt;"ok"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"aaaaa"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt; aaaaa
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"ok"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"checkbox"&lt;/span&gt; &lt;span class="na"&gt;placeholder=&lt;/span&gt;&lt;span class="s"&gt;"ok"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"bbbbbb"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt; bbbbbb
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"file"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"file"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"file"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;SUBMIT!&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Single file upload
&lt;/h2&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;uploadSingleFile&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="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;header&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&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;FormFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"file"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

  &lt;span class="c"&gt;// create a destination file&lt;/span&gt;
  &lt;span class="n"&gt;dst&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filepath&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Join&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;header&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Filename&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="k"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;dst&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

  &lt;span class="c"&gt;// upload the file to destination path&lt;/span&gt;
  &lt;span class="n"&gt;nb_bytes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;io&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dst&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;file&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="s"&gt;"File uploaded successfully"&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;strong&gt;Note:&lt;/strong&gt; All of the &lt;code&gt;_&lt;/code&gt; underscore variables are errors, I just ignored them for the sake of simplicity. so don't do that in production.&lt;/p&gt;

&lt;p&gt;As you noticed we get the uploaded file with the &lt;code&gt;r.FormFile("input_name_here")&lt;/code&gt; function, it returns 3 variables, &lt;code&gt;file&lt;/code&gt; which is the uploaded file itself, &lt;code&gt;header&lt;/code&gt; which contains information about the file like the name, size, MIME type etc..., you can use these information to validate the file before uploading it, and finally &lt;code&gt;_&lt;/code&gt; which is the error if any.&lt;/p&gt;

&lt;h2&gt;
  
  
  Multiple files upload
&lt;/h2&gt;

&lt;p&gt;First of all before you can select multiple files, you need to add the &lt;code&gt;multiple&lt;/code&gt; attribute to the file input&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;uploadMultipleFiles&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="c"&gt;// key = "file" in the form&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;files&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;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MultipartForm&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;File&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;file&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;files&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;dst&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filepath&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Join&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;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Filename&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;_&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="n"&gt;io&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dst&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="p"&gt;}&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;Just FYI, the &lt;code&gt;r.MultipartForm.File&lt;/code&gt; is a &lt;code&gt;map[string][]*multipart.FileHeader&lt;/code&gt; where the key is the name of the input in the form. The &lt;code&gt;file&lt;/code&gt; variable contains the same information as the &lt;code&gt;header&lt;/code&gt; variable in the single file upload example, (size, name, MIME type etc...)&lt;/p&gt;

&lt;h2&gt;
  
  
  The other fields
&lt;/h2&gt;

&lt;p&gt;To get the other fields, there are two options, which at some point you will need to use both of them.&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="c"&gt;// OPTION 1: returns a string which is the first value of the key&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;FormValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// string&lt;/span&gt;

&lt;span class="c"&gt;// OPTION 2: returns a slice of strings of all the values of the key&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;MultipartForm&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c"&gt;// map[string][]string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can use the first option when you want to get a single value, and the second option when you want to get multiple values, in our case the checkbox can have multiple values.&lt;/p&gt;

&lt;p&gt;So &lt;code&gt;r.FormValue("name")&lt;/code&gt; is like doing &lt;code&gt;r.MultipartForm.Value["name"][0]&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;That's it, I hope you enjoyed this tutorial. this blog is originally posted on my &lt;a href="https://wassimbj.github.io/blog/how-to-upload-files-in-golang"&gt;blog&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>files</category>
      <category>forms</category>
    </item>
    <item>
      <title>The pragmatic programmer book tips (Part 4)</title>
      <dc:creator>Wassim Ben Jdida</dc:creator>
      <pubDate>Sun, 14 Nov 2021 07:11:57 +0000</pubDate>
      <link>https://forem.com/wassimbj/the-pragmatic-programmer-book-tips-part-4-9kh</link>
      <guid>https://forem.com/wassimbj/the-pragmatic-programmer-book-tips-part-4-9kh</guid>
      <description>&lt;h3&gt;
  
  
  #30 - You Can't Write Perfect Software
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;But Pragmatic Programmers take this a step further. They don't trust themselves, either. Knowing that no one writes perfect code, including themselves, Pragmatic Programmers code in defenses against their own mistakes.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  #31 - Design with Contracts
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;In languages that do not support DBC (Design By Contracts) in the code, this might be as far as you can go—and that's not too bad. DBC is, after all, a design technique. Even without automatic checking, you can put the contract in the code as comments and still get a very real benefit. If nothing else, the commented contracts give you a place to start looking when trouble strikes.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  #32 - Crash Early
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;One of the benefits of detecting problems as soon as you can is that you can crash earlier. And many times, crashing your program is the best thing you can do. The alternative may be to continue, writing corrupted data to some vital database or commanding the washing machine into its twentieth consecutive spin cycle.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  #33 - If It Can't Happen, Use Assertions to Ensure That It Won't
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;"This code won't be used 30 years from now, so two-digit dates are fine." "This application will never be used abroad, so why internationalize it?" "count can't be negative." "This printf can't fail."&lt;br&gt;
Let's not practice this kind of self-deception, particularly when coding.&lt;br&gt;
Whenever you find yourself thinking "but of course that could never happen," add code to check it.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  #34 - Use Exceptions for Exceptional Problems
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;One of the problems with exceptions is knowing when to use them. We believe that exceptions should rarely be used as part of a program's normal flow; exceptions should be reserved for unexpected events. Assume that an uncaught exception will terminate your program and ask yourself, "Will this code still run if I remove all the exception handlers?" If the answer is "no," then maybe exceptions are being used in nonexceptional circumstances.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  #35 - Finish What You Start
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;We all manage resources whenever we code: memory, transactions, threads, files, timers—all kinds of things with limited availability. Most of the time, resource usage follows a predictable pattern: you allocate the resource, use it, and then deallocate it. However, many developers have no consistent plan for dealing with resource allocation and deallocation.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  #36 - Minimize Coupling Between Modules
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;You can see symptoms of this phenomenon in a number of ways:&lt;/em&gt;&lt;br&gt;
&lt;em&gt;1. Large C or C++ projects where the command to link a unit test is longer than the test program itself&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;2. "Simple" changes to one module that propagate through unrelated modules in the system&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;3. Developers who are afraid to change code because they aren't sure what might be affected&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The Law of Demeter for functions attempts to minimize coupling between modules in any given program. It tries to prevent you from reaching into an object to gain access to a third object's methods.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  #37 - Configure, Don't Integrate
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;First, we want to make our systems highly configurable. Not just things such as screen colors and prompt text, but deeply ingrained items such as the choice of algorithms, database products, middleware technology, and user-interface style.&lt;br&gt;
These items should be implemented as configuration options, not through integration or engineering.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  #38 - Put Abstractions in Code Details in Metadata
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Use metadata to describe configuration options for an application: tuning parameters, user preferences, the installation directory, and so on.&lt;/em&gt;&lt;br&gt;
&lt;em&gt;Metadata is any data that describes the application—how it should run, what resources it should use, and so on. Typically, metadata is accessed and used at runtime, not at compile time. You use metadata all the time—at least your programs do. Suppose you click on an option to hide the toolbar on your Web browser. The browser will store that preference, as metadata, in some sort of internal database.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  #39 - Analyze Workflow to Improve Concurrency
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;In many projects, we need to model and analyze the user's workflows as part of requirements analysis. We'd like to find out what can happen at the same time, and what must happen in a strict order. One way to do this is to capture their description of workflow using a notation such as the UML activity diagram&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;

</description>
      <category>programming</category>
      <category>webdev</category>
      <category>engineering</category>
    </item>
    <item>
      <title>The pragmatic programmer book tips (Part 3)</title>
      <dc:creator>Wassim Ben Jdida</dc:creator>
      <pubDate>Tue, 09 Nov 2021 20:49:40 +0000</pubDate>
      <link>https://forem.com/wassimbj/the-pragmatic-programmer-book-tips-part-3-12id</link>
      <guid>https://forem.com/wassimbj/the-pragmatic-programmer-book-tips-part-3-12id</guid>
      <description>&lt;h3&gt;
  
  
  #20 - Keep Knowledge in Plain Text
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Plain text is made up of printable characters in a form that can be read and understood directly by people,&lt;br&gt;
e.g: Fieldl9=467abe is meaningless, DrawingType=UMLActivityDrawing is better.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  #21 - Use the Power of Command Shells
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Every woodworker needs a good, solid, reliable workbench, somewhere to hold work pieces at a convenient height while he or she works them. The workbench becomes the center of the wood shop, the craftsman returning to it time and time again as a piece takes shape. For a programmer manipulating files of text, that workbench is the command shell.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  #22 - Use a Single Editor Well
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;We think it is better to know one editor very well, and use it for all editing tasks: code, documentation, memos, system administration, and so on&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  #23 - Always Use Source Code Control
&lt;/h3&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  #24 - Fix the Problem, Not the Blame
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Embrace the fact that debugging is just problem solving, and attack it as such. It doesn't really matter whether the bug is your fault or someone else's. It is still your problem.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  #25 - Don't Panic
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;If your first reaction on witnessing a bug or seeing a bug report is "that's impossible," you are plainly wrong. Don't waste a single neuron on the train of thought that begins "but that can't happen" because quite clearly it can, and has.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  #26 - "select" Isn't Broken
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;We worked on a project where a senior engineer was convinced that the select system call was broken on Solaris. No amount of persuasion or logic could change his mind (the fact that every other networking application on the box worked fine was irrelevant). He spent weeks writing work-arounds, which, for some odd reason, didn't seem to fix the problem. &lt;br&gt;
When finally forced to sit down and read the documentation on select, he discovered the problem and corrected it in a matter of minutes. We now use the phrase "select is broken" as a gentle reminder whenever one of us starts blaming the system for a fault that is likely to be our own.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  #27 - Don't Assume It—Prove It
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Don't gloss over a routine or piece of code involved in the bug &lt;br&gt;
because you "know" it works. Prove it. Prove it in this context, with this data, with these boundary conditions.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;p&gt;&lt;em&gt;When you come across a surprise bug, beyond merely fixing it, you need to determine why this failure wasn't caught earlier. Consider whether you need to amend the unit or other tests so that they would have caught it.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  #28 - Learn a Text Manipulation Language
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;There is a growing number of good text manipulation languages. Unix developers often like to use the power of their command shells, augmented with tools such as awk and sed. People who prefer a more structured tool like the object-oriented nature of Python. Some people use Tcl as their tool of choice. We happen to prefer Perl for hacking out short scripts.&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media.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%2Fveruggom1ok6tkocv7hz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fveruggom1ok6tkocv7hz.png" alt="Text Manipulation Language"&gt;&lt;/a&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  #29 - Write Code That Writes Code
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;In the same way a woodworker invests the time in a jig, a programmer can build a code generator. Once built, it can be used throughout the life of the project at virtually no cost.&lt;/em&gt;&lt;br&gt;
 &lt;br&gt;
&lt;em&gt;There are two main types of code generators: Passive code generators are run once to produce a result, Active code generators are used each time their results are required&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;

</description>
      <category>programming</category>
      <category>webdev</category>
      <category>engineering</category>
    </item>
    <item>
      <title>The pragmatic programmer book tips (Part 2)</title>
      <dc:creator>Wassim Ben Jdida</dc:creator>
      <pubDate>Mon, 27 Sep 2021 13:54:47 +0000</pubDate>
      <link>https://forem.com/wassimbj/the-pragmatic-programmer-book-tips-part-2-3213</link>
      <guid>https://forem.com/wassimbj/the-pragmatic-programmer-book-tips-part-2-3213</guid>
      <description>&lt;h3&gt;
  
  
  #10 - It's Both What You Say and the Way You Say It
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;It's not just what you've got, but also how you package it. Having the best ideas, the finest code, or the most pragmatic thinking is ultimately sterile unless you can communicate with other people. A good idea is an orphan without effective communication.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;
&lt;h3&gt;
  
  
  #11 - DRY—Don't Repeat Yourself
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Every piece of knowledge (code, docs, ...) must have a single, unambiguous, authoritative representation within a system.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;
&lt;h3&gt;
  
  
  #12 - Make It Easy to Reuse
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;if you fail to reuse, you risk duplicating knowledge.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;
&lt;h3&gt;
  
  
  #13 - Eliminate Effects Between Unrelated Things
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;In a well-designed system, the database code will be orthogonal to the user interface: you can change the interface without affecting the database, and swap databases without changing the interface.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;
&lt;h3&gt;
  
  
  #14 - There Are No Final Decisions
&lt;/h3&gt;

&lt;p&gt; &lt;/p&gt;
&lt;h3&gt;
  
  
  #15 - Use Tracer Bullets to Find the Target
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;when you're building something that hasn't been built before. Like the gunners, you're trying to hit a target in the dark. Because your users have never seen a system like this before, their requirements may be vague. Because you may be using algorithms, techniques, languages, or libraries you aren't familiar with, you face a large number of unknowns. And because projects take time to complete, you can pretty much guarantee the environment you're working in will change before you're done..&lt;/em&gt;&lt;br&gt;
&lt;em&gt;we're looking for something that gets us from a requirement &lt;br&gt;
to some aspect of the final system quickly, visibly, and repeatably.&lt;/em&gt;&lt;br&gt;
&lt;em&gt;It simply is not fully functional. However, once you have achieved an end-to-end connection among the components of your system, you can check how close to the target you are, adjusting if necessary. Once you're on target, adding functionality is easy.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;
&lt;h3&gt;
  
  
  #16 - Prototype to Learn
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Prototypes are designed to answer just a few questions, so they are much cheaper and faster to develop than applications that go into production. You can prototype Architecture, New functionality in an existing system, Structure or contents of external data, Third-party tools or components, Performance issues, User interface design.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;
&lt;h3&gt;
  
  
  #17 - Program Close to the Problem domain
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;By coding at a higher level of abstraction, you are free to concentrate on solving domain problems, and can ignore petty implementation details.&lt;/em&gt;&lt;br&gt;
&lt;em&gt;you can invent a mini-language tailored to the application domain that expresses exactly what users want:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// this is my example:
if userA.balance &amp;gt;= transferAmount {
 tx.transfer(balance, toUserB)
} else {
 print "Fill your balance"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;This language need not be executable. Initially, it could be simply a way of capturing the user's requirements, a specification..&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  #18 - Estimate to Avoid Surprises
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Quick! How long will it take to send War and Peace over a 56k modem line? How much disk space will you need for a million names and addresses? How long does a 1,000-byte block take to pass through a router? How many months will it take to deliver your project&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  #19 - Iterate the Schedule with the Code
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;So you complete the coding and testing of the initial functionality and mark this as the end of the first increment. Based on that experience, you can refine your initial guess on the &lt;br&gt;
number of iterations and what can be included in each. The refinement gets better and better each time, and confidence in the schedule grows along with it&lt;/em&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>advanced</category>
      <category>programming</category>
    </item>
    <item>
      <title>The pragmatic programmer book tips (Part 1)</title>
      <dc:creator>Wassim Ben Jdida</dc:creator>
      <pubDate>Wed, 22 Sep 2021 14:30:22 +0000</pubDate>
      <link>https://forem.com/wassimbj/the-pragmatic-programmer-book-tips-33hj</link>
      <guid>https://forem.com/wassimbj/the-pragmatic-programmer-book-tips-33hj</guid>
      <description>&lt;p&gt;3 weeks ago I bought The pragmatic programmer book, I saw it on a blog and the writer really recommended it, so I said why not.&lt;/p&gt;

&lt;p&gt;the book is full of great tips, actually each section the book talks about, ends with a tip its like a summary of the section.&lt;/p&gt;

&lt;p&gt;the book has about 70 tip, this is part 1. I will list them and explain some of the ones that are not clear, &lt;em&gt;the explanation is from the book itself.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  #1 - Care About Your Craft
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;There is no point in developing software unless you care about doing it well.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  #2 - Think! About Your Work
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;think about what you're doing while you're doing it. Never run on auto-pilot. Constantly be thinking, critiquing your work in real time.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  #3 - Provide Options, Don't Make Lame Excuses
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;don't have a backup, it's your fault. Telling your boss "the cat ate my source code" just won't cut it.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  #4 - Don't Live with Broken Windows
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Don't leave "broken windows" (bad designs, wrong decisions, or poor code) unrepaired. Fix  each one as soon as it is discovered. If there is insufficient time to fix it properly, then board it up. Perhaps you can comment out the offending code, or display a "Not Implemented" message, or substitute dummy data instead.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  #5 - Be a Catalyst for Change
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;You may be in a situation where you know exactly what needs doing and how to do it. The entire system just appears before your eyes you know it's right. But ask permission to tackle the whole thing and you'll be met with delays and blank stares. People find it easier to join an ongoing success. Show them a glimpse of the future and you'll get them to rally around.&lt;/em&gt;&lt;br&gt;
&lt;em&gt;"It's easier to ask forgiveness than it is to get permission."&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  #6 - Remember the Big Picture
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Keep an eye on the big picture. Constantly review what's happening around you, not just what you personally are doing.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  #7 - Make Quality a Requirements Issue
&lt;/h3&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  #8 - Invest Regularly in Your Knowledge Portfolio
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Learn at least one new language every year. Read a technical book each quarter. Read nontechnical books, too. Look for interesting courses. Participate in local user groups, Isolation can be deadly to your career. Experiment with different environments. Stay current. Surf the Web for papers, commercial sites, and any other sources of information you can find.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;It's important to continue investing. Once you feel comfortable with some new language or bit of technology, move on. Learn another one&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  #9 - Critically Analyze What You Read and Hear
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Just because a bookstore features a book prominently doesn't mean it's a good book, or even popular; they may have been paid to place it there&lt;/em&gt;&lt;br&gt;
 &lt;br&gt;
 &lt;br&gt;
 &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Part 2 will be ready soon&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>advanced</category>
      <category>programming</category>
    </item>
    <item>
      <title>Get better at reading open source code</title>
      <dc:creator>Wassim Ben Jdida</dc:creator>
      <pubDate>Sun, 25 Jul 2021 17:18:05 +0000</pubDate>
      <link>https://forem.com/wassimbj/how-to-get-better-at-reading-open-source-code-k5m</link>
      <guid>https://forem.com/wassimbj/how-to-get-better-at-reading-open-source-code-k5m</guid>
      <description>&lt;p&gt;This guide is from a personal experience and for me it was so practical, so if you have any other techniques please list them in the comment.&lt;/p&gt;

&lt;p&gt;First let me list the main techniques i use to be able to read an open-source project. then i will explain.&lt;/p&gt;

&lt;h2&gt;
  
  
  1- Techniques
&lt;/h2&gt;

&lt;p&gt;1- Understand what the project do&lt;br&gt;
2- Understand folder names (yep this is the most important one)&lt;br&gt;
3- Your skills in the project's tech stack&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Understand what the project do&lt;/em&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The first step is understanding what the project is about, why using it, what it's functionalities and what language it uses so you can read the code. this is easy for almost everybody. if you don't know what's the project is about you can't do anything.&lt;/p&gt;

&lt;p&gt;for example you want to contribute to kubernetes for someone who is reading now doesn't even know or didn't use kubernetes before and doesn't know how kubernetes is built, won't be able to do anything.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Understand folder names&lt;/em&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;after understanding what the project is about, you need to understand the project's folder name, this is really so important.&lt;br&gt;
i will give you some folder names and tell you what it may contain.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Folder&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;build,dist&lt;/td&gt;
&lt;td&gt;Compiled files&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;src,lib,&lt;/td&gt;
&lt;td&gt;main source code&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;test,spec&lt;/td&gt;
&lt;td&gt;automated tests&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;tools,utils,helpers&lt;/td&gt;
&lt;td&gt;Tools and utilities&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;static,public&lt;/td&gt;
&lt;td&gt;static files&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;...&lt;/td&gt;
&lt;td&gt;...&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href="https://github.com/golang-standards/project-layout"&gt;golang folder names and meaning&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;there is a plenty of names sometimes names are meaningful and sometimes it doesn't make sense so just search for name meaning or just ask the developer,  and sometimes the developer explains the folder names to make it easy for new contributors.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Your skills in the project's tech stack&lt;/em&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;last thing is your skills in the project's tech stack, for example you are experienced in ruby and react, but the project is using ruby and vue, you still can contribute but the other porject is using PHP and angular you won't be able to read the code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;after reading this, try to find an open-source project or some project that you are interested in and try to read it using these techniques.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Good luck !&lt;/p&gt;

</description>
      <category>github</category>
      <category>opensource</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Too quick, What hoisting really is ?</title>
      <dc:creator>Wassim Ben Jdida</dc:creator>
      <pubDate>Thu, 04 Feb 2021 12:48:51 +0000</pubDate>
      <link>https://forem.com/wassimbj/too-quick-what-hoisting-really-is-eof</link>
      <guid>https://forem.com/wassimbj/too-quick-what-hoisting-really-is-eof</guid>
      <description>&lt;p&gt;Simply, &lt;strong&gt;Hoisting&lt;/strong&gt; in javascript is a memory space that the javascript engine sets up before executing your code.&lt;/p&gt;

&lt;p&gt;it sets up a memory space for your variables and functions, that's what explain when you call your function and then actually create it, works.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// this will work fine and it will return 4&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// this will return "undefined" &lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if you rewrite this code in languages like python, php or go, it will throw an error, because the function is called before its defined and the variable too.&lt;/p&gt;

&lt;p&gt;but as I said before, the javascript engine creates a memory space for your functions and variables too and it sets the value of the variables as "undefined".&lt;/p&gt;

&lt;p&gt;so when the javascript engine start to execute your code it sees the &lt;code&gt;add()&lt;/code&gt; function, and it recognize it cause its already in its memory, so it execute it without any issues, and for the variable too, it sees the &lt;code&gt;x&lt;/code&gt; variable and it recognize it, but the value is set to "undefined" by default so it returns it.&lt;/p&gt;

&lt;p&gt;some articles say that the javascript engine moves the variables and functions to top, thats not it, javascript engine doesn't do that, it just remember them cause they are already in its memory.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>fancywords</category>
    </item>
    <item>
      <title>Understanding the fancy words in software industry.</title>
      <dc:creator>Wassim Ben Jdida</dc:creator>
      <pubDate>Thu, 07 Jan 2021 11:22:04 +0000</pubDate>
      <link>https://forem.com/wassimbj/understanding-the-fancy-words-in-software-industry-29ii</link>
      <guid>https://forem.com/wassimbj/understanding-the-fancy-words-in-software-industry-29ii</guid>
      <description>&lt;p&gt;Two days ago I was searching to understand some "fancy words" in the software industry, but it took me some time to find it and actually understand it.&lt;/p&gt;

&lt;p&gt;so I created &lt;a href="https://github.com/wassimbj/sfw"&gt;stupid fancy words GitHub repo&lt;/a&gt; to explain the words I daily find in simple and understandable way. and planning to add examples.&lt;/p&gt;

&lt;p&gt;I'm sharing this so you guys help me out in adding more and more words and also to people who find some "magic" words while they are learning something new. cause understanding them will help you so much in mastering that topic (language or whatever cool thing you are learning).&lt;/p&gt;

&lt;p&gt;Thanks for reading. 😄&lt;/p&gt;

</description>
      <category>software</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Understanding goroutines and concurrency in Golang</title>
      <dc:creator>Wassim Ben Jdida</dc:creator>
      <pubDate>Fri, 01 Jan 2021 13:43:05 +0000</pubDate>
      <link>https://forem.com/wassimbj/understanding-goroutines-and-concurrency-in-golang-216n</link>
      <guid>https://forem.com/wassimbj/understanding-goroutines-and-concurrency-in-golang-216n</guid>
      <description>&lt;p&gt;&lt;em&gt;this article assume that you have read about Golang, and know the basics at least.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So let me start with the definition of concurrency,&lt;br&gt;
it's simply the execution of some code instructions at the same time. that's it.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Goroutines&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;A goroutine is a lightweight thread of execution.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A Thread, or thread of execution, is a software term for the basic ordered sequence of instructions that can be passed through or processed by a single CPU core. - &lt;a href="https://stackoverflow.com/questions/5201852/what-is-a-thread-really"&gt;stackoverflow&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;so goroutine let us execute functions concurrently, how is that ?&lt;br&gt;
a simple 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;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;"time"&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;createUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&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;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" has been created successfully"&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;uploadUserImage&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="s"&gt;"User image has been successfully uploaded to the cloud !!"&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="k"&gt;go&lt;/span&gt; &lt;span class="n"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"wassim"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="n"&gt;uploadUserImage&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

  &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Second&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;User image has been successfully uploaded to the cloud &lt;span class="o"&gt;!!&lt;/span&gt;
wassim has been created successfully
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so as you can see the output of this simple program is not as the function order, cause they are executed concurrently so the result can be unordered.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;but wait a minute, why i put that time sleep at the end there ? is it necessary why is it necessary ?&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;so Go program is gonna finish executing when the main function ends, So, Our two function calls are running asynchronously in separate goroutines now. Wait for them to finish, but main function doesn't wait it doesn't know that the two functions are still running, so if we remove that sleep it won't print anything to the console. try it if you want.&lt;/p&gt;

&lt;p&gt;now lets go to channels.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Channels&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Channels are the pipes that connect concurrent goroutines. You can send values into channels from one goroutine and receive those values into another goroutine.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;lets start with code directly, here is an example using channels.&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;createUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&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;done&lt;/span&gt; &lt;span class="k"&gt;chan&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="kt"&gt;bool&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;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" has been created successfully"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;done&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="no"&gt;true&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;uploadUserImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;done&lt;/span&gt; &lt;span class="k"&gt;chan&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="kt"&gt;bool&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="s"&gt;"User image has been successfully uploaded to the cloud !!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;done&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="no"&gt;true&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;done&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;chan&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;go&lt;/span&gt; &lt;span class="n"&gt;uploadUserImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;done&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="s"&gt;"Image uploaded: "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="n"&gt;done&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="n"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"wassim"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;done&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="s"&gt;"User created: "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="n"&gt;done&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;So let me first tell you what is the difference between this &lt;code&gt;chan&amp;lt;-&lt;/code&gt; , this &lt;code&gt;&amp;lt;-chan&lt;/code&gt; and this &lt;code&gt;chan&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;"chan" =&amp;gt; in the function parameter, means that we will read and write from that channel, its a data type.&lt;br&gt;
"chan&amp;lt;-" =&amp;gt; means that we will write only&lt;br&gt;
"&amp;lt;-chan" =&amp;gt; means that we will read only&lt;/p&gt;

&lt;p&gt;the output of this progam:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;User image has been successfully uploaded to the cloud &lt;span class="o"&gt;!!&lt;/span&gt;
Image uploaded:  &lt;span class="nb"&gt;true
&lt;/span&gt;wassim has been created successfully
User created:  &lt;span class="nb"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so in the main function we created a channel named done, we passed that channel as a parameter to our workers (createUser, and upload), and whenver a worker is done we notify the main function.&lt;br&gt;
we put in the channel with &lt;code&gt;channelName &amp;lt;- VALUE&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and we consume the message from a channel like this &lt;code&gt;&amp;lt;-channelName&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;if we don't consume the message, the goroutine function is not gonna print anything cause main will not wait for it, so by using &lt;code&gt;&amp;lt;-done&lt;/code&gt; we are telling the main function to wait until we get a message from the worker.&lt;br&gt;
so if we removed this line here&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;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="s"&gt;"User created: "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="n"&gt;done&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we will get an output like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;User image has been successfully uploaded to the cloud &lt;span class="o"&gt;!!&lt;/span&gt;
Image uploaded:  &lt;span class="nb"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so &amp;lt;-done is simply blocking the main function from exiting. and therefore ending the program from executing.&lt;/p&gt;

&lt;p&gt;And this is what is called &lt;em&gt;Channel Synchronization&lt;/em&gt;, its just a fancy word that means blocking until a goroutine finish executing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But&lt;/strong&gt;, When waiting for multiple goroutines to finish, you may prefer to use a WaitGroup.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;WaitGroup&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;To wait for multiple goroutines to finish, we can use a wait group.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Again, lets jump directly to code, and then i will explain how and why.&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;"sync"&lt;/span&gt;
    &lt;span class="s"&gt;"time"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;wg&lt;/span&gt; &lt;span class="n"&gt;sync&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WaitGroup&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&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="k"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;wg&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Done&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;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" has been created successfully"&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;uploadUserImage&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;wg&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Done&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;             &lt;span class="c"&gt;// defer -&amp;gt; it will delay the execution of this func we upload the user image&lt;/span&gt;
    &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Second&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// just to say that this is an expensive function (that took 3s)&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="s"&gt;"User image has been successfully uploaded to the cloud !!"&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="k"&gt;go&lt;/span&gt; &lt;span class="n"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"wassim"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="n"&gt;uploadUserImage&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="n"&gt;wg&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Add&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="n"&gt;wg&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Wait&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;well, to use WaitGroup, we need to import the sync package, then we declared a global variable wg.&lt;/p&gt;

&lt;p&gt;now let me explain how the WaitGroup works. the &lt;code&gt;wg.Add(NUMBER_OF_GOROUTINES)&lt;/code&gt; this function increment the number of the running goroutines, the &lt;code&gt;wg.Done()&lt;/code&gt; will decrease the number of goroutines, so whenever a worker (function) is done working you fire the &lt;code&gt;wg.Done()&lt;/code&gt; function the &lt;code&gt;defer&lt;/code&gt; keyword just tells the function to delay until the main function ends its work &lt;strong&gt;(by main function i mean its parent wich is the upload and create function)&lt;/strong&gt;, lastly the &lt;code&gt;wg.Wait()&lt;/code&gt; this function blocks the program from exiting until the number of goroutines is 0. so &lt;code&gt;wg.Done()&lt;/code&gt; will decrease until we reach the 0 then the &lt;code&gt;wg.Wait()&lt;/code&gt; will fire, and the program exit/finish.&lt;/p&gt;




&lt;p&gt;Thanks for reading, this is my first post, so if there is anything !good let me know :)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gobyexample.com"&gt;More to learn about GO&lt;/a&gt;&lt;/p&gt;

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