<?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: Ben</title>
    <description>The latest articles on DEV Community by Ben (@benzo).</description>
    <link>https://dev.to/benzo</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%2F101304%2F572d194a-2a05-4e75-8369-1a5e10a2a490.png</url>
      <title>DEV Community: Ben</title>
      <link>https://dev.to/benzo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/benzo"/>
    <language>en</language>
    <item>
      <title>Remote Onboarding as a Software Engineer</title>
      <dc:creator>Ben</dc:creator>
      <pubDate>Wed, 19 May 2021 00:00:00 +0000</pubDate>
      <link>https://dev.to/benzo/remote-onboarding-as-a-software-engineer-2h48</link>
      <guid>https://dev.to/benzo/remote-onboarding-as-a-software-engineer-2h48</guid>
      <description>&lt;p&gt;If you are a software developer and you have been working remotely during this pandemic, it might come as no surprise that due to recent studies &lt;a href="%5Bhttps://www.techrepublic.com/article/74-of-software-engineers-abroad-are-not-interested-in-moving-to-the-us-for-work/%5D(https://www.techrepublic.com/article/74-of-software-engineers-abroad-are-not-interested-in-moving-to-the-us-for-work/)"&gt;over 80% of software engineers want to continue working at least partly remote after the pandemic&lt;/a&gt;. While this opens up new possibilities in the way we live and work, it comes with certain difficulties. Not only do companies need to learn how to manage their remote workforce, but also employees need to find new ways to be successful at their job while wearing no pants. &lt;/p&gt;

&lt;h2&gt;
  
  
  Remote onboarding is difficult
&lt;/h2&gt;

&lt;p&gt;The most difficult part of working remotely is joining a new company. Companies themselves invest a lot of time and budget to work and improve on their onboarding process. If they manage to onboard their employees quickly, they will be more useful to them and their employees will be happy. But how can we as software engineers make the most of this phase? &lt;a href="https://twitter.com/chris_herd/status/1359135080753614854"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--znOnxU2s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1621418398/remote_onboarding_1_hu9kjg.png" alt="remote onboarding software engineer 1"&gt;&lt;/a&gt; As software engineers, we need to understand the problem we have to solve. Making sure we get this right is essential for our personal success. Moreover, in order to make a good job, we need to understand company dynamics, communication structures and find a way to socialize when there is no way to randomly meet people in the hallway. &lt;/p&gt;

&lt;h2&gt;
  
  
  Lay the groundwork
&lt;/h2&gt;

&lt;p&gt;When you start out at a new job, the one thing you want to get right is to understand what your job actually is. Most of the times, it varies quite a lot from what the job description says. Start out and figure out what the company mission is. From there, go a step deeper and figure out what your team or department is responsible for. Ultimately, &lt;strong&gt;make sure you know how you fit into the puzzle&lt;/strong&gt; and why you were hired. Reading through the company wiki is a good start to get a broader sense of what is out there, what topics are discussed and also how well people document. Aim for broad knowledge gathering in the beginning and focus on the details later. Try to understand as quickly as possible how the development process from a commit on a feature branch to the actual production deployment works, so you yourself can be productive quickly. &lt;/p&gt;

&lt;h2&gt;
  
  
  Be humble and patient
&lt;/h2&gt;

&lt;p&gt;Most of us are aware of &lt;a href="https://mikeschinkel.com/2004/developerbias/"&gt;developer bias&lt;/a&gt; for certain technologies, patterns and frameworks. However, we tend to fall into the same trap of believing our way of writing code is the way it should be done again and again. Even if you have years of experience, when you join a new team and work on a new code base stay humble and be patient. It is easy to criticize a project that evolved over years. Some things won't make sense on the first view, but on the second. Most of the times, &lt;strong&gt;there were good reasons why a team chose to use technology X or design a system a certain way&lt;/strong&gt;. Even if these reasons are not viable anymore at this current point in time, try to understand why choices were made in the past. Your coworkers will fell much more respected. &lt;/p&gt;

&lt;h2&gt;
  
  
  Ask
&lt;/h2&gt;

&lt;p&gt;Especially in the beginning of a new job, you will have a lot of questions and that is completely normal. While undisturbed, focused time is sacred to developers, you will need to ask questions to your co-workers at some point. Try to get an answer by yourself by looking at the wiki or other internal guides and documentation, but don't hesitate and waste too much time. People are usually friendly and helpful and getting quick answers will prevent bigger misunderstandings in the future.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/Wq8ydtDm8ectNjwT7H/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/Wq8ydtDm8ectNjwT7H/giphy.gif" alt="remote onboarding software engineer"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Communicate outside of your team
&lt;/h2&gt;

&lt;p&gt;Getting in touch with co-workers outside of your team is difficult. In a remote environment, you cannot randomly meet people in the hallway or kitchen. You need to get out of your comfort zone. Ask your employer if you could join one of the other teams to get to know them and their way of work. Their might even be something like an exchange program between teams in place. Look out for projects and opportunities to work together with other teams. Maybe you have done something in the past or experience in a topic another team is working on? Share your knowledge. Your coworkers will appreciate it and you will boost your impact. &lt;/p&gt;

&lt;h2&gt;
  
  
  1:1s
&lt;/h2&gt;

&lt;p&gt;While this also depends on your manager, making use of your personal 1:1s can help immensely with your remote onboarding experience as a software engineer. When you join a new company, you certainly have some expectations and goals in mind. Make the effort and write them down. Share them with your manager and look together if they align with the goals of your manager and the goals of the company. Try to learn what the goals of your manager are to align them with your goals. This way, you guarantee to work in the same direction and both of you share the same expectations. &lt;/p&gt;

&lt;p&gt;After all, we are all in the same boat. Do not forget that if you fit into the job quickly, this also a success for your manager and team. Be open with your expectations and your concerns and raise any issues quickly and your coworkers will most likely repay you.&lt;/p&gt;

</description>
      <category>career</category>
      <category>beginners</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Learnings From Three Years Into Software Development Without A Degree</title>
      <dc:creator>Ben</dc:creator>
      <pubDate>Tue, 23 Feb 2021 00:00:00 +0000</pubDate>
      <link>https://dev.to/benzo/learnings-from-three-years-into-software-development-without-a-degree-4ei1</link>
      <guid>https://dev.to/benzo/learnings-from-three-years-into-software-development-without-a-degree-4ei1</guid>
      <description>&lt;p&gt;Three and a half years ago, I got my first job as a software engineering intern at my current employer. At this point in time, I barely knew that Java was not only an island but also a programming language. I was coming from a complete different field of study, originally on my way to become a teacher in Germany. Today, I am leaving my employer, starting my second job as a software developer without a degree.&lt;/p&gt;

&lt;p&gt;These are my learnings and tips for everyone considering going this path (especially in Germany).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Spoiler: I won't advise you to drop out of college and get into tech like so many people advocate. If you are twenty something most likely you should finish your studies or at least join a bootcamp, if this is possible for you.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Find a sponsor
&lt;/h2&gt;

&lt;p&gt;No, not that kind of sponsor that gives you money and baseball caps. A sponsor is a person that believes in you and supports you. In Germany, getting into a company without a degree or anything to show off is near impossible. I was lucky enough to have a friend who offered me an internship position in his startup team, which was originally not even about software development. &lt;strong&gt;You will need someone who believes in you&lt;/strong&gt; and supports you to a) get into the job and b) pull you through when you start struggling and questioning yourself. To get off the ground you might want to consider doing some work for little or no money to show off your potential.&lt;/p&gt;

&lt;h2&gt;
  
  
  Work on your social skills and learn to adapt
&lt;/h2&gt;

&lt;p&gt;You can only do so much to learn and teach yourself about software development. In my experience, the best software developers were those which were also fit to &lt;strong&gt;work in a team&lt;/strong&gt;. You might be surprised, but if you support others and help them reach their goals, chances are high, they will pay you back with their help. There will be many questions and sometimes you simple might not know what search for in Google. Care for your peers and they will care for you.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/0MHgWp8xxjdd0TW0n6/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img alt="software development without a degree team" src="https://i.giphy.com/media/0MHgWp8xxjdd0TW0n6/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At some point, some of your peers will leave the company, you get a new boss or just a new team member. Every time one of those events happen, your personal situation changes with it. You will have a hard time, if you are not able to adapt to new situations, so learn to embrace change and watch out for new opportunities for yourself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Develop a constant learning habit
&lt;/h2&gt;

&lt;p&gt;If you take anything from this, it should probably be this. Software developers are knowledge workers. Every developer needs to learn new languages, frameworks or design patterns at some point. If you start with little to no knowledge in a team of peers that have some kind of education in software development, you will need to outrank them in your ability to learn something new. This seems hard (and it is), but if you manage to do it, it has the nice side effect that you will likely keep this habit throughout your career.&lt;/p&gt;

