<?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 Fırat Yenidünya on Medium]]></title>
        <description><![CDATA[Stories by Fırat Yenidünya on Medium]]></description>
        <link>https://medium.com/@firatyenidunya?source=rss-93ee6b02b96------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*17ExV1kZ8IPXFHp6hGym5g.jpeg</url>
            <title>Stories by Fırat Yenidünya on Medium</title>
            <link>https://medium.com/@firatyenidunya?source=rss-93ee6b02b96------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 03:39:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@firatyenidunya/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[Defer: Swift vs Go]]></title>
            <link>https://medium.com/@firatyenidunya/defer-swift-vs-go-149d98c7278a?source=rss-93ee6b02b96------2</link>
            <guid isPermaLink="false">https://medium.com/p/149d98c7278a</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Fırat Yenidünya]]></dc:creator>
            <pubDate>Sat, 07 Jul 2018 12:09:51 GMT</pubDate>
            <atom:updated>2018-07-07T12:18:24.751Z</atom:updated>
            <content:encoded><![CDATA[<p>What’s defer, how to use it and examples are written in the documents as clearly as it gets.</p><p>Swift, <a href="https://docs.swift.org/swift-book/LanguageGuide/ErrorHandling.html">https://docs.swift.org/swift-book/LanguageGuide/ErrorHandling.html</a> — Specifying Cleanup Actions</p><p>Go, <a href="https://blog.golang.org/defer-panic-and-recover">https://blog.golang.org/defer-panic-and-recover</a></p><p>For a long time I have been working with swift, but these days I am learning go lang and I noticed that defer has different styles in execution.</p><p><strong>How?</strong></p><p>Let me quickly show you two code examples, their outputs and reasons.</p><p>Defer Go Example,</p><blockquote>func deferTest() {<br> for i := 0; i &lt; 5; i++ {<br> defer fmt.Println(i)<br> }<br>}</blockquote><p>Output,</p><p><strong>4<br>3<br>2<br>1<br>0</strong></p><p>Defer Swift Example,</p><blockquote>func deferTest(){<br> for index in 0..&lt;5{<br> defer{<br> print(index)<br> }<br> }<br>}</blockquote><p>Output,</p><p><strong>0<br>1<br>2<br>3<br>4</strong></p><p>So what is the difference between these examples? In two languages defer using LIFO structure. So as a sample,</p><p>Go,</p><blockquote>func deferTest() {<br> defer fmt.Println(“1”)<br> defer fmt.Println(“2”)<br>}</blockquote><p>Output,</p><p><strong>2<br>1</strong></p><p>Swift,</p><blockquote>func deferTest(){<br> defer{<br> print(“1”)<br> }<br> defer{<br> print(“2”)<br> }<br>}</blockquote><p>Output,</p><p><strong>2<br>1</strong></p><p>As you can see outputs are the same. So why do you think the first two example outputs were not the same? To answer that is again about the documents. So shortly for go lang to work with defer we are waiting to functions to end and for swift we are waiting end of the scope. So that’s why two outputs are different.</p><p>So going back to docs again, for what is said for defer in go,</p><p><strong>“The list of saved calls is executed after the surrounding function returns.”</strong></p><p>And what’s said for defer in swift,</p><p><strong>“You use a defer statement to execute a set of statements just before code execution leaves the current block of code.”</strong></p><p>I hope to be helpful. Thank you for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=149d98c7278a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>