<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Haithem on Medium]]></title>
        <description><![CDATA[Stories by Haithem on Medium]]></description>
        <link>https://medium.com/@haithemAb?source=rss-c3e8ce3afd19------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*5Am_Rx0OsFO8RN7BTDn5TQ.png</url>
            <title>Stories by Haithem on Medium</title>
            <link>https://medium.com/@haithemAb?source=rss-c3e8ce3afd19------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 13:14:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@haithemAb/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[One month of Go …]]></title>
            <link>https://medium.com/@haithemAb/one-month-of-go-b2ff7e960d21?source=rss-c3e8ce3afd19------2</link>
            <guid isPermaLink="false">https://medium.com/p/b2ff7e960d21</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[learning]]></category>
            <dc:creator><![CDATA[Haithem]]></dc:creator>
            <pubDate>Sun, 03 May 2020 20:26:53 GMT</pubDate>
            <atom:updated>2020-05-03T20:29:22.011Z</atom:updated>
            <content:encoded><![CDATA[<h3>One month of Go …</h3><p>One month ago, I decided to finally start learning Go after willing to do it for months. I spent few years doing professional work mostly using Python and I was wondering how challenging would it be to learn a new language with a kind of different Philosophy …</p><h4>Go philosophy</h4><p>If you are learning Go, I’m sure you heard about that. Probably you heard about it from the first few articles you read, the conferences you watched or friends &amp; colleagues that are already familiar with the language. It is actually one of the things that marked my experience.</p><p>The <strong>Go Philosophy</strong> that everyone is talking about, everyone is referencing or sometimes blaming, when you ask questions like <em>“why is it done this way”</em> ? <em>“why not that way”</em> ?<br>I’m not in a position to explain the Go design philosophy, It’s just <em>“one month of GO”</em>! but I share with you some of the main keywords that I managed to gather and think they are tightly linked to the language.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/881/1*xQgKFJJkkF35pc_e50lMaQ.jpeg" /><figcaption>Go Philosophy</figcaption></figure><h4>Go is Fast! Super fast!</h4><p>One of the main key reasons that made me think to learn Go is <strong>performance</strong>. Go is a fast performant programming language and the community agrees on that. Performance is one of the most important criteria when deciding which language to use for a task or an application. It helps you save money, deliver better, robust and scalable products and avoid nightmares when everyone else is enjoying the spikes of the traffic your company has during public holidays or peak seasons.</p><p>For me it was interesting to understand what is behind Go performance.<br>One of the common answers will be <em>“the Go philosophy”</em> but when we dig deeper we come up with those more technical factors :</p><ul><li>Go is compiled to machine code: In general compiled languages are faster than interpreted languages and this is because the way the machine will run the code. In the case of Go it is even better, since it is compiled to machine code which gives it a performance similar to C unlike java for example which is compiled to java bytecode.</li><li>Go uses Goroutines: Goroutines are cooperatively scheduled, have better startup time than threads and it is possible to run more of them than threads.</li></ul><p>Other factors contribute to Go performance like the memory management, small executable binaries, efficient storage of values, etc.</p><h4>Welcome back pointers</h4><p>Before doing Python, I used to write C code, but I never thought I will be thinking about C when learning Go. Actually I was expecting an Object Oriented programming style similar to Java or Python but I realized Go is way similar to C than any other language I know. The major syntactical difference between the two languages is the declaration backwards. <br>To understand what I mean, I tried to rewrite one of the <a href="https://tour.golang.org/moretypes/4">Go Tour</a> programs in C and I share the result with you :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*rW7P01xyX3x8Mc4IVhmosA.jpeg" /><figcaption>C code vs Go code</figcaption></figure><p>Similarly to C, Go uses <strong>pointers</strong> <em>(variable that holds memory address of a value)</em>. Pointers are required in multiple use cases and understanding them is crucial to learn Go. It brings back a lot of memories!!! However, unlike C, we can’t perform arithmetic operations on Go pointers.<br>Go also allows grouping a collection of fields into a <strong>struct</strong> and creating new types. While it is not possible to create class in Go, you still can create methods on types. Also Go introduced <strong>Interfaces</strong>, a key feature that enabled <em>“OOP”</em> like concepts such as compositions.</p><p>Finally, while in C developers need to handle memory manually, Go uses a <strong>Garbage Collector </strong>to avoid this overhead.</p><h4>Go project Structure</h4><p>When learning a new language, I’m usually curious to see how projects are structured. With Go that was not so easy. I saw projects in different structures, and sometimes they share few patterns. Multiple articles and slides were written and presented about this subject and they all agree that there are multiple ways to do it and evaluate it depending on the project itself, its size and target.</p><p>While layered architecture was very common among the go developers, two different designs are being widely highlighted in the community : Domain Driven Design &amp; Hexagonal Architecture.<br>Kat Zien’s <a href="https://www.youtube.com/watch?v=oL6JBUk6tj0">talk in GopherCon</a> explained with well elaborated examples different ways to structure projects and the advantages/disadvantages of each model.</p><p>This is a summary of the main directories you will see in most of the projects with minor differences depending on which design the project is following:</p><ul><li><strong>cmd:</strong> main.go of the different applications will be in this folder. Keep it small and minimized. Name of the folders will reflect the different binaries name you will build.</li><li><strong>pkg:</strong> the place to hold all packages intended to be used by other projects.</li><li><strong>internal: </strong>Packages that are internal to the application. Go makes sure that packages in this directory are protected from external imports.</li><li><strong>vendor: </strong>contains all application dependencies.</li><li><strong>mocks: </strong>in Go, it is a good practice to group all your tests’ mocks in one directory.</li></ul><p>You will be surprised by the number of other directories that you can use. For a larger list please refer to this <a href="https://github.com/golang-standards/project-layout">repository</a>. It describes well multiple folders and patterns and lists different projects that use it.</p><h4>Web Frameworks</h4><p>When I started with Go, I planned to build a small web application to apply what I have learned. And I was curious which famous frameworks for web development go has. My research made me come across another controversial situation. A lot of Go developers are against the idea of using Frameworks and support the usage of Go standards libraries along with a few small packages for specific use cases. <br>Personally, I’m not convinced with the idea of throwing away all the cool stuff the open source community prepared over the years and spend a considerable amount of time reinventing the wheel instead of focusing on the core logic of the application. I decided to give it a try and tried to understand the reasons behind it.</p><p>The first thing I noticed is that Go has great standard packages that made me feel I have an included micro framework to build a decent web application without the need for many external dependencies.<br>Check out the <a href="https://golang.org/pkg/net/http/">net/http</a> package, <a href="https://golang.org/pkg/database/sql/">database/sql</a> package and <a href="https://golang.org/pkg/text/template/">text/templat</a>e package as examples.<br>The other reason that I think makes people think twice before using a framework is that Go is being promoted to be a lightweight language and with frameworks you find yourself most of the time installing tons of packages but you probably need only two or three of them in the end.</p><p>All of this didn’t make Go poor in terms of available frameworks, a fairly long list exists with a huge variety of features and packages.</p><h4>Vendor nightmare</h4><p>When speaking about the project structure, we came across the vendor directory. It’s the directory that will contain a copy of all the external project dependencies. If you are using the Go Modules tool, it will take care of managing the vendor directory for you.<br>Use<strong> “<em>go mod vendor”</em></strong><em> </em>to initiate the vendor directory. You can also manage the vendor directory manually or by using other tools.</p><p>So far so good, but I realized that a large number of repositories written in Go actually have the folder pushed to the version control. For me, it is a nightmare to see a pull request with 100 edited files to find out later 90% of it is just vendor files, without mentioning the size of the repository growing exponentially fast.</p><p>Since Go doesn’t support any central server to host the different packages, It is not guaranteed that the packages your application depends on will not get removed, renamed or updated without you being ready for that, for this reason it is recommended sometimes to keep a copy of external dependencies. That’s not a rule though. For libraries, developers should not commit their dependencies.</p><h4>Confusing Loops &amp; Unusual flow control mechanisms</h4><p>In most of the programming languages, we are used to more than one looping type. Python syntax has <strong><em>“For”</em></strong> loops and <strong><em>“While”</em></strong> loops, C, C++ &amp; Java use extra<strong> <em>“Do … While”</em></strong> loop, but Go is limited to a <strong><em>“For”</em></strong> loop and depends on how you write it, it will act differently :</p><ul><li><strong>classic for loop:</strong> for i := 0; i &lt; 10; i++ {…}</li><li><strong>while loop:</strong> for i &lt; 1000 {…}</li><li><strong>while true:</strong> for {…}</li><li><strong>python for range like:</strong> for i, v := range lst {…}</li></ul><p>When it comes to flow control mechanisms, Go is not limited to the common ones <em>(if/else, switch, loops …)</em> but it supports three unusual statements and functions : defer, panic &amp; recover.</p><ul><li>defer: used mainly to execute a call when the parent function returns. It’s very useful when you are closing a database connection, a file or any other clean up actions that need to be performed when a function returns. I find it quite similar to Python’s <em>“finally”</em> statement or the with statement <em>“__exit__()”</em> method.</li><li>panic: To stop the ordinary flow.</li><li>recover: To regain control over the ordinary flow.</li></ul><p>I highly recommend this <a href="https://blog.golang.org/defer-panic-and-recover">article</a> to get more details with clear examples and scenarios.</p><h4>Go doesn’t have exceptions</h4><p>Another surprise from Go is that it doesn’t have exceptions. Instead, functions are expected to return an <strong>error</strong> type value which the developer is required to check and act accordingly. Ignoring those errors is highly not recommended and considered bad practice by the community.</p><pre><em>res, err := DoSomething()<br>if res != nil { <br>    log.Fatal(err)<br>}</em></pre><p>You will definitely encounter a snippet similar to this code many times while reading a Go code.</p><blockquote>In Go official FAQ <a href="https://golang.org/doc/faq#exceptions">page</a> the decision to not have exceptions is explained this way: <br><em>“We believe that coupling exceptions to a control structure, as in the try-catch-finally idiom, results in convoluted code. It also tends to encourage programmers to label too many ordinary errors, such as failing to open a file, as exceptional.“</em></blockquote><p>I recommend this <a href="https://medium.com/@hussachai/error-handling-in-go-a-quick-opinionated-guide-9199dd7c7f76">article</a> that explains different ways and best practices to handle errors.</p><h4>Unused Import &amp; Vars</h4><p>Python pep8 style guide covers most of the details on how to style your code, how to name things and how to handle imports and variables, it covers many details but you are not enforced by the language to follow it. Most programmers tend to use tools to do style checks before committing their code.</p><p>Go surprised me when I have found out that It is not possible to build code and test it when you have unused imports or unused variables. While definitely it’s good for production code, It can be annoying during the development process.<br>In specific situations, Go developers tend to import a package for it’s “<em>side effect”</em> but without using it directly in the code. In this case a blank identifier <em>“_”</em> is used to rename the package and avoid the compiler validation on it.</p><p>I was actually thinking it’s a typo when I first saw it!</p><h4>Naming Conventions</h4><p>Go also uses naming conventions for more than styling, and enforces few rules when it comes to how to name methods or types in case you want to export it to different packages. First letter of method, type or variable is required to be capitalized in order to be accessible from outside its own package.</p><p>When it comes to what go didn’t enforce, I have the feeling that the community didn’t agree yet on a standard. If you want to know more about naming conventions, the following <a href="https://medium.com/better-programming/naming-conventions-in-go-short-but-descriptive-1fa7c6d2f32a">article</a> covered it.</p><h4>Conclusion</h4><p>I tried to summarize my experience with Go so far focusing on the main details that brought my attention in comparison with what I’m used to with other languages. One month was not enough to go deep into all details but definitely the upcoming days will make me discover more.<br>I want to finish with a quote from the creator of C programming language <em>Denys Retchie</em>:</p><blockquote><em>“The only way to learn a new programming language is by writing programs in it.”</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b2ff7e960d21" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>