&lt;p&gt;Just to be clear: this does not mean to work 12+ hours a day, but to &lt;strong&gt;improve the way you learn and work&lt;/strong&gt;. There is a whole industry around productivity. Look out for ways to improve the quality of your work rather than the pure amount of time you work. Working 40 hours a week in front of a screen is exhausting. I have done the same mistake and I have seen many peers do it: working 60+ hours a week, because you just want to finish that feature or fix that bug. Get some sleep and take breaks and fixing that bug, which would have taken you four hours after a hard working day, might just be fixed in a couple of minutes the next morning.&lt;/p&gt;

&lt;h2&gt;
  
  
  Favour fundamentals over frameworks
&lt;/h2&gt;

&lt;p&gt;Especially at the beginning of your journey the amount of frameworks, languages and design patterns seems overwhelming and staggering. While you probably will need to learn some framework for your job, &lt;strong&gt;focus your studies on fundamental concepts of software development&lt;/strong&gt;. I was working for a company doing mainly backend web development, so I had to teach myself everything from HTTP over databases to a suitable language like Java. There are many great resources out there, but if you are trying to get into web development, I recommend taking a look at &lt;a href="%5Bhttps://github.com/kamranahmedse/developer-roadmap%5D(https://github.com/kamranahmedse/developer-roadmap)"&gt;these developer roadmaps&lt;/a&gt; or your welcome to &lt;a href="//mailto:contact@benbarten.com"&gt;reach out to me&lt;/a&gt;. You will have a much easier time following a structured learning path than I had blindly stumbling from one topic to another and hammering my peers with questions.&lt;/p&gt;

&lt;h2&gt;
  
  
  T-shaped skills
&lt;/h2&gt;

&lt;p&gt;As described above, you should pick a niche in software development. It is easy to jump from one broad topic to the other, because there is so much out there in surely a real developer knows everything about them, don't they?&lt;/p&gt;

&lt;p&gt;They don't.&lt;/p&gt;

&lt;p&gt;As developers we all specialise in something and this is important. If you are looking for jobs, you want to differentiate yourself from others and most companies have specific requirements for the people they hire. There is a well-known concept among developers, which is called the &lt;a href="%5Bhttps://medium.com/quick-code/what-it-is-a-t-shaped-developer-and-why-you-should-be-one-e87293e4bb84%5D(https://medium.com/quick-code/what-it-is-a-t-shaped-developer-and-why-you-should-be-one-e87293e4bb84)"&gt;T-shaped developer&lt;/a&gt;. It is based on the notion to specialise in one topic and then develop broader understanding of other topics around that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Take responsibility and get shit done
&lt;/h2&gt;

&lt;p&gt;Even though the mantra of developing a "get things done"-attitude seems kind of obsolete, it is not. If you take responsibility and commit to goals with your team lead, you do not only develop a better feeling to estimate the time of your work, you actually learn to "own" your work. There are only few things more rewarding than working on something that is actually yours. This can lift you through hard days when you may lack motivation and it will show your boss and peers that you actually care and they can rely on you.&lt;/p&gt;

&lt;p&gt;To make this happen you will also need to let some things go and &lt;strong&gt;learn to prioritise.&lt;/strong&gt; From my experience, there is no need for a super framework or tool to organise your work. I use very simple and effective methods:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Define long term goals for the next six months (you might want to look at &lt;a href="https://www.mindtools.com/pages/article/smart-goals.htm"&gt;SMART goals&lt;/a&gt;)).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Choose a simple tool like a todo list or &lt;a href="https://en.wikipedia.org/wiki/Kanban_(development)"&gt;KanBan tool&lt;/a&gt; to write down your tasks for every day and keep a backlog (I organise everything in &lt;a href="https://www.notion.so/Quick-Note-d9bb0c57a9c64eb891447a1662369b44"&gt;Notion&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Define your daily tasks on the evening before, starting with the most important to the least important.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Every morning, check if your tasks today are in line with your long term goals. Try to eliminate everything that is not.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Be consistent with your daily tasks but be flexible with goals, if new opportunities arise.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The hard part here is to really be consistent in prioritising the right tasks and then sticking to them. Defining my tasks on the evening before is probably the most valuable productivity hack. If you start planning your day in the morning, you are tempted to avoid difficult tasks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Promote yourself
&lt;/h2&gt;

&lt;p&gt;One thing many people feel uncomfortable with, including me, is promoting themselves. As a software developer without a degree you will need to do this at least to some extent. You do not need to be a Twitter influencer with 100k followers, but you should &lt;strong&gt;make sure to always have an up-to-date &lt;a href="https://www.linkedin.com/"&gt;LinkedIn&lt;/a&gt; profile&lt;/strong&gt; , a &lt;a href="https://github.com"&gt;GitHub&lt;/a&gt; profile with some projects that showcase your skills and a CV. I do like &lt;a href="https://twitter.com"&gt;Twitter&lt;/a&gt; as well, since you can easily connect with people from the tech industry, but it has not provided any value in terms of getting a new job or project so far.&lt;/p&gt;

&lt;p&gt;Setting up a small portfolio or blog like this one can also be beneficial. First, you have one platform that you own to link to all your social media accounts and promote yourself. Second, blogging about what you learn and do does not only help with promoting, but also with understanding more deeply what you write about. I am not super consistent with blogging so far, but every time I do, I am suprised by how much value I get out of it for myself. Third, building a blog can be a perfect sample project to showcase your engineering skills.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;As I mentioned in the beginning, I would not advise you to take this path, if you have the possibility, time and money to get a proper education in computer science. Having a degree and something that proves your skills will make your life much easier and less stressful. But if you do not have that for any reason, it can be done and the job as a software developer can be very rewarding one, not just financially.&lt;/p&gt;

&lt;p&gt;If you are considering a career in software development without a degree, I would love to know more about where you are at and I hope my learnings were helpful. Feel free to reach out to me &lt;a href="https://twitter.com/ben_barten"&gt;on Twitter&lt;/a&gt; or via &lt;a href="//mailto:contact@benbarten.com"&gt;mail&lt;/a&gt; and tell me your story :)&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>career</category>
      <category>programming</category>
    </item>
    <item>
      <title>API Usability Testing in 8 Simple Steps</title>
      <dc:creator>Ben</dc:creator>
      <pubDate>Fri, 05 Feb 2021 00:00:00 +0000</pubDate>
      <link>https://dev.to/benzo/api-usability-testing-in-8-simple-steps-4i5e</link>
      <guid>https://dev.to/benzo/api-usability-testing-in-8-simple-steps-4i5e</guid>
      <description>&lt;p&gt;If you are building an API product, developer experience should be at the top of your mind. Testing your APIs for usability is crucial to offer your customers the best experience possible. While there are many people who offer usability tests for user-facing products, API usability testing is a bit more tricky, since your customers are developers. In this blog post, I will provide you with a guide on how you can get started with API usability testing without burning through your valuable startup budget. We will look at when to test, who to test and &lt;strong&gt;eight simple steps&lt;/strong&gt; to organize your testing. &lt;/p&gt;

&lt;h3&gt;
  
  
  The Timing
&lt;/h3&gt;

&lt;p&gt;The best time to start usability testing is right at the beginning of your project. If you have not done that, the second best time is now. It is true. Do not fall into the trap of testing your APIs for usability shortly before a release. &lt;strong&gt;Make it part of your development process&lt;/strong&gt; early on and gather feedback as early as possible, so you can iterate on that feedback and change your product while it is easy to do so. If you are working in a somehow agile development process, which I will assume at this point, your development cycle will probably look something like this. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2-9JuxfW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1612963387/dev-cycle-without-testing_mhoaly.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2-9JuxfW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1612963387/dev-cycle-without-testing_mhoaly.png" alt="usability testing development cycle without testing"&gt;&lt;/a&gt; The best time to do your usability testing is after you have designed your product or in our case, the API. With modern tools like &lt;a href="https://swagger.io/"&gt;Swagger&lt;/a&gt; and the &lt;a href="https://www.openapis.org/"&gt;OpenAPI&lt;/a&gt; standard you can easily generate &lt;a href="https://swagger.io/solutions/mocking-and-virtualization/"&gt;Mock APIs&lt;/a&gt; which can then be used for usability testing with real users or your team. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--l1Jr0Y5A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1612963387/dev-cycle-with-testing_qxjpsp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--l1Jr0Y5A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1612963387/dev-cycle-with-testing_qxjpsp.png" alt="usability testing development cycle wit testing"&gt;&lt;/a&gt; If you do not have mock APIs and prefer to develop first, usability tests can also be done after the &lt;strong&gt;Development&lt;/strong&gt; stage. &lt;/p&gt;

&lt;h3&gt;
  
  
  The Users
&lt;/h3&gt;

&lt;p&gt;Developers come in different forms and shapes. They vary in their roles and skill sets, but also in the way they perceive content. If you can, try to determine which roles and skills your customers have and derive &lt;strong&gt;user personas&lt;/strong&gt; from it. This will help you to view your APIs from your customers perspective and find suitable candidates for a usability test. We will not go into detail on how to define user personas, but I recommend &lt;a href="https://www.youtube.com/watch?v=tCAeHfvsjoM"&gt;watching this short video&lt;/a&gt; by &lt;a href="http://pluralsight.com/"&gt;Pluralsight&lt;/a&gt; to get an idea. In reality, we often do not know the developers who will integrate our APIs when we start developing a new API. Therefore, it is helpful to assume a broad variety of different skill sets and find users of different backgrounds. As we do not want to hire an agency and spend as little money as possible on usability testing, there are mainly two easy options: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Freelance developer platforms&lt;/strong&gt;: 
&lt;a href="https://www.upwork.com/"&gt;Upwork&lt;/a&gt;, &lt;a href="https://www.fiverr.com/"&gt;fiverr&lt;/a&gt;, etc.. Look out for developers of various backgrounds and hire them for a 30-60 minute usability test. We will go into detail on how to do that in the next section. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Onboarding of new developers&lt;/strong&gt;:
When a new developer joins your team, he can be tasked to make a usability test of the API. This has the nice side effect that they get to know the product/API and a unique view of how users perceive it. &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  API Usability Testing in 8 Steps
&lt;/h3&gt;

&lt;p&gt;To learn as much as possible from our tests, we need a bit of preparation. These are eight simple steps, to get everything out of your usability tests: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Define an explicit task on what your tester is supposed to test. &lt;/li&gt;
&lt;li&gt;Give as little information as possible, but as much as needed to get started with the assigned task. &lt;/li&gt;
&lt;li&gt;Find a way to observe the user when using your API. Nowadays, where in-person meetings are less common, you can make use of screen sharing/recording tools. &lt;/li&gt;
&lt;li&gt;Make sure, the user speaks out loud while testing your product, explaining what he or she is thinking at each step. &lt;/li&gt;
&lt;li&gt;Provide a possibility for questions, if the user gets stuck at a certain point of the process and does not find any other option than asking for help. &lt;/li&gt;
&lt;li&gt;Create a detailed report on the observations during the test. &lt;/li&gt;
&lt;li&gt;Derive learnings and tasks from your observations. &lt;/li&gt;
&lt;li&gt;Share your learnings and observations with your team.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;One other way to get started with usability testing is to do a Hackathon based on your API. This has the positive side effect that you do not only learn about the usability but also potential use cases of your API. &lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;API usability testing does not have to be an expensive and tedious task. Getting started with it will immediately improve your understanding of your own product and how users perceive it. You will be astonished by how much your personal conceptions may differ from a user's fresh view on your API and your customers will thank you for providing the best developer experience. Since, API products are still quite new, there is not much content around usability testing for APIs. If you want to share your learnings and discuss, feel free to reach out to me &lt;a href="https://twitter.com/ben_barten"&gt;on Twitter&lt;/a&gt; or &lt;a href="https://conways-newsletter.mailchimpsites.com/"&gt;subscribe to my Newsletter&lt;/a&gt; if you want to read more on this topic.&lt;/p&gt;

</description>
      <category>api</category>
      <category>ux</category>
    </item>
    <item>
      <title>Exposing microservices with an API gateway and the API composition pattern</title>
      <dc:creator>Ben</dc:creator>
      <pubDate>Fri, 17 Jul 2020 10:57:28 +0000</pubDate>
      <link>https://dev.to/benzo/exposing-microservices-with-an-api-gateway-and-the-api-composition-pattern-54i4</link>
      <guid>https://dev.to/benzo/exposing-microservices-with-an-api-gateway-and-the-api-composition-pattern-54i4</guid>
      <description>&lt;p&gt;Microservices are a widely spread pattern in modern software architectures. Tools like &lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt;, &lt;a href="https://kubernetes.io/"&gt;Kubernetes&lt;/a&gt; and modern CI/CD pipelines have enabled software teams to build highly scalable distributed systems in which each microservice can be operated by a small team. In fact, the implicit notion of microservices is to be self-contained and focused on a single purpose. Thus, implemented correctly, microservices enable your system to be highly flexible and your teams to operate independently. While this is a major upside for the development process itself, it comes with certain challenges when you want to expose your system to external users.&lt;/p&gt;

&lt;p&gt;Check out this &lt;a href="https://www.nginx.com/blog/introduction-to-microservices/"&gt;well-written blog series of NGINX&lt;/a&gt;, if you want to get deeper into microservices architecture.&lt;/p&gt;

&lt;h3&gt;
  
  
  Flexibility leads to complexity
&lt;/h3&gt;

&lt;p&gt;Don't get me wrong. The microservices pattern takes away a lot of complexity introduced by highly cohesive and monolithic systems, but it introduces a new kind of complexity. The fact that teams are flexible in the way they build their microservices - even if you have certain guidelines in place - makes it more difficult for your clients to access your system. They possibly need to &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;talk with all the different services&lt;/li&gt;
&lt;li&gt;understand which service provides the data they need&lt;/li&gt;
&lt;li&gt;communicate via different protocols.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In terms of user experience, this is not optimal and might lead to frustration on the client side. The &lt;strong&gt;complexity increases the learning curve of your system&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SIs13Ase--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1594981123/api_composition_1_seenaj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SIs13Ase--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1594981123/api_composition_1_seenaj.png" alt="microservices"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;A simplified overview of clients A,B and C which require different data from individual services X,Y, and Z of a microservice architecture. All are accessible via different protocols: REST, GraphQL and gRPC.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The picture above illustrates such a microservice architecture. Even if your microservices do not use different communication protocols such as in this example: Talking to multiple hosts might still require multiple ways of authentication, understanding different styles of API design, etc..&lt;/p&gt;

&lt;h2&gt;
  
  
  An API gateway as the single entry point
&lt;/h2&gt;

&lt;p&gt;An API gateway might help you with this complexity. As the term "gateway" implies, the main purpose of an API gateway is to be the single entry point for your microservice architecture. It is build on top of your individual services and routes requests of clients to these services. It does &lt;strong&gt;not contain or know of any business or domain logic&lt;/strong&gt; of the individual services.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oApJAJyW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1594981123/api_composition_2_quvwgl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oApJAJyW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1594981123/api_composition_2_quvwgl.png" alt="api-gateway"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;An API gateway sitting between your microservices and the clients who want to access these services. The API gateway provides a unified interface to the clients and hides implementation details of the services X, Y, and Z.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Upsides of API gateways
&lt;/h3&gt;

&lt;p&gt;As the illustration shows, an API gateway makes it possible to easily hide implementation details of our microservice architecture and unify the external interface. This enables clients to have a single point of entry to your system, thus, making it more accessible. Besides these UX improvements, the API gateway can be used to centralize and handle topics such as&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;authentication&lt;/li&gt;
&lt;li&gt;permission handling&lt;/li&gt;
&lt;li&gt;API security&lt;/li&gt;
&lt;li&gt;policies (rate limits, allow-listing, etc.)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This &lt;strong&gt;enables the individual microservice teams to concentrate on the core business/domain logic&lt;/strong&gt; of their service and improves development speed. The API gateway might further support your system by providing extensive (access) logging and collecting performance as well as business related metrics of your clients.&lt;/p&gt;

&lt;h3&gt;
  
  
  Downsides of API gateways
&lt;/h3&gt;

&lt;p&gt;Introducing a new part in your architecture always comes with certain challenges and so does an API gateway. The API gateway is another piece of software in our system that has to be developed, deployed and maintained. While this was also one of the upsides listed above, a single point of entry to our application might also become a &lt;strong&gt;bottleneck&lt;/strong&gt; in two different ways. &lt;/p&gt;

&lt;p&gt;First, it has to be highly scalable. Since it is another layer between every request of the client and our microservices, it needs to have as little impact on the request latency as possible. Second, it can slow down development, since every microservice has to be exposed via the gateway.&lt;/p&gt;

&lt;h2&gt;
  
  
  The API composition pattern
&lt;/h2&gt;

&lt;p&gt;So far, we have introduced the API gateway simply to route each request of a client to a microservice and back. With microservices being self-contained and the owner of their domain and data models, they often fail the expectations of clients on how to consume the data. Clients might want to query data of multiple sub-domains at the same time or just a small portion of the data a single microservice offers. To fit these needs, the API composition pattern can be applied. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FjbOK2w4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1594981123/api_composition_3_rewopg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FjbOK2w4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1594981123/api_composition_3_rewopg.png" alt="api-composition"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Overview of an API gateway with API composition. Composition 1 composes data of service X and Y to a single resource, while Composition 2 composes data of service Y and Z to a single resource.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Even though the API composition now manipulates the payloads of one or multiple microservice(s), an API composition still remains free of any domain or business logic. Each composition could also be defined as an aggregate of the microservices behind the API gateway to &lt;strong&gt;fit the clients need&lt;/strong&gt; and improve the developer experience.&lt;br&gt;
This pattern introduces some more advantages. With API composition in place, we suddenly can &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;make up for bad design decisions and apply a new shiny interface&lt;/li&gt;
&lt;li&gt;hide legacy systems and replace these whenever we have the time for it&lt;/li&gt;
&lt;li&gt;enable specialists for API design and developer experience to create a consistent design of the overall API.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On the other hand, as with the API gateway itself, we introduce another layer in our overall system. Development teams have to be careful that API composition does not evolve to a replacement for good API design of the individual microservices or mixing up the domain logic and models within the API gateway.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;API gateways and API composition are a useful tool to reduce complexity of microservice architectures.  They can help with handling access and monitoring of the overall system, as well as enabling clients to request data as they expect it. Nevertheless, one has to be careful when introducing a new layer to a system, since it inevitably leads to more dependencies in the development chain.&lt;/p&gt;

&lt;p&gt;What do you think of this approach? Have you faced similar challenges? If yes, how did you solve them? &lt;br&gt;
If you want to talk more on the topic of APIs and microservices, feel free to contact me on &lt;a href="%5Bhttps://twitter.com/ben_barten%5D(https://twitter.com/ben_barten)"&gt;Twitter&lt;/a&gt; or via &lt;a href="//mailto:contact@benbarten.com"&gt;e-mail&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>microservices</category>
      <category>api</category>
      <category>architecture</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Docker Hands-On Start for Developers - Part I</title>
      <dc:creator>Ben</dc:creator>
      <pubDate>Mon, 20 Apr 2020 09:52:38 +0000</pubDate>
      <link>https://dev.to/benzo/docker-hands-on-start-for-developers-part-i-jfl</link>
      <guid>https://dev.to/benzo/docker-hands-on-start-for-developers-part-i-jfl</guid>
      <description>&lt;p&gt;If you are working as a software developer in 2020, you probably have heard about &lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt;. In fact, by now over 20% of all hosts are running &lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt; with increasing numbers for the upcoming years.&lt;/p&gt;

&lt;p&gt;If you have no experience and you want to get started with this technology, you have come to the right place. You will get a succinct overview of the topic and some best practices on how to leverage &lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt; for your own projects or your projects at work. In this part, we will learn a short definition of the technology and we will dive into more detail on the basic elements of &lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt;: images and containers. To get some immediate hands-on experience, we will build our own docker image and spin up containers from it. &lt;/p&gt;

&lt;h3&gt;
  
  
  Contents
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;What Docker is and why you want to use it (Part I)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Images (Part I)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Containerizing you application (Part I)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Containers (Part I)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Best Practices (Part II)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What Docker is and why you want to use it
&lt;/h3&gt;

&lt;p&gt;You probably know about the concept of virtual machines (VMs). VMs are simulating operating systems with system-specific pre-installed software. They are managed by a hypervisor (virtual machine monitor) which manages and shares hardware for multiple VMs on a single physical machine. VMs need a big amount of memory and computing power and therefore are expensive to setup and maintain. Furthermore, moving VM workloads between hypervisors and cloud platforms is not as easy and intuitive as it could be. &lt;/p&gt;

&lt;p&gt;In contrary, &lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt; or other containerization technologies are running on the host operating system (OS). Containers contain your application code, dependencies, your runtime system and system libraries, settings and tools. That is it. Nothing more than the bare minimum of what you need to run your application. They do not need a full-blown OS to setup, instead they free up a lot of CPU, RAM and storage for running applications. As long as a host has the &lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt; Engine running, you can easily port &lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt; containers from one host to another. You will never have to worry, what kind of server your application is running on or how to ship between hypervisors. You simply pick or create your own docker image and focus on developing your software.&lt;/p&gt;

&lt;p&gt;This image visualisation is copied from the &lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt; website and illustrates the difference:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JhIfBh9u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1587375536/docker_developers_part_i_1_sbxavm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JhIfBh9u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1587375536/docker_developers_part_i_1_sbxavm.png" alt="pic1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Source: &lt;a href="https://www.docker.com/resources/what-container#/package_software"&gt;https://www.docker.com/resources/what-container#/package_software&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Images
&lt;/h3&gt;

&lt;p&gt;As a developer, you should be familiar with the concept of object-oriented programming. In a way, you can think of images as classes and containers as objects. Images are your templates to build containers on.&lt;/p&gt;

&lt;p&gt;To start out, you usually pull an image from a docker registry like &lt;a href="https://hub.docker.com/"&gt;Docker Hub&lt;/a&gt;. Images are made of one or multiple stacked read-only layers. The only writable layer is build by the container as the illustration below shows. We will discuss what exactly a layer is in a minute.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CGNNKrrn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1587375536/docker_developers_part_i_2_ckemfl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CGNNKrrn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1587375536/docker_developers_part_i_2_ckemfl.png" alt="pic2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Source: &lt;a href="https://docs.docker.com/v17.09/engine/userguide/storagedriver/imagesandcontainers/#container-and-layers"&gt;https://docs.docker.com/v17.09/engine/userguide/storagedriver/imagesandcontainers/#container-and-layers&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Getting started
&lt;/h5&gt;

&lt;p&gt;So, to dive deeper, let’s start and play around with &lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt;. If you have not done so already, go &lt;a href="https://docs.docker.com/install/"&gt;here&lt;/a&gt; and install the &lt;a href="https://docs.docker.com/engine/"&gt;Docker Engine&lt;/a&gt; on your host. When you’re done, open your command line tool and type:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker pull node:latest&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Congratulations! You have pulled your first &lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt; image. Let’s look at what happened here:&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;docker pull&lt;/code&gt; command initiates a call to a Docker registry (the default is &lt;a href="https://hub.docker.com/"&gt;Docker Hub&lt;/a&gt;) to pull an image with the name &lt;em&gt;node&lt;/em&gt; and the tag &lt;em&gt;latest&lt;/em&gt;. A &lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt; registry serves as a storage server for docker images. From the command above, we can extract the following syntax: &lt;code&gt;docker pull &amp;lt;name&amp;gt;:&amp;lt;tag&amp;gt;&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;name&lt;/code&gt; specifies the name of the image and the &lt;code&gt;tag&lt;/code&gt; specifies the version of a particular image. (Attention: Image tags are mutable. To make sure, you can rely on an image version, you might want to consider &lt;a href="https://success.docker.com/article/images-tagging-vs-digests"&gt;image digests&lt;/a&gt;). &lt;/p&gt;

&lt;p&gt;In this case, we pulled an image with a pre-installed version of Node.js. Your output should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hFywIelM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1587375535/docker_developers_part_i_3_cvryku.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hFywIelM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1587375535/docker_developers_part_i_3_cvryku.png" alt="pic3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Working with images
&lt;/h5&gt;

&lt;p&gt;Let’s have a closer look at the image we just pulled. Execute the following command in your terminal:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker images&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will return a list of all your images like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wvO8kK5v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1587375531/docker_developers_part_i_4_smindc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wvO8kK5v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1587375531/docker_developers_part_i_4_smindc.png" alt="pic4"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see the name of the image, the tag we specified before, a unique image id, a timestamp when it was created and the size of the image. To further inspect the image execute:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker inspect node:latest&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here you can see a lot of metadata for the image like the size of the image, the different layers, the underlying OS and much more. We will come back to the &lt;code&gt;inspect&lt;/code&gt; command in the the &lt;em&gt;Best Practices&lt;/em&gt; section.&lt;/p&gt;

&lt;p&gt;Now, let's remove the image with:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker rmi node:latest&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Cpo31DEs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1587375538/docker_developers_part_i_5_inm0js.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Cpo31DEs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1587375538/docker_developers_part_i_5_inm0js.png" alt="pic5"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This way, we removed the image and the respective layers of the image from our Docker host.&lt;/p&gt;

&lt;p&gt;Note that all these commands can be executed with the image id, but they will only work, as long as no container based on the image is running. To understand how images are built and why they are structured into layers, we will build a simple Node.js server, containerize it and spin up our first containers from it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Containerizing your application
&lt;/h3&gt;

&lt;p&gt;To keep things simple, I set up a simple Node.js server. You can find the source code &lt;a href="https://github.com/bbenzo/docker-for-devs-1"&gt;here&lt;/a&gt;. For this example, you do not need any experience with Node.js. You simply need to have &lt;code&gt;node&lt;/code&gt; and the node package manager &lt;code&gt;npm&lt;/code&gt; installed and follow along the tutorial. You can install both as described &lt;a href="https://www.npmjs.com/get-npm"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Clone the repository, &lt;code&gt;cd&lt;/code&gt; into the repository’s folder and execute the following commands to build the application and install all dependencies.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install &amp;amp;&amp;amp; npm run build&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, let’s run the server with&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm start&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, our server should be running. If you navigate your browser to &lt;code&gt;http://localhost:8080&lt;/code&gt;, you should see a "Hello World of Docker" message.&lt;/p&gt;

&lt;p&gt;Great! Now, let’s see how we can make an image of this application and spin up a container of it. Remember, our image is our template for all our containers and it should do only one thing: run our application.&lt;/p&gt;

&lt;p&gt;In the project repository, create a new file and name it &lt;code&gt;Dockerfile&lt;/code&gt;. In there, copy the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; node:latest&lt;/span&gt;


&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; app&lt;/span&gt;


&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; . .&lt;/span&gt;


&lt;span class="k"&gt;RUN &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; npm run build


&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="s"&gt; 8080&lt;/span&gt;


&lt;span class="k"&gt;ENTRYPOINT&lt;/span&gt;&lt;span class="s"&gt; ["npm", "start"]  &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To build your image, execute &lt;code&gt;docker build -t my-first-docker .&lt;/code&gt; in the same directory as your &lt;code&gt;Dockerfile&lt;/code&gt;. You should see the different steps building. When we execute &lt;code&gt;docker images&lt;/code&gt;, we can see our newly created image:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QGtE15pJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1587375533/docker_developers_part_i_6_s3ikrb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QGtE15pJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1587375533/docker_developers_part_i_6_s3ikrb.png" alt="pic6"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are two kind of instructions in a &lt;code&gt;Dockerfile&lt;/code&gt;. Those, which add data and therefore create a new layer and those which simply add metadata to the image. I will walk you through those steps and explain in detail what they are doing.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;FROM&lt;/code&gt; command specifies a base image, on which we want to build our image. In our case, we use the latest image from the node repository. This image has Node.js pre-installed since we want to build a Node.js server. In general, it is recommended to use base images from official repositories since you do not have to worry about installing Node.js properly. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;WORKDIR&lt;/code&gt; creates a working directory for your application. It is seen as best practice, because we avoid unreadable syntax like &lt;code&gt;cd .. &amp;amp;&amp;amp; do foo&lt;/code&gt;. The working directory will be your default directory in which the following RUN, COPY, etc. commands will be executed. Our working directory will be &lt;code&gt;app&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;COPY&lt;/code&gt; copies files from your project directory into the &lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt; image. For now, we will just copy all files in our working directory, even though this is not considered best practice. Notice: this is the first time we are adding data to our image. Thus, this instruction creates a new layer in your image. Each instruction of this type creates its own layer.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;RUN&lt;/code&gt; executes a command in the default shell of the image. Since it adds data, it creates another layer. In our case, we add data by installing all dependencies with &lt;code&gt;npm install&lt;/code&gt; and building the compiled files with &lt;code&gt;npm run build&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;EXPOSE&lt;/code&gt; exposes a specified network port for our container.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ENTRYPOINT&lt;/code&gt; sets a default shell command, which will we be executed when we start a container and run it. This command makes the &lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt; image an executable when run. We could also use the &lt;code&gt;CMD&lt;/code&gt; instruction to achieve this behaviour. Nevertheless, it is recommended to set &lt;code&gt;ENTRYPOINT&lt;/code&gt; whenever you want your users to use this image in a specific way. If you want to give your users more flexibility, choose &lt;code&gt;CMD&lt;/code&gt;, because it can be overwritten more easily.&lt;/p&gt;

&lt;h3&gt;
  
  
  Containers
&lt;/h3&gt;

&lt;p&gt;As explained, containers are the runtime counterpart to images. Containers accept an image and a command as arguments. The image is used to run the container and the command specifies the application, the container should run. Let’s use our example to elaborate on this. Run:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker run -it my-first-docker&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NQA7_JjV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1587375536/docker_developers_part_i_7_hbjfsj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NQA7_JjV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1587375536/docker_developers_part_i_7_hbjfsj.png" alt="pic7"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And just like that, you have your first docker container running! The &lt;code&gt;run&lt;/code&gt; command starts a container. You pass an image as an argument and with the &lt;code&gt;-it&lt;/code&gt; flag you will attach your shell to the container’s shell. To exit the container, you can simply press CRTL-C on Linux/Win or CMD-C on MacOS.&lt;/p&gt;

&lt;p&gt;Now, we do not want to attach our shell every time we build a &lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt; container. In these cases, you can add the &lt;code&gt;-d&lt;/code&gt; flag to run a daemon of the container. We can try it like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker run -d -p 8080:8080 my-first-docker&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Your docker container is now started in the background. At the same time, we mapped the exposed port 8080 of the container to our local port 8080.  Run:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker ps&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Rp-O8ZYb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1587375533/docker_developers_part_i_8_c0r7hr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Rp-O8ZYb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1587375533/docker_developers_part_i_8_c0r7hr.png" alt="pic8"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, our docker container is running. &lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt; assigns a unique &lt;code&gt;CONTAINER ID&lt;/code&gt; to every container. We also can see the command, which is executed as specified in our &lt;code&gt;ENTRYPOINT&lt;/code&gt; command in the image, the mapped ports and a randomly assigned name for the container (can be specified manually with the &lt;code&gt;—name&lt;/code&gt; flag). If you move back to your browser and &lt;a href="http://localhost:8080/"&gt;http://localhost:8080&lt;/a&gt;, you should see your “Welcome to Docker World” message again, since all traffic on our local port 8080 is routed to our running docker container.&lt;/p&gt;

&lt;p&gt;To stop the container, simply use:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker stop &amp;lt;CONTAINER ID&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now that our container is stopped, we can see the running and stopped containers with:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker ps -a&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WtWe72Xk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1587375534/docker_developers_part_i_9_zr7fhj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WtWe72Xk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1587375534/docker_developers_part_i_9_zr7fhj.png" alt="pic9"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see the container still exists. To remove our container completely, we can use a final:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker rm &amp;lt;CONTAINER ID&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;For now, this should be everything to get started with &lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt; and experiment a bit on your own. We learned, why &lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt; is such a great alternative to VMs, what &lt;strong&gt;images&lt;/strong&gt; and &lt;strong&gt;containers&lt;/strong&gt;  are, how we can &lt;strong&gt;containerize&lt;/strong&gt; our own app and how to interact with our running containers. Hopefully, this has helped you to dive into this amazing technology.&lt;/p&gt;

&lt;p&gt;In the next part, we will have a look at some general best practices when working with &lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt;, especially on how to build your images and set up your Dockerfiles.&lt;/p&gt;

&lt;p&gt;Stay tuned!&lt;/p&gt;

</description>
      <category>docker</category>
      <category>todayilearned</category>
      <category>beginners</category>
      <category>node</category>
    </item>
    <item>
      <title>Explore Arrays &amp; Slices in Go</title>
      <dc:creator>Ben</dc:creator>
      <pubDate>Tue, 31 Mar 2020 15:59:57 +0000</pubDate>
      <link>https://dev.to/benzo/explore-arrays-slices-in-go-n6n</link>
      <guid>https://dev.to/benzo/explore-arrays-slices-in-go-n6n</guid>
      <description>&lt;p&gt;There are many ways to organize sequential data in the programming world. The most common ones are the abstract data types &lt;strong&gt;array&lt;/strong&gt; and &lt;strong&gt;list&lt;/strong&gt;. They are at the beginning of every programmer's learning journey. With arrays and lists we can implement our first for loops, we can store those notes for the note taking app we are building, or implement our own buffer.&lt;/p&gt;

&lt;p&gt;In this post, we will have a look at how these data types are implemented in Go. After defining what exactly those types are, we will inspect &lt;strong&gt;static arrays&lt;/strong&gt; and why we have &lt;strong&gt;slices&lt;/strong&gt; instead of &lt;strong&gt;lists&lt;/strong&gt; in Go. Finally we will look at the complexity of working with these types to get a better grasp on when to use these data types.&lt;/p&gt;

&lt;h3&gt;
  
  
  Static Arrays
&lt;/h3&gt;

&lt;p&gt;Static arrays are at the core of every programming language. They have a fixed length of &lt;em&gt;n&lt;/em&gt;, which has to be defined upfront. In memory, all elements of the array are stored adjacently. If you want to iterate over an array, this can be achieved via an index starting at 0 for the first element and ending at &lt;em&gt;n - 1&lt;/em&gt; for the last. In Go, arrays and slices are both evaluated during runtime. Furthermore, the length of a slice is also part of the array type, so &lt;code&gt;int[10]&lt;/code&gt; and &lt;code&gt;int[20]&lt;/code&gt; are different types.&lt;/p&gt;

&lt;h4&gt;
  
  
  Declaration and initialization
&lt;/h4&gt;

&lt;p&gt;There are different ways to declare and initialize an array. We can first declare it and set its values explicitly.&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;var&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;
&lt;span class="n"&gt;array&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="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"all"&lt;/span&gt;
&lt;span class="n"&gt;array&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="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"the"&lt;/span&gt;
&lt;span class="n"&gt;array&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="s"&gt;"elements"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or we declare and initialize it with one line.&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;var&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;int32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="m"&gt;23&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;12&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;8&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Accessing elements
&lt;/h4&gt;

&lt;p&gt;Elements are accessed by their index. The following statement gives us the second element of 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;var&lt;/span&gt; &lt;span class="n"&gt;secondElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Specifying an index lower than 0 or greater than &lt;em&gt;n - 1&lt;/em&gt; results in an &lt;strong&gt;index out of bound&lt;/strong&gt; error. In Go, specifying an index out of range will not even compile.&lt;/p&gt;

&lt;h4&gt;
  
  
  Searching elements
&lt;/h4&gt;

&lt;p&gt;Searching elements can be done via iteration and a simple &lt;code&gt;if&lt;/code&gt; condition.&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;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"foo"&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;"Found foo"&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;h4&gt;
  
  
  Time complexity
&lt;/h4&gt;

&lt;p&gt;Since static arrays are fixed in size, our options to work with them are limited to only accessing and searching for data.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Action&lt;/th&gt;
&lt;th&gt;Complexity&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Accessing elements&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Searching elements&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Slices
&lt;/h3&gt;

&lt;p&gt;With slices things get interesting. The ability to grow and shrink this &lt;em&gt;dynamic-array-like&lt;/em&gt; data structure in size gives us a new set of possibilities to work with. Before inspecting how they are used, let's have a closer look at slices and how they are special compared to similiar data structures of different languages.&lt;/p&gt;

&lt;p&gt;Compared to other languages, creating a slice from an array is basically &lt;strong&gt;just a struct with a pointer to the array&lt;/strong&gt; and two variables holding length and capacity of the array. It does not allocate any new memory. As a consequence, basic operations on slices are very cheap. Creating, moving on or expanding the respective array (until the capacity is reached) can all be done without any new memory allocation. This leaves the user with the indicated &lt;em&gt;dynamic-array-like&lt;/em&gt; feeling when working with slices. &lt;/p&gt;

&lt;h4&gt;
  
  
  Declaration and initialization
&lt;/h4&gt;

&lt;p&gt;Here, we also have different ways to declare and initialize an array. This time we start with the declaration and initialization combined. This will create a slice with an underlying array of size and capacity 4.&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;var&lt;/span&gt; &lt;span class="n"&gt;slice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="m"&gt;23&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;12&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;8&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is fairly convenient, but may not be the most efficient way. This way, when adding an element via the &lt;code&gt;append()&lt;/code&gt;function, a new array has to be allocated in memory. On the other hand, Go gives us the tools to handle the allocation ourselves.&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;var&lt;/span&gt; &lt;span class="n"&gt;slice&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="kt"&gt;int&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;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;slice&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="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;6&lt;/span&gt;
&lt;span class="n"&gt;slice&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="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;12&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This &lt;code&gt;make&lt;/code&gt; function creates a new slice with an underlying array of length 2 and capacity 3.&lt;/p&gt;

&lt;h4&gt;
  
  
  Appending elements
&lt;/h4&gt;

&lt;p&gt;When we add one more element to the slice above, no new memory is allocated, because of the big enough capacity, which improves our efficiency. Another element more and a new array hast to be created.&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;slice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;7&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// still working on the same array&lt;/span&gt;
&lt;span class="n"&gt;slice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;14&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// allocates new memory, since we expanded the capacity&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can easily test this behaviour on your own. If you want to know more about what exactly happens when appending new elements in Go, I recommend &lt;a href="https://blog.golang.org/go-slices-usage-and-internals"&gt;this article from The Go Blog&lt;/a&gt;. &lt;/p&gt;

&lt;h4&gt;
  
  
  Slicing a slice
&lt;/h4&gt;

&lt;p&gt;We can create a "sub-slice" of a slice by specifying the upper and lower index like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;subSlice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that this does not create a new slice. We are still operating on the same data in memory. You should be aware that this is behaviour might lead to the fact that your program holds more data in memory than you might expect.&lt;/p&gt;

&lt;h4&gt;
  
  
  Deleting elements
&lt;/h4&gt;

&lt;p&gt;Since Go does not have a respective method in its standard library, let's implement a method to delete an element of a slice of strings.&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;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slice&lt;/span&gt; &lt;span class="p"&gt;[]&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;element&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&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="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"element not found in slice"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;slice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;nil&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 watch closely, you can see that we can use our sub-slicing syntax with &lt;a href="https://en.wikipedia.org/wiki/Variadic_function"&gt;variadic arguments&lt;/a&gt; to cut the element from our slice. Another quite convenient feature at this place.&lt;/p&gt;

&lt;h4&gt;
  
  
  Inserting elements
&lt;/h4&gt;

&lt;p&gt;We do the same for inserting new elements.&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;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slice&lt;/span&gt; &lt;span class="p"&gt;[]&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;index&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;element&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="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nb"&gt;append&lt;/span&gt;&lt;span class="p"&gt;([]&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;element&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="n"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&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;In this case, we have no other option than creating a new slice with the &lt;code&gt;append()&lt;/code&gt; function. Again we can use &lt;a href="https://en.wikipedia.org/wiki/Variadic_function"&gt;variadic arguments&lt;/a&gt; as a shortcut.&lt;/p&gt;

&lt;h4&gt;
  
  
  Time complexity
&lt;/h4&gt;

&lt;p&gt;Now, let's enhance our time complexity chart for the available operations appending, inserting and deleting.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Action&lt;/th&gt;
&lt;th&gt;Complexity&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Accessing elements&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Searching elements&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Appending elements&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Inserting elements&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Deleting elements&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As we can see, working with arrays and slices in Go is quite convenient while there are some specialities which might be surprising for developers with a background in other languages. Generally spoken, working with arrays is very foreseeable and easy, which is why they are so popular. Nevertheless, when we work with large or specially structured data, they might not always suite your use case and come with certain performance downsides.&lt;br&gt;
I will elaborate on this in future posts on data structures in Go. For now, I hope I could shed some light onto these data types and their implementation. If you have any questions, feedback or just want to discuss about the topic, feel free to contact me via &lt;a href="//mailto:contact@benbarten.com"&gt;mail&lt;/a&gt; or &lt;a href="https://twitter.com/ben_barten"&gt;follow me on Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>go</category>
      <category>codenewbie</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Keep your service alive with a circuit breaker</title>
      <dc:creator>Ben</dc:creator>
      <pubDate>Thu, 19 Mar 2020 16:51:39 +0000</pubDate>
      <link>https://dev.to/benzo/keep-your-service-alive-with-a-circuit-breaker-1g9c</link>
      <guid>https://dev.to/benzo/keep-your-service-alive-with-a-circuit-breaker-1g9c</guid>
      <description>&lt;p&gt;Chances are high you are familiar with microservices architecture. If you do not work in such an environment, you should at least be familiar with the concepts of services running in a network, communicating with each other. With this architectural approach come certain challenges. When sending network calls, our service might run into timeouts of a downstream service. Not handling those timeouts or errors might result in processing or memory overload. To fix this, we can apply a pattern from the electrical hardware world: circuit breakers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why you would want a circuit breaker in place
&lt;/h3&gt;

&lt;p&gt;The circuit breaker pattern is an architectural pattern to make a specific function call fault tolerant and robust. It is used in distributed systems where remote or network calls take place. Its main goal is to quickly analyse if a downstream service is unhealthy and to stop calling it, thus risking memory or processing overload. You might be familiar with this term in the context of electrical switches where a circuit breaker is used to avoid damage on a electrical circuit by overload or a short circuit.&lt;br&gt;
If you are dealing with a high amount of traffic which depends on a certain downstream service, you should consider this pattern to improve your system’s overall stability and end user experience.&lt;/p&gt;
&lt;h3&gt;
  
  
  How does it work?
&lt;/h3&gt;

&lt;p&gt;The concept of a circuit breaker is fairly simple: whenever a downstream service or function stops working, our circuit breaker "trips" and stops calling it over and over until it works again. Meanwhile all attempted calls result in a specified error.&lt;/p&gt;

&lt;p&gt;To apply this pattern effectively in software, we need three steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;failure monitoring&lt;/li&gt;
&lt;li&gt;the circuit state machine&lt;/li&gt;
&lt;li&gt;error handling&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To illustrate the power of the circuit breaker pattern, we will implement a simple example which will focus on the circuit state machine. We will outline a very simple failure monitoring strategy, trigger the circuit state machine to “trip” and show up some real world use cases. The respective code for the state machine we are building can be found &lt;a href="https://github.com/bbenzo/go-circuit-breaker"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I do not recommend to use this code in a production-like context. There are already battle proven frameworks with this pattern. For Go you can check out &lt;a href="https://github.com/sony/gobreaker"&gt;gobreaker&lt;/a&gt; or if you are looking for a HTTP client with the respective functionality, have a look at &lt;a href="https://github.com/gojek/heimdall"&gt;Heimdall&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Overview
&lt;/h3&gt;

&lt;p&gt;Before we dive into the hands-on action, let’s have a look at a visual representation of this pattern. &lt;br&gt;
The circuit breaker can have three different states: &lt;em&gt;closed&lt;/em&gt;, &lt;em&gt;open&lt;/em&gt; and &lt;em&gt;half-open&lt;/em&gt;. The default state is closed. While being closed, the circuit breaker will forward all requests to a certain function or downstream service. Meanwhile, it monitors all requests and errors for the execution of a given function. When the circuit breaker is open, all incoming requests will return an error immediately. Anyhow, before switching from a closed to an open state, the circuit breaker will try to heal itself by switching to a half-open state.&lt;br&gt;
For the sake of simplicity, we will follow a rather simple approach for our failure monitoring strategy. Let’s say, we want our circuit to trip when a call throws a certain amount of consecutive errors. &lt;br&gt;
The circuit breaker pattern will look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pP-3oO1i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1584636096/circuit_breaker_komfda.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pP-3oO1i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1584636096/circuit_breaker_komfda.png" alt="pic1"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Implementation
&lt;/h3&gt;

&lt;p&gt;Now, let’s see how this might be implemented in code. At first, we define our different states.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;State&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;

&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="n"&gt;Closed&lt;/span&gt;   &lt;span class="n"&gt;State&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;
   &lt;span class="n"&gt;HalfOpen&lt;/span&gt; &lt;span class="n"&gt;State&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;
   &lt;span class="n"&gt;Open&lt;/span&gt;     &lt;span class="n"&gt;State&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our circuit breaker should take in a function and return a generic interface and an error. Therefore, we will implement a respective interface.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;CircuitBreaker&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;Execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="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="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;))&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="kt"&gt;error&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 next step is to define all required variables for our &lt;strong&gt;failure monitoring&lt;/strong&gt; strategy. In the beginning we said, the circuit breaker should start to trip when a certain error threshold is exceeded. Therefore, let’s add a strategy which holds the threshold to our circuit breaker, a counter to count our consecutive errors, and the current state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Strategy&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;threshold&lt;/span&gt;     &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;circuitBreaker&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;strategy&lt;/span&gt;          &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Strategy&lt;/span&gt;
   &lt;span class="n"&gt;state&lt;/span&gt;             &lt;span class="n"&gt;State&lt;/span&gt;
   &lt;span class="n"&gt;consecutiveErrors&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;CircuitBreaker&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;Execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="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="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;))&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="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="n"&gt;GetState&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="n"&gt;State&lt;/span&gt;
   &lt;span class="n"&gt;GetName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For now, this is enough to make the circuit breaker switch from &lt;em&gt;closed&lt;/em&gt; to &lt;em&gt;open&lt;/em&gt; whenever our &lt;code&gt;threshold&lt;/code&gt; is exceeded. Therefore we need to count the consecutive errors. We will also send an alert whenever our state has switched to open to signal that our function is not healthy. To keep things simple, our alert will just be a print of the open state to the console. &lt;br&gt;
Here is what our &lt;code&gt;Execute&lt;/code&gt; function might look like.&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;circuitBreaker&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;()&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="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;))&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="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;Closed&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
      &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;handleError&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="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
      &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&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;Sprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%v circuit breaker open"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&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;"ALERT: %v"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&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="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="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;circuitBreaker&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;handleError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;()&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="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;consecutiveErrors&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;consecutiveErrors&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;threshold&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is basically all you need to reproduce the behaviour of an electrical circuit breaker in code. &lt;br&gt;
Every consecutive call will result in an immediate error without even calling the original function. In a distributed system, this might keep you from stacking up calls to a downstream service and risking processing or memory overload. Nevertheless, this will keep the circuit breaker open forever. When we want to close it, because our downstream service is working again, we have to switch the state back manually. Therefore, we can implement a simple recovering mechanism. With Go’s fairly simple approach to concurrency, we can execute a go routine which checks if the function we want to execute is still returning an error.&lt;/p&gt;

&lt;p&gt;To enable this, we update our strategy to hold a &lt;code&gt;retryMax&lt;/code&gt; counter and &lt;code&gt;retryInterval&lt;/code&gt; which specify how often and with what interval we want to check if a service is up again.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Strategy&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;threshold&lt;/span&gt;     &lt;span class="kt"&gt;int&lt;/span&gt;
   &lt;span class="n"&gt;retryInterval&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
   &lt;span class="n"&gt;retryMax&lt;/span&gt;      &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let’s add the new state to our &lt;code&gt;Execute&lt;/code&gt; function and implement our error handling while in half open state.&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;circuitBreaker&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;()&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="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;))&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="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;Closed&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
      &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;handleError&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="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;handleSuccess&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
   &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;HalfOpen&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"circuit half open. trying to recover"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
      &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&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;Sprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%v circuit breaker open"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&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;"ALERT: %v"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&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="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="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;circuitBreaker&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;handleError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;()&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="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;consecutiveErrors&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;consecutiveErrors&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;threshold&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;HalfOpen&lt;/span&gt;
      &lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;recover&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="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;circuitBreaker&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;handleSuccess&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;consecutiveErrors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;circuitBreaker&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nb"&gt;recover&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;()&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="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;retries&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
   &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;HalfOpen&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c"&gt;// Open circuit breaker when recovering fails&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;retries&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;retryMax&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Open&lt;/span&gt;
         &lt;span class="k"&gt;return&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="o"&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;Duration&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;retryInterval&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

      &lt;span class="c"&gt;// set state to closed if request is successful&lt;/span&gt;
      &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Closed&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="n"&gt;retries&lt;/span&gt;&lt;span class="o"&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;Now we got it! Our circuit breaker can heal itself by checking upon the given function if it still continues to throw errors. Instead of switching to the open state immediately, we switch to half-open and fire up a go routine which executes the &lt;code&gt;recover&lt;/code&gt; function. While the &lt;code&gt;retryMax&lt;/code&gt; threshold is not exceeded, it will asynchronously continue sending new requests to the service to check if it is still returning errors. Meanwhile, the &lt;code&gt;Execute&lt;/code&gt; function is still returning immediate errors to the consumer of the original function, thus preventing our service from overload. If the &lt;code&gt;retryMax&lt;/code&gt; threshold is exceeded, we will set the state to open and send the alert we configured before. If our function stops returning errors, we reset the state to &lt;em&gt;closed&lt;/em&gt; and the &lt;code&gt;consecutiveErrors&lt;/code&gt; to 0. If you want to see this in action, you can have a look at the &lt;a href="https://github.com/bbenzo/go-circuit-breaker"&gt;tests in the GitHub repository&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;By now, you should have a good understanding of how this pattern might help you in a distributed system and you might consider it the next time you are dealing with systems with high throughput. If you have any&lt;br&gt;
questions or any kind of feedback, feel free to leave comment, contact me or &lt;a href="https://twitter.com/ben_barten"&gt;follow me on Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>go</category>
      <category>microservices</category>
      <category>programming</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Go Big O</title>
      <dc:creator>Ben</dc:creator>
      <pubDate>Mon, 09 Mar 2020 10:54:03 +0000</pubDate>
      <link>https://dev.to/benzo/go-big-o-43k8</link>
      <guid>https://dev.to/benzo/go-big-o-43k8</guid>
      <description>&lt;p&gt;Software development has become widely accessible in today’s world. Besides the traditional academic way, &lt;br&gt;
there are endless ways to study the concepts of software development. &lt;br&gt;
Getting acquainted with modern software development frameworks is just as easy as watching an online course on platforms like Udemy or Coursera. As long as you have a computer with internet connection at hand, you can teach yourself software development with basically no money. So did I.&lt;/p&gt;

&lt;p&gt;What is widely missed in these courses and often also underrated in the workforce are the basic concepts of computer science.&lt;br&gt;
This post aims at getting started with these concepts, explicitly the &lt;strong&gt;Big O notation&lt;/strong&gt;, which every developer should not only be familiar with but master it, if he or she wants to do a good job. Furthermore, having a solid knowledge of &lt;strong&gt;Big O&lt;/strong&gt; will be fundamental to survive a technical job interview. Since I am currently learning Go as a new programming language, all examples will be provided in Go. You can find all examples and algorithms in this &lt;a href="https://github.com/bbenzo/algo"&gt;GitHub repository&lt;/a&gt;. The algorithms are implemented by me, so there will probably be more efficient implementations. Any feedback is welcome!&lt;/p&gt;
&lt;h3&gt;
  
  
  What is Big O?
&lt;/h3&gt;

&lt;p&gt;Big O notation is a mathematical notation that describes the limiting behavior of a function when the argument tends towards a particular value or infinity. (Source: &lt;a href="https://en.wikipedia.org/wiki/Big_O_notation"&gt;Wikipedia&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;In other words, with Big O we are looking for a generalised way to describe how well an algorithm will perform when maximising its input. Therefore, we are not looking for the exact number of executed lines of codes but rather for the growth rate of the function in respect to executed lines of code. This can be in a matter of time as well as space/memory. &lt;br&gt;
We will have a closer look at time and space complexity within this article.&lt;/p&gt;
&lt;h3&gt;
  
  
  An Example
&lt;/h3&gt;

&lt;p&gt;To get a better sense of how Big O is calculated, let’s have a look at the classic (not so efficient) selection sort algorithm. &lt;br&gt;
Here is a simple implementation in Go:&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;SelectionSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int32&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;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&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;j&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&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;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;swap&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;swap&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="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We iterate over every element in the list and compare its value by iterating over every element after the given one. &lt;br&gt;
Now, let’s call the size of the list n. First, we iterate n times over the list, then the inner loop iterates over the following elements n-1 times. This leaves us with n * (n-1) which is equal to (n^2 )/2 - n/2. In Big O notation this can be reduced to O(n^2 ). To understand why, let's have a closer look.&lt;/p&gt;
&lt;h3&gt;
  
  
  Best Case, Worst Case, and Expected Case
&lt;/h3&gt;

&lt;p&gt;To describe the runtime of our algorithm, we actually have three different ways: the best, worst and expected case. To illustrate this, let's have a look at this implementation of the Quick Sort algorithm.&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;QuickSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&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="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;pivotIndex&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;partition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pivotIndex&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="n"&gt;pivotIndex&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="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;partition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&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="n"&gt;pivotIdx&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;leftIdx&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rightIdx&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="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;2&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;arr&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;foundLeft&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;leftIdx&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;rightIdx&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;leftIdx&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;pivotIdx&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;leftIdx&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pivotIdx&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;foundLeft&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;leftIdx&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;foundRight&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;rightIdx&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;leftIdx&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;rightIdx&lt;/span&gt; &lt;span class="o"&gt;&amp;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="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;rightIdx&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;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pivotIdx&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;foundRight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;rightIdx&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;foundLeft&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;foundRight&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;swap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;leftIdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rightIdx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;partition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pivotIdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;leftIdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rightIdx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;leftIdx&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;rightIdx&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;swap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;leftIdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pivotIdx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="n"&gt;partition1&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;leftIdx&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;pivotIndex1&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;partition1&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="n"&gt;partition2&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;leftIdx&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;pivotIndex2&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;partition2&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="n"&gt;partition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;partition1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pivotIndex1&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="n"&gt;pivotIndex1&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="n"&gt;partition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;partition2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pivotIndex2&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="n"&gt;pivotIndex2&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="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;arr&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;swap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&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="n"&gt;leftIdx&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rightIdx&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;swap&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;leftIdx&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;leftIdx&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;rightIdx&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;rightIdx&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;swap&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 are not familiar with the implementation of Quick Sort, I recommend &lt;a href="https://www.youtube.com/watch?v=Hoixgm4-P4M"&gt;this short video&lt;/a&gt;.&lt;br&gt;
Quick Sort is a very efficient sorting algorithm which consists of recursively partitioning an array into two subsequent arrays around an initially determined pivot. The left array holds smaller values than the pivot, the right one bigger values. This implementation of quick sort sets the last element of the array as the initial pivot. &lt;/p&gt;

&lt;p&gt;Now, let's have a look at what might happen&lt;br&gt;
during execution.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Best case: All elements are equal. In this scenario, quick sort will just traverse through the array and return the array as it is.
This leaves us with a complexity of &lt;strong&gt;O(n)&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Worst case: The chosen pivot is repeatedly the highest element in the array. In this case, partitioning the array will
only result in reducing the array by one element and will effectively end in a &lt;strong&gt;O(n^2 )&lt;/strong&gt; runtime.&lt;/li&gt;
&lt;li&gt;Expected case: The above examples are rare scenarios which will, depending on the implementation of th algorithm, happen
from time to time given a certain input. But on average, Quick Sort has a &lt;strong&gt;O(n * log n)&lt;/strong&gt; runtime.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are not familiar with the Big O notation and you feel a bit confused by now, don't worry! We will take a step back&lt;br&gt;
and have a close look at the most common scenarios of time complexity now.&lt;/p&gt;
&lt;h3&gt;
  
  
  Typical Big Os and their Complexity
&lt;/h3&gt;

&lt;p&gt;Below, we can see a Big O complexity chart. It shows the different Big O types and their growth rate for an increasing &lt;br&gt;
number of elements.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9PnbLwdK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1583750484/big_o_complexity_chart_h9tryt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9PnbLwdK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/dcx2m9enb/image/upload/v1583750484/big_o_complexity_chart_h9tryt.png" alt="pic1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(Source: &lt;a href="https://www.bigocheatsheet.com/"&gt;https://www.bigocheatsheet.com/&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;In our leading example, we said, with Big O we can reduce a complexity of (n^2 )/2 - n/2 to O(n^2 ). The reason why we only focus on the &lt;strong&gt;dominant term&lt;/strong&gt; of a function is, because it is dominating the growth rate in the long run. &lt;br&gt;
In addition, we can &lt;strong&gt;drop the constants&lt;/strong&gt; leading the dominant term. This might be a bit confusing in the beginning, but you can easily verify this behaviour with inserting very high values. The only thing which really interests us with Big O is, how the algorithm scales in a generalized way. &lt;/p&gt;

&lt;p&gt;To demonstrate this, I will show some common examples of these Big O types and briefly explain why they perform as they do.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Given a list, print the first element of that list. No matter how big that list is, we will always have only that one operation. &lt;br&gt;
If there is any way, this is what we should aim for.&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;PrintFirstElementOfList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list&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;list&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;strong&gt;O(log n)&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;The classical binary search algorithm is a perfect example. Given a sorted list, search a number by cutting the list in half and comparing its value to the ends of the two halves. Halving the input with each iteration leaves us with a time complexity of log n.&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;BinarySearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sortedList&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="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
    &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sortedList&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="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;sortedList&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&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="n"&gt;i&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;sortedList&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&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="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&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="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;O(n)&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Traversing a list or array. One operation for each element. While it is still worse than O(log n), O(n) is fairly acceptable and easy to estimate.&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;PrintEveryItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list&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="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&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;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&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;&lt;strong&gt;O(n log n)&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;This one is a bit more tricky and famous case for technical interviews. One classical example for this case is the Merge Sort algorithm. At first, a list is divided into halves until we have only lists with one element (log n) and then all elements are merged together iteratively (n). It is more complex than O(n) but still not as bad as polynomials.&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;MergeSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&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="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&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="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&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;merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;MergeSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;MergeSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;right&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;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&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="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="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;length&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&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;left&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
   &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;:&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;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;right&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;merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;left&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="n"&gt;right&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="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;merged&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="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

   &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
   &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;left&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="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;right&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="n"&gt;merged&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;left&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="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="n"&gt;merged&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;right&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="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&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;j&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;merged&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&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;j&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;merged&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&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;merged&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;O(n^2 )&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Whenever you deal with this pattern, be cautious. As in this example, when we print all possible pairs of an array of integers which leads us to a time complexity of O(n^2 ). Especially nested loops should let your alarm bells ring. &lt;br&gt;
You should only consider these algorithms if you can estimate the input size.&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;PrintAllPairs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list&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="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&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;j&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&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;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%v:%v"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&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;&lt;strong&gt;O(2^n )&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;These algorithms are extremely bad, since they rise meteorically. As in this example, for a recursive calculation of the fibonacci numbers every additional n doubles the growth of the algorithm. Just avoid these.&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;RecursiveFibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="kt"&gt;int&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="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="m"&gt;1&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;num&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;recursiveFibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&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="n"&gt;recursiveFibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;O(n!)&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;The most trivial example for an algorithm with factorial time complexity is - well - factorial addition.&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;FactorialAddition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="kt"&gt;int&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="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&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;factorialAddition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&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="p"&gt;}&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Time vs. Space Complexity
&lt;/h3&gt;

&lt;p&gt;By now, we should have a good understanding of how complexity is computed and described. So far, we only looked at the time it takes to execute an algorithm and the respective growth rate. In computation, besides time, space is one of our concerns which must be analysed.&lt;br&gt;
If we look at our example of the beginning, the selection sort algorithm, the space complexity is O(1). We only need our swap variable to keep the current value to be swapped even though our time complexity is O(n^2 ). Compared to the O of time complexity, we now have to analyse how much space in memory has to be allocated. In most cases, space complexity is not as high as time complexity. &lt;br&gt;
To illustrate this, we will have a look at a simple example.&lt;/p&gt;

&lt;p&gt;First of all, we need to know much memory is used by different data types:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Size&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;bool, char, unsigned char, signed char, int8&lt;/td&gt;
&lt;td&gt;1 byte&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;int16, short, unsigned short, wchar_t&lt;/td&gt;
&lt;td&gt;2 byte&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;float, int32, int, unsigned int, long, unsigned long&lt;/td&gt;
&lt;td&gt;4 byte&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;double, int64, long double, long long&lt;/td&gt;
&lt;td&gt;8 byte&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Now let's have a look at a simple sum 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="c"&gt;// n is the length of the array&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&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="n"&gt;n&lt;/span&gt; &lt;span class="kt"&gt;int&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="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="c"&gt;// 4 bytes for result&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c"&gt;// 4 bytes for i&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&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="n"&gt;result&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our space complexity scales linearly with the size of our array. Besides the three variables &lt;code&gt;result&lt;/code&gt;, &lt;code&gt;i&lt;/code&gt; and &lt;code&gt;n&lt;/code&gt; which take up &lt;code&gt;3*4&lt;/code&gt; bytes of memory, each element of the array takes up 4 bytes as well. This leads us to &lt;code&gt;4*n + 3*4&lt;/code&gt; bytes. Drop the constants and we are left with &lt;strong&gt;O(n)&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;By now, you hopefully have an enhanced understanding of Big O and its role in software development. Even though, in your daily&lt;br&gt;
business as a software developer you will most likely not be confronted with this topic, it is important to inherit these&lt;br&gt;
principals to be able to analyse your code and make a profound statement about its complexity.&lt;/p&gt;

&lt;p&gt;If you have any questions on this topic, feedback or you have any ideas on new topics that you want me to write about, feel free to &lt;a href="//mailto:contact@benbarten.com"&gt;contact me&lt;/a&gt; or follow me on &lt;a href="https://twitter.com/ben_barten"&gt;Twitter&lt;/a&gt;.&lt;br&gt;
Besides this being a way of improving my own learnings, I enjoy technical writing, so I am happy for new ideas and any discussions.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>codenewbie</category>
      <category>go</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
