<?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 Uğur Kınık on Medium]]></title>
        <description><![CDATA[Stories by Uğur Kınık on Medium]]></description>
        <link>https://medium.com/@ugurkinik?source=rss-b850ff534905------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*5X1hKC_vClRSclJKx0cBGQ.jpeg</url>
            <title>Stories by Uğur Kınık on Medium</title>
            <link>https://medium.com/@ugurkinik?source=rss-b850ff534905------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 15:14:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ugurkinik/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[Goroutines are not threads]]></title>
            <link>https://medium.com/@ugurkinik/goroutines-are-not-threads-958c53d0d7f0?source=rss-b850ff534905------2</link>
            <guid isPermaLink="false">https://medium.com/p/958c53d0d7f0</guid>
            <category><![CDATA[go]]></category>
            <category><![CDATA[goroutines]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[threads]]></category>
            <category><![CDATA[concurrency]]></category>
            <dc:creator><![CDATA[Uğur Kınık]]></dc:creator>
            <pubDate>Thu, 30 Dec 2021 14:02:07 GMT</pubDate>
            <atom:updated>2022-05-23T11:32:54.831Z</atom:updated>
            <content:encoded><![CDATA[<p>You might have heard that goroutines are not the same as threads, but do you know what the differences are?</p><p><em>Note: The threads I mention in this post is the kernel threads which are most common type of thread.</em></p><p>First thing first, <strong><em>what is concurrency</em></strong>? Concurrency is being able to run other tasks even if a task is not completed. For example, while a concurrent app is waiting for input from the terminal, it can do another calculation. For Go, we use goroutines for concurrency. A goroutine waits for input while another goroutine does the calculations.</p><p>Note that concurrency is not <strong><em>parallelism</em></strong>. Parallelism means the ability to run more than one task at the same time. It is possible if you have more than one CPU. In concurrency, the apps switch between goroutines (or threads if you use a different language).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/947/1*VARgBCgx5x6BQy96879sxA.png" /><figcaption>Visual explanation of parallelism and concurrency</figcaption></figure><p>You can see green lines in the image. These lines are <strong><em>context switches</em></strong>, which means the moment when the app switch to run the other task.</p><p>For concurrency, we use threads in most programming languages. Go has a different mechanism instead. This mechanism is goroutines. Basically, both threads and goroutines have the same purpose; concurrency. However, goroutines are better in some points. Let’s check what they are.</p><h4>Speed</h4><p>While threads are <strong><em>managed by the kernel</em></strong>, goroutines are <strong><em>managed by go runtime</em></strong>. Since we don’t need kernel system calls for goroutines as much as threads, goroutines are faster.</p><h4>Memory Size</h4><p>Threads have fixed <strong><em>stack size</em></strong>. When your app is run, the operating system allocates a memory block for the stack. In order not to face problems about insufficient memory, the size of this block cannot be small.</p><p>On the other hand, the stack size of goroutines can grow or shrink. Therefore, go allocate a small memory block at the start. Whenever your app requires more, go runtime can allocate it. Hence, goroutines use way lesser memory than threads.</p><h4>Sharing Data</h4><p>It is one of the strongest points of go. Go uses <strong>channels</strong> to share data between goroutines. It is like a pipeline to send and receive data. Channels are concurrency safe. Therefore, you don’t need to deal with locking. If you coded in another concurrent programming language, you can see how important it is.</p><p>Threads use shared memory. It is the most important difference against processes. However, you have to use mutex, semaphore, etc. to avoid any problem as the opposite of goroutines.</p><h4>Managing</h4><p>As I mentioned in the speed part, goroutines are managed by go runtime. It means it doesn’t work differently in different OSs.</p><p>In the oldest versions of go, go runtime was creating a new thread for each goroutine. However, go runtime started to use the same thread for multiple goroutines after go 1.5. It means you might have thousands of goroutines in a few threads now. In each thread, there is a stack pointer which represents which goroutine is working now. In order to switch to another goroutine in the same thread, go runtime changes the value of this stack pointer.</p><p>When one of your goroutines is blocked (e.g. waiting for input), go runtime can rearrange your goroutines. If there are other goroutines in the same thread, they will move into another thread.</p><p>In summary, you think less because go runtime manages most of the work here.</p><p>As I wrote in the title, goroutines are not threads. They are faster, require less memory, and are easy to share data between each other. It makes go one of the best programming languages for concurrent jobs.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=958c53d0d7f0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Optimizing Memory by Changing the Order of Struct Field]]></title>
            <link>https://awstip.com/optimizing-memory-by-changing-the-order-of-struct-field-485106504087?source=rss-b850ff534905------2</link>
            <guid isPermaLink="false">https://medium.com/p/485106504087</guid>
            <category><![CDATA[struct]]></category>
            <category><![CDATA[optimization]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Uğur Kınık]]></dc:creator>
            <pubDate>Mon, 29 Nov 2021 15:26:49 GMT</pubDate>
            <atom:updated>2021-12-01T08:24:33.669Z</atom:updated>
            <content:encoded><![CDATA[<p>You can save some memory by changing the order of struct fields. I know it sounds weird, but let me explain it.</p><p>First, I want to demonstrate what I will explain. I created a small tool to visualize the memory for structs.</p><p>The tool creates an image that includes some blocks. Each block represents a byte in the memory and the numbers in these blocks show which field is stored in that block.</p><p>You don’t need to know more details about this tool, but if you want to learn more, you can check this link: <a href="https://github.com/kinix/struct-memory-visualization">https://github.com/kinix/struct-memory-visualization</a></p><p>Let’s start. I have two structs below. These structs have same fields, but these fields are in different order.</p><pre>type struct1 struct {<br>    num1  int64<br>    bool1 bool<br>    bool2 bool<br>    bool4 bool<br>    bool5 bool<br>    num2  int32<br>}</pre><pre>type struct2 struct {<br>    bool1 bool<br>    num1  int64<br>    bool2 bool<br>    bool4 bool<br>    num2  int32<br>    bool5 bool<br>}</pre><p>At first glance, you should have thought the sizes of these structs are the same. Let’s check with our visualization tool.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/472/1*Yb_PUS_2fwB5mJ-GsbYCYA.png" /><figcaption>struct1</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/472/1*9DR4b765UddHdgXpL9-pjQ.png" /><figcaption>struct2</figcaption></figure><p>While struct1 is 16 bytes, struct2 is 32 bytes. Why did Go add empty red blocks between our fields? Well, these red blocks are called <strong><em>padding</em></strong>. I will explain what is padding, but you need to learn something else first.</p><p>You might have noticed that each line consists of 8 bytes. The tool created these lines deliberately. CPUs read data from memory word by word. In a 64bit architecture, a <strong><em>word</em></strong> means an 8-byte memory block (4-byte for 32bit architecture).</p><p>When the CPU needs a byte, it has to read a full word even it doesn’t need other bytes in that word. On the other hand, if half of your field is in a word and the other half is in another word, the CPU needs to read two words to read that field. Therefore, the compiler needs to add some padding between fields to avoid dividing the field.</p><h4>What can you do to minimize the size of structs?</h4><p>If you have a struct that is used few times, this optimization is not that important for you. However, if your struct is used lots, you might avoid big memory usage.</p><p>You can calculate your struct fields and paddings, but it’s not easy to do for each struct. An easier option is sorting your fields by size. In addition, there are some tools like <a href="https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/fieldalignment">fieldalignment</a>, <a href="https://gitlab.com/opennota/check">check</a> for this optimization.</p><h4>BONUS TRICK: Optimize CPU usage</h4><p>As I mentioned, CPUs read data word by word. Assume that you have a big struct, but you usually use a few fields of that struct. Despite reading different words for each field, keeping these fields close helps your CPU to do less read operation.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=485106504087" width="1" height="1" alt=""><hr><p><a href="https://awstip.com/optimizing-memory-by-changing-the-order-of-struct-field-485106504087">Optimizing Memory by Changing the Order of Struct Field</a> was originally published in <a href="https://awstip.com/">AWS Tip</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Golang Basics: GOROOT, GOPATH]]></title>
            <link>https://medium.com/@ugurkinik/golang-basics-goroot-gopath-3f80063a08d8?source=rss-b850ff534905------2</link>
            <guid isPermaLink="false">https://medium.com/p/3f80063a08d8</guid>
            <category><![CDATA[goroot]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[gopath]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Uğur Kınık]]></dc:creator>
            <pubDate>Mon, 15 Nov 2021 17:04:36 GMT</pubDate>
            <atom:updated>2021-11-15T17:11:49.025Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ixw0s47neHTU_Dt9frfLAg.png" /><figcaption><a href="https://www.reddit.com/r/ProgrammerHumor/comments/98cv0x/i_am_goroot/">/r/ProgrammerHumor</a></figcaption></figure><p>If you are trying to learn Go, you must have seen GOROOT and GOPATH. In the newer versions of Go, you usually don’t need to set or change these variables. However, having information about how Go is working can help you in some of your future troubles.</p><p>First, I should tell you I haven’t written an article like that before, so don’t be harsh.</p><h3>GOROOT</h3><p>GOROOT is an environment variable that defines the location of your Go SDK. It means you can find the compiler, go tools or, standard libraries in this directory.</p><p>The default value of GOROOT is /usr/local/go. To check where is your GOROOT, you can use go env.</p><pre>go env GOROOT</pre><h3>GOPATH</h3><p>GOPATH is another environment variable. It defines the locations of your go source codes. If this variable includes more than one location, these locations are separated by columns (semicolon separated for Windows).</p><p>When your code has any imported package, this package has to be located here.</p><p>Like GOROOT, you can use go env to learn GOPATH value. The default value is $HOME/GO.</p><pre>go env GOPATH</pre><p>In your GOPATH, there are three folders:</p><ul><li><strong>src:</strong> Your source code has to be here if you don’t use go modules.</li><li><strong>pkg:</strong> Go packages that are used in your projects</li><li><strong>bin:</strong> Compiled executable programs</li></ul><p>Thanks to <strong>Go modules</strong>, which is a new method to manage dependencies, you don’t have to use $GOPATH/src for your projects since Go 1.11. However, pkg and bin folders are still used.</p><h3>Do I need to change them in any case?</h3><p>I don’t think you need to change GOPATH in any case. But, there are some situations in which you want to change GOROOT.</p><h4>1- Different installation folder:</h4><p>If you want to install Go into a different folder, you have to set this folder to your GOROOT. Otherwise, your go tools or codes won’t work.</p><h4>2- Use more than one version of Go:</h4><p>Let’s say you need to install two different versions of Go (Like 1.15 and 1.17). You installed them in two different folders, so what’s next? You should set GOROOT as the folder of which version you want to use. Then, you can set it as the other folder whenever you want to switch to the other version.</p><p>It is possible, yes. However, I strongly recommend you to use Docker or similar solutions in order to use different environments.</p><h3>Do I need to know these details?</h3><p>Actually, no.</p><p>There are some tools we don’t remember where they are until we break something. And we cannot find them when we need these tools. These basics are like these tools. You don’t need them now, but you can solve some of your future problems easily if you know them.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3f80063a08d8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>