<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Daiki Nakashita on Medium]]></title>
        <description><![CDATA[Stories by Daiki Nakashita on Medium]]></description>
        <link>https://medium.com/@dakaii?source=rss-41ef483bbaf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*0IvxcjPPKeVL4_5I.</url>
            <title>Stories by Daiki Nakashita on Medium</title>
            <link>https://medium.com/@dakaii?source=rss-41ef483bbaf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 02:10:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dakaii/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Say No to Those Lengthy Coding Assignments]]></title>
            <link>https://dakaii.medium.com/say-no-to-those-lengthy-coding-assignments-348181ffd9d9?source=rss-41ef483bbaf------2</link>
            <guid isPermaLink="false">https://medium.com/p/348181ffd9d9</guid>
            <category><![CDATA[company-culture]]></category>
            <category><![CDATA[interview]]></category>
            <category><![CDATA[tech-industry]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Daiki Nakashita]]></dc:creator>
            <pubDate>Tue, 31 May 2022 13:57:57 GMT</pubDate>
            <atom:updated>2023-04-04T01:29:19.438Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7J_yKSdAXAdteyKL" /><figcaption>Photo by <a href="https://unsplash.com/@jstrippa?utm_source=medium&amp;utm_medium=referral">James Harrison</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>One of the most annoying things, when I apply for jobs as a software engineer, is that a lot of companies don’t seem to have figured out how to accurately assess candidates’ competence. Some of them would send me a link to a HackerRank coding challenge and have me solve unnecessarily complex algorithm problems, and others would give me a coding assignment that’d take 3 days to complete. I, personally, despise those lengthy coding assignments.</p><p>There was a time when the company I was interviewing for had me complete an assignment, (which took me about 2 days to finish…) and the interviewer asked me about the time complexity of the solution I submitted during the subsequent interview. I initially answered, “I’m not entirely sure…But I think it should be something like O(3^n) because this function is calling itself 3 times ”. Figuring out the time complexity of a recursive function is no easy task. I couldn’t figure it out on the spot. The interviewer subsequently responded with…. <br>“There are 3 loops, so it should be something like O(3N), right?”</p><p>Then, I thought to myself “there are no….loops…..?”. He referred to the recursive calls as “loops” and inaccurately estimated the time complexity to be way more efficient than it should be. However, I guess I was the one that incorrectly assessed the time complexity from his perspective. (But really though…? loops?) I joined the company about a month later and found out they were having difficulty hiring because a lot of candidates won’t even work on the coding assignment. They also had trouble retaining engineers because their tech lead whose job title was “senior solution architect” only had 3 years of experience as an engineer and was also a dick to everyone… (Obviously the latter was the primary reason why so many people were leaving every month.)</p><p>Then it occurred to me “maybe… most interviewers at startups aren’t experts on algorithmic problems. They are merely imitating FAANG interviews because they don’t know how else to figure out who to hire. They are not working for FAANG companies just like I’m not for a reason.”</p><p>What’s the point of spending 2–3 days on a coding assignment only to have your skill level assessed by another engineer who can’t even accurately assess the performance of the submitted solution…? (I can’t either, tbh.)</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fbx3--22D4E4%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dbx3--22D4E4&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fbx3--22D4E4%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/feb07d68f83c24f42b6c4701817c974c/href">https://medium.com/media/feb07d68f83c24f42b6c4701817c974c/href</a></iframe><p>As Ben Awad articulates how broken coding interviews are in the video, I also do believe that coding interviews are indeed broken. I don’t see a point in inverting a binary tree for them to see if I’m skilled enough as a frontend engineer, either, but at least those algorithm problems he mentions in the video are somewhat consistent across many companies and less time-consuming.</p><p>The problem I’m hoping to address here is that by accepting to work on these lengthy coding assignments, we might be promoting this unhealthy interview culture indirectly where the potential employers can take advantage of the power imbalance of the employer/employee relationship and expect you to spend your entire weekend working on a project that they’ll quickly glance through the following Monday.</p><p>I have interviewed for positions at a few companies that claim that their coding assignments shouldn’t take more than 3–4 hours, and in a lot of cases, what they said was true. This one company asked me after the interviews how long I took to complete their assignment and told me my answer won’t affect the interview outcome and that they are simply trying to improve the interview efficiency.<br>We, as engineers, should appreciate such companies and their interview culture where they value candidates’ time and constantly make an extra effort to improve the interview efficiency.</p><p>A lot of software engineers spend extra time working on personal projects on the weekend, learning about new technologies and frameworks. We shouldn’t be required to work on company-specific coding assignments that take 4 — 5 days to complete. In hopes of promoting a more efficient interview culture, we should start saying no to those lengthy coding assignments. With a few years of professional experience under your belt, you can definitely find a company that needs your coding expertise and won’t subject you to a day’s worth of free labor.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=348181ffd9d9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Basic Algorithms That You Need to Know by Heart to Crack Your Interviews]]></title>
            <link>https://medium.com/analytics-vidhya/basic-algorithms-that-you-need-to-know-by-heart-as-a-self-taught-junior-developer-4005b14b865c?source=rss-41ef483bbaf------2</link>
            <guid isPermaLink="false">https://medium.com/p/4005b14b865c</guid>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Daiki Nakashita]]></dc:creator>
            <pubDate>Wed, 16 Feb 2022 14:15:09 GMT</pubDate>
            <atom:updated>2022-05-29T06:50:08.070Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*OO_0QI_6DTsE9aqP" /><figcaption>Photo by <a href="https://unsplash.com/@clemhlrdt?utm_source=medium&amp;utm_medium=referral">Clément Hélardot</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>I learned to code on my own after college and have been working as a software engineer for the past 6 years. I mentioned my background and how I got into this field to my friends, and a lot of them asked me what they should study and whether they need to go to a BootCamp to become a software engineer. The simple answer is no. You don’t need to go to school to become a software engineer. You can set foot in the software industry without a formal education in computer science. You just need to work on coding projects and try making Android applications or something. (You’ll get paid significantly less than your CS major counterparts for the initial 2–3 years, though.) So, What are the differences between software engineers that received a formal education in computer science and the ones that taught themselves?</p><p>For one, the ones that received a formal education have been exposed to more aspects of computer science while you may have only worked on Android/IOS applications as a self-taught programmer. But the biggest difference that affects your hirability and your practical software engineering skills is probably the understanding of algorithms and data structures. You won’t know the difference between lists and dictionaries, the performance of those data structures, or even how and when to use them.</p><p>So here are some of the fundamental algorithms that you absolutely need to know by heart to pass the future coding interviews.</p><ol><li>Valid Parentheses</li></ol><p><a href="https://leetcode.com/problems/valid-parentheses/">Valid Parentheses - LeetCode</a></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/075919e4114a82463feadab93ca0626a/href">https://medium.com/media/075919e4114a82463feadab93ca0626a/href</a></iframe><p>This problem and the solution give you an idea of how to use a stack and a dictionary.</p><p>Dictionaries are used to store data in key/value pairs. It has the average (amortized) time complexity of O(1) and the worst time complexity of O(n) for lookups, meaning that if you wanted to check if a value is present in a dictionary with a particular key, your computer can do it really fast most of the time, but if you&#39;re extremely unlucky, it might take a long time.</p><p>There’s also a very similar data structure called a set. Sets are primarily used to store multiple values in order to keep track of whether a particular data value has already been added.</p><p>Before I explain the other data structure mentioned above called a stack. Here’s a good article to read to understand what FIFO and LIFO are.</p><p><a href="https://www.guru99.com/python-queue-example.html#:~:text=There%20are%20two%20types%20of,the%20put(item)%20method.">Python Queue: FIFO, LIFO Example</a></p><ul><li>For a FIFO (First in First out Queue), the element that goes first will be the first to come out.</li><li>For a LIFO (Last in First out Queue), the element that is entered last will be the first to come out.</li></ul><p>(The LIFO queue data structure is also commonly called a stack since the visual representation of its behavior is similar to a stack of pancakes where what’s placed last will be the first one to be taken out.)<br>You need to use these data structures to efficiently keep track of and process data in your algorithms.</p><p>2. Time to Sell Stock</p><p><a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock/">Best Time to Buy and Sell Stock - LeetCode</a></p><p>Sometimes you are given a problem and solve it, and the interviewer tells you to optimize it or says something like “you can do it in one pass”. They would probably tell you that if you didn&#39;t know this optimized solution (unless you are smart enough to come up with it on the spot.)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/670f7c59056add815b41c08fa09cb4fa/href">https://medium.com/media/670f7c59056add815b41c08fa09cb4fa/href</a></iframe><p>3. Tree Traversals</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/95b851bc241a969c47d1b06e40ba746b/href">https://medium.com/media/95b851bc241a969c47d1b06e40ba746b/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3962d3bbc93fa30ea2cf1e4cacfcc3d8/href">https://medium.com/media/3962d3bbc93fa30ea2cf1e4cacfcc3d8/href</a></iframe><p><a href="https://leetcode.com/problems/binary-tree-inorder-traversal/">LeetCode - The World&#39;s Leading Online Programming Learning Platform</a></p><p>You should do some research on tree structures and in-order, pre-order, post-order traversal algorithms if you’re not already familiar with them, but the key takeaway from these solutions above is that you can always turn a recursive function into an iterative function, and a lot of times, the iterative solution will be more memory-efficient.</p><p><a href="https://stackoverflow.com/questions/931762/can-every-recursion-be-converted-into-iteration#:~:text=Can%20you%20always%20turn%20a,Turing%20machine)%20and%20vice%20versa">Can every recursion be converted into iteration?</a></p><p>4. Binary Search</p><p><a href="https://leetcode.com/problems/binary-search/">Binary Search - LeetCode</a></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/49411f38acceaf38d729c4f878cfb11a/href">https://medium.com/media/49411f38acceaf38d729c4f878cfb11a/href</a></iframe><p>This is a very widely known algorithm, you should definitely review this before your interview (along with all the other ones on this article….)</p><p>5. Depth-first search</p><p>I had to work on this problem and the similar variations of it during my past interviews so many times, and I already memorized it by heart, and you probably should too.</p><ul><li><a href="https://leetcode.com/problems/number-of-islands/">Number of Islands - LeetCode</a></li><li><a href="https://leetcode.com/problems/number-of-islands/discuss/56340/Python-Simple-DFS-Solution">Python Simple DFS Solution - LeetCode Discuss</a></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/81665e0532d4a40099431f0a0674d664/href">https://medium.com/media/81665e0532d4a40099431f0a0674d664/href</a></iframe><p>6. Fibonacci Sequence</p><p><a href="https://realpython.com/fibonacci-sequence-python/">A Python Guide to the Fibonacci Sequence - Real Python</a></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a0916ee3178a495e263d7c426acdaab7/href">https://medium.com/media/a0916ee3178a495e263d7c426acdaab7/href</a></iframe><p>They will always have you optimize your naive recursive solution with a dictionary…</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2ea672376053509160b28aea81549c9a/href">https://medium.com/media/2ea672376053509160b28aea81549c9a/href</a></iframe><p>You are wondering why you need to use a dictionary and retain the previously calculated values?<br>It’s super nicely explained in this Youtube video.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fe0CAbRVYAWg%3Fstart%3D3%26feature%3Doembed%26start%3D3&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3De0CAbRVYAWg&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fe0CAbRVYAWg%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/35857fe9f5b197603c71d3e23b807327/href">https://medium.com/media/35857fe9f5b197603c71d3e23b807327/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/34ca1697a96142b6a770c286b48b4cef/href">https://medium.com/media/34ca1697a96142b6a770c286b48b4cef/href</a></iframe><p>The last one with the yield keyword is only a must for python developers.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a945066899af7b39e64cd40c9587810/href">https://medium.com/media/3a945066899af7b39e64cd40c9587810/href</a></iframe><p>Bonus: Effective Python and Cracking the Coding Interview</p><p><a href="https://www.amazon.com/Effective-Python-Specific-Software-Development-dp-0134853989/dp/0134853989/ref=mt_other?_encoding=UTF8&amp;me=&amp;qid=">Effective Python: 90 Specific Ways to Write Better Python (Effective Software Development Series)</a></p><p>If you’re a python developer, I highly recommend checking this book out.</p><p><a href="https://www.amazon.com/Cracking-Coding-Interview-Programming-Questions/dp/0984782850/ref=sr_1_1?crid=ZRCT1RWE3AUN&amp;keywords=cracking+the+coding+interview&amp;qid=1645019553&amp;s=books&amp;sprefix=cracking+the+c%2Cstripbooks-intl-ship%2C425&amp;sr=1-1">Cracking the Coding Interview: 189 Programming Questions and Solutions</a></p><p>Also, every software engineer probably needs this book as well.</p><p>(I don’t work for Amazon or anything …. I wish though.)</p><p>Of course, there are a lot more things you need to know to pass the interviews and work productively and efficiently. You need to learn about everything from sorting algorithms to new frameworks and what microservices are and what could potentially be the bottleneck in your application.<br>But in terms of the interview algorithm questions, some companies seem to give their candidates incredibly tough problems, and when you encounter one of those companies… maybe they aren’t trying to hire you.<br>(Also some companies would give you super lengthy coding assignments for you to complete within a week or so. You‘ve just got to say no to those)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zpiinkKH07YE4fOv1mIbCw.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4005b14b865c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/basic-algorithms-that-you-need-to-know-by-heart-as-a-self-taught-junior-developer-4005b14b865c">Basic Algorithms That You Need to Know by Heart to Crack Your Interviews</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Creating a Super Easily Testable Django App with Docker, Pytest, and Factory Boy.]]></title>
            <link>https://medium.com/analytics-vidhya/creating-a-super-easily-testable-django-app-with-docker-2f3477101186?source=rss-41ef483bbaf------2</link>
            <guid isPermaLink="false">https://medium.com/p/2f3477101186</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[factory-boy]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Daiki Nakashita]]></dc:creator>
            <pubDate>Fri, 08 Jan 2021 15:10:39 GMT</pubDate>
            <atom:updated>2023-06-09T08:57:35.521Z</atom:updated>
            <content:encoded><![CDATA[<h3>Writing Integration Tests for a Django App with Docker, Pytest, and Factory Boy.</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vA2s_hISIlgnHMycQkxCIA.jpeg" /></figure><p>I’ve seen a lot of different ways to set up a Django project, but I believe that the development environment of Django should always be containerized with Docker. The reason being, I know how frustrating it is to have to manually set up the testing database, destroy it, and re-create it when something goes wrong, and docker-containerizing it will save you a lot of time and effort. As a bonus, it’ll just be one YAML file away from being ready to be deployed on Kubernetes! (Although That might depend on the cloud service you are planning to use…)</p><h3>Docker Configuration</h3><p>First, here’s the docker file. I understand that there are some downsides to using the alpine image, but I’m sure you can easily replace it with a smaller image such as slim, and the required changes will be minimal.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e11700f54f0a41ab7aaec94bc1067db4/href">https://medium.com/media/e11700f54f0a41ab7aaec94bc1067db4/href</a></iframe><p>You can set up the development environment with the YAML file below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3aea21e22e4e5211076582aa3f88d1f4/href">https://medium.com/media/3aea21e22e4e5211076582aa3f88d1f4/href</a></iframe><p>You might be wondering why two different port numbers are listed in the ports section. The reason is I use VSCode as my primary code editor, and in order to debug the project with breakpoints, you would need to use a pip package named ptvs and expose a port to get it to work. Here is an example of how to expose the debugging port.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/df2a1d99c5dedb890d1ea908ca4469f5/href">https://medium.com/media/df2a1d99c5dedb890d1ea908ca4469f5/href</a></iframe><p>Creating a make file also will make your life a lot easier.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/402f260c545834c390bfc7534cbeb009/href">https://medium.com/media/402f260c545834c390bfc7534cbeb009/href</a></iframe><h3>Writing Tests</h3><p>There are many different ways to write tests for your Django application. You could use fixtures or use the mock package and mock everything. I personally like to use the packages below.</p><ul><li><a href="https://github.com/pytest-dev/pytest-factoryboy">pytest-dev/pytest-factoryboy</a></li><li><a href="https://github.com/kiwicom/pytest-recording">kiwicom/pytest-recording</a></li></ul><p>You probably shouldn&#39;t be wasting your time mocking API responses unless you really have to. It’s incredibly prone to typing errors, and you will potentially need to make a lot of adjustments manually if the APIs you&#39;re using introduce specification revisions. You can just record the API response with the recording package above and let your application handle the potential specification revisions.<br>In the example Django application that I created and linked at the bottom of this article, I didn’t use the recording package since I’m not using any external API in it. However, I did use Factory Boy to populate the testing database, and I’ll attach a few code snippets to help you understand how it’s used. (I swear it’s way easier to use than fixtures….)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c6030a612eef093ff2fad513bdfa1cba/href">https://medium.com/media/c6030a612eef093ff2fad513bdfa1cba/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d6abde75dd3b86b7e6cf028fe4e4a75f/href">https://medium.com/media/d6abde75dd3b86b7e6cf028fe4e4a75f/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac93435ba80d179e2d3b58b12048052a/href">https://medium.com/media/ac93435ba80d179e2d3b58b12048052a/href</a></iframe><p>Django and Ruby on Rails being such highly advanced frameworks with a lot of modern features provided by default, I really don’t understand the benefits of using technologies like NodeJS and TypeORM. Golang has goroutines and its uniquely simple syntax, but what does NodeJS really offer? (Seriously, why is NodeJS so popular?)</p><p><a href="https://github.com/dakaii/fuzzy-parakeet/tree/medium">dakaii/fuzzy-parakeet</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2f3477101186" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/creating-a-super-easily-testable-django-app-with-docker-2f3477101186">Creating a Super Easily Testable Django App with Docker, Pytest, and Factory Boy.</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Repository Pattern in Golang]]></title>
            <link>https://dakaii.medium.com/repository-pattern-in-golang-d22d3fa76d91?source=rss-41ef483bbaf------2</link>
            <guid isPermaLink="false">https://medium.com/p/d22d3fa76d91</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[repository-pattern]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Daiki Nakashita]]></dc:creator>
            <pubDate>Sun, 03 Jan 2021 15:51:04 GMT</pubDate>
            <atom:updated>2022-02-13T14:12:45.064Z</atom:updated>
            <content:encoded><![CDATA[<h3>Repository Pattern in Golang (a.k.a. the best way I could think of to pass around the database connection in this oddly hyped language.)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*6pDypcdk1oeKi5BzBGYhDw.png" /></figure><h3>Background</h3><p>Having been working as a software engineer for almost 5 years, I knew about the hype surrounding Golang for some time but never really worked on a big enough project that drives me to explore the pros and cons and the quirks of the language. Since Covid-19 is forcing me to be a loner, I decided to work on a little project.</p><p>The first obstacle that I faced was, “How do I pass around the database instance?” When you use a framework like Ruby on Rails or Django, the framework provides you a way to declare the database connection. By default, you basically have access to the database instance from anywhere in the application.</p><p>That’s not the case for Golang. Golang doesn’t have a mature, full-fledged framework like Django backed by thousands of contributors, and you would need to think of a suitable project structure on your own. I discovered a blog post about different ways to pass around the database connection.</p><p><a href="https://techinscribed.com/different-approaches-to-pass-database-connection-into-controllers-in-golang/">Different Ways to Pass Database Connection to Controllers in Golang</a></p><p>I was too lazy to read the entire article (Although I know I’ve been verbosely blabbering about this project&#39;s background), but what I got from this article is that the repository pattern is the way to GO! (I know you know what I just did there.)</p><h3>Structure and Implementation</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b3c7ca1b39ca0fdc8a48286234474e6e/href">https://medium.com/media/b3c7ca1b39ca0fdc8a48286234474e6e/href</a></iframe><p>One of the modifications I made that deviates from what was suggested in the article is that I decided to declare the repository interface inside the user package to decouple the repository&#39;s actual implementation and initialize it in the controller, as shown below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ac523904e24c671a8a85b51bd35eb8a/href">https://medium.com/media/4ac523904e24c671a8a85b51bd35eb8a/href</a></iframe><p>Well, I guess it doesn’t make much sense unless I post the implementation of the repository package…</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4dd259308c2cc64880c9730d2770dbd4/href">https://medium.com/media/4dd259308c2cc64880c9730d2770dbd4/href</a></iframe><p>Then, I Initialized the repository.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/57f4a886db308c000104e1b2fe9781ab/href">https://medium.com/media/57f4a886db308c000104e1b2fe9781ab/href</a></iframe><p>Finally, You’ll need to initialize the database connection, repositories, and controllers as follows.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f33dd06f2e89870a4fc7bc477f66e65/href">https://medium.com/media/1f33dd06f2e89870a4fc7bc477f66e65/href</a></iframe><p>In order to help you understand the project structure, I will link my Github repository here.</p><p><a href="https://github.com/dakaii/graphyy/tree/medium">dakaii/graphyy</a></p><p>I used GraphQL to expose the APIs, using the following library, and it’s actually something I regret the most about this project.</p><p><a href="https://github.com/graphql-go/graphql">graphql-go/graphql</a></p><p>The problem is that it takes roughly 4 to 5 seconds for this application to return a response upon an API call for some reason. I initially suspected that it might be because of Gorm, but after digging into it, I realized it might be because of this GraphQL package. I initially tried to use a library called Thunder, but it was too buggy. I replaced it with the GraphQL package above, but it still poses some performance issues.</p><p>(I didn’t spend too much time delving into it, so maybe the lag is caused by Gorm or something else… I have no idea at the moment)</p><h4>Something to Remember</h4><p>I discovered this one thing, and I hope it’ll save you some time in the future.<br>If you ever need to compile a GoLang project for an Operating System that’s different from the one you’re building it on, you will need to specify the target OS by explicitly providing environmental variables.<br>In my case, I use macOS but decided to use Ubuntu as the development docker image, so I had to run this command.</p><pre>env GOOS=linux GOARCH=386 go build -o build .</pre><ul><li><a href="https://stackoverflow.com/questions/20829155/how-to-cross-compile-from-windows-to-linux">How to cross compile from Windows to Linux?</a></li><li><a href="https://www.digitalocean.com/community/tutorials/building-go-applications-for-different-operating-systems-and-architectures">Building Go Applications for Different Operating Systems and Architectures | DigitalOcean</a></li></ul><p>Well, I hope it was helpful in some way…. Happy social distancing!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d22d3fa76d91" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>