<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Chapter 6 Functions, Defer, and Panic on Go: Under the Hood</title><link>https://golang.design/under-the-hood/en/part2lang/ch06func/</link><description>Recent content in Chapter 6 Functions, Defer, and Panic on Go: Under the Hood</description><generator>Hugo</generator><language>en</language><atom:link href="https://golang.design/under-the-hood/en/part2lang/ch06func/index.xml" rel="self" type="application/rss+xml"/><item><title>6.1 Function Calls</title><link>https://golang.design/under-the-hood/en/part2lang/ch06func/func/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://golang.design/under-the-hood/en/part2lang/ch06func/func/</guid><description>&lt;h1 id="61-function-calls"&gt;6.1 Function Calls&lt;/h1&gt;
&lt;p&gt;Functions are first-class citizens in Go: they can be assigned, passed, returned, and they can capture
outer variables to become closures. Behind this lie two implementation questions: how a function value
(a closure) is represented in memory, and how a single function call passes arguments and returns values
at the lowest level. The latter also hides a quiet but sweeping change introduced in Go 1.17: passing
arguments in registers instead of on the stack. This section explains both thoroughly, and at the seam
between them points out a key fact, the very fact that welds &amp;ldquo;what a closure is&amp;rdquo; and &amp;ldquo;how a call happens&amp;rdquo;
into one and the same thing.&lt;/p&gt;</description></item><item><title>6.2 Deferred Statements</title><link>https://golang.design/under-the-hood/en/part2lang/ch06func/defer/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://golang.design/under-the-hood/en/part2lang/ch06func/defer/</guid><description>&lt;h1 id="62-deferred-statements"&gt;6.2 Deferred Statements&lt;/h1&gt;
&lt;p&gt;The deferred statement &lt;code&gt;defer&lt;/code&gt; did not exist in the earliest Go designs; it was added later as a separate feature, with Robert Griesemer writing the language specification [Griesemer, 2009] and Ken Thompson producing the earliest implementation [Thompson, 2009]. Its semantics read very short: a &lt;code&gt;defer&lt;/code&gt;-red call runs when the enclosing function returns, when a panic occurs, or when &lt;code&gt;runtime.Goexit&lt;/code&gt; is called. Intuitively this looks like a purely compile-time feature, similar to C++&amp;rsquo;s RAII (automatic destruction when leaving a scope); the compiler would seem to only need to &amp;ldquo;move&amp;rdquo; the deferred call to the end of the function, with no runtime cost.&lt;/p&gt;</description></item><item><title>6.3 The panic and recover Builtins</title><link>https://golang.design/under-the-hood/en/part2lang/ch06func/panic/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://golang.design/under-the-hood/en/part2lang/ch06func/panic/</guid><description>&lt;h1 id="63-the-panic-and-recover-builtins"&gt;6.3 The panic and recover Builtins&lt;/h1&gt;
&lt;p&gt;&lt;code&gt;defer&lt;/code&gt; (&lt;a href=".././defer"&gt;6.2&lt;/a&gt;) has already settled the question of &amp;ldquo;what to do when a function exits,&amp;rdquo; leaving open the question of &amp;ldquo;what happens when a function is interrupted abnormally.&amp;rdquo; The pair of builtins &lt;code&gt;panic&lt;/code&gt; and &lt;code&gt;recover&lt;/code&gt; answers exactly this: the former interrupts the current normal control flow and begins unwinding up the call stack, while the latter catches it mid-unwind. This section first makes their semantics clear, then comes down to the go1.26 runtime implementation of &lt;code&gt;gopanic&lt;/code&gt; and &lt;code&gt;gorecover&lt;/code&gt;, and finally returns to a more pressing question: what panic should really be taken to be, and why Go did not make it into an exception mechanism like the one in C++ or Java.&lt;/p&gt;</description></item></channel></rss>