<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Chapter 3 The Life of a Program on Go: Under the Hood</title><link>https://golang.design/under-the-hood/en/part1overview/ch03life/</link><description>Recent content in Chapter 3 The Life of a Program on Go: Under the Hood</description><generator>Hugo</generator><language>en</language><atom:link href="https://golang.design/under-the-hood/en/part1overview/ch03life/index.xml" rel="self" type="application/rss+xml"/><item><title>3.1 Starting from the `go` Command</title><link>https://golang.design/under-the-hood/en/part1overview/ch03life/cmd/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://golang.design/under-the-hood/en/part1overview/ch03life/cmd/</guid><description>&lt;h1 id="31-starting-from-the-go-command"&gt;3.1 Starting from the &lt;code&gt;go&lt;/code&gt; Command&lt;/h1&gt;
&lt;p&gt;The life cycle of a Go program begins with running the &lt;code&gt;go&lt;/code&gt; command. The &lt;code&gt;go build&lt;/code&gt;, &lt;code&gt;go test&lt;/code&gt;,
and &lt;code&gt;go run&lt;/code&gt; that readers type every day look like they merely &amp;ldquo;turn source into a binary,&amp;rdquo; but
behind them hides a fact that is often misunderstood: &lt;code&gt;go&lt;/code&gt; itself is not the compiler. It is a
&lt;strong&gt;build orchestrator&lt;/strong&gt;, responsible for breaking a single build into many individual steps,
arranging their ordering and parallelism, and then invoking the real tools one by one: the
compiler &lt;code&gt;compile&lt;/code&gt; (&lt;a href=".././compile"&gt;3.2&lt;/a&gt;), the assembler &lt;code&gt;asm&lt;/code&gt;, and the linker &lt;code&gt;link&lt;/code&gt;
(&lt;a href=".././link"&gt;3.4&lt;/a&gt;). This section first makes this orchestration relationship clear, as it is the
master outline for the sections that follow: once you understand how &lt;code&gt;go&lt;/code&gt; decomposes a build into
a graph and how it uses a content-addressed cache to avoid duplicate work, then turning to the
details of compilation and linking gives you a frame to stand on.&lt;/p&gt;</description></item><item><title>3.2 The Go Compilation Pipeline</title><link>https://golang.design/under-the-hood/en/part1overview/ch03life/compile/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://golang.design/under-the-hood/en/part1overview/ch03life/compile/</guid><description>&lt;h1 id="32-the-go-compilation-pipeline"&gt;3.2 The Go Compilation Pipeline&lt;/h1&gt;
&lt;p&gt;&lt;a href=".././cmd"&gt;3.1&lt;/a&gt; explained that the real work behind &lt;code&gt;go build&lt;/code&gt; is carried out by two programs, &lt;code&gt;compile&lt;/code&gt; and &lt;code&gt;link&lt;/code&gt;. This section zooms in on &lt;code&gt;compile&lt;/code&gt; and traces the full journey from a single &lt;code&gt;.go&lt;/code&gt; source file to a single &lt;code&gt;.o&lt;/code&gt; object file: what each stage receives, what it produces, and why the work is cut up this way. This section is a &lt;strong&gt;panorama&lt;/strong&gt; of the compilation pipeline. The inner workings of each stage (how the grammar is designed, what algorithm type checking uses, the optimization rules of SSA) are left to &lt;a href="../../../part5toolchain/ch15compile/readme"&gt;Chapter 15&lt;/a&gt; to unfold one by one. Here we only string them into a single line and point out two threads that run through all of it.&lt;/p&gt;</description></item><item><title>3.3 Bootstrapping the Language</title><link>https://golang.design/under-the-hood/en/part1overview/ch03life/bootstrap/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://golang.design/under-the-hood/en/part1overview/ch03life/bootstrap/</guid><description>&lt;h1 id="33-bootstrapping-the-language"&gt;3.3 Bootstrapping the Language&lt;/h1&gt;
&lt;p&gt;One question sounds like a paradox: Go&amp;rsquo;s compiler, assembler, linker, and runtime are all written in Go today, so where did the first program capable of compiling Go come from? Without a Go compiler, how do you compile a Go compiler? This is &lt;strong&gt;bootstrapping&lt;/strong&gt;. It is both the classic chicken-and-egg dilemma and a piece of engineering in the history of the Go toolchain that can be retold precisely. This section answers three things: how this egg was first hatched; how, after bootstrapping, the chain for building a new version of Go works, and why the demands it places on the bootstrap version keep rising year over year; and finally, why a language that &amp;ldquo;implements itself with itself&amp;rdquo; deserves to be taken seriously.&lt;/p&gt;</description></item><item><title>3.4 Module Linking</title><link>https://golang.design/under-the-hood/en/part1overview/ch03life/link/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://golang.design/under-the-hood/en/part1overview/ch03life/link/</guid><description>&lt;h1 id="34-module-linking"&gt;3.4 Module Linking&lt;/h1&gt;
&lt;p&gt;The compiler (&lt;a href=".././compile"&gt;3.2&lt;/a&gt;) turns each package into an object file, but an object file cannot run on its own. The object files still reference one another&amp;rsquo;s functions and variables, addresses are not yet fixed, and the runtime support is still missing. Assembling these fragments into a complete program that can be loaded and executed is the job of the &lt;strong&gt;linker&lt;/strong&gt; (&lt;code&gt;cmd/link&lt;/code&gt;, usually invoked by &lt;code&gt;go build&lt;/code&gt; as &lt;code&gt;go tool link&lt;/code&gt;). This section looks at what the linker does, and at a few choices Go makes around linking that together explain why a Go program is so often a self-contained file that &amp;ldquo;just runs once you copy it over.&amp;rdquo;&lt;/p&gt;</description></item><item><title>3.5 Bootstrapping a Go Program</title><link>https://golang.design/under-the-hood/en/part1overview/ch03life/boot/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://golang.design/under-the-hood/en/part1overview/ch03life/boot/</guid><description>&lt;h1 id="35-bootstrapping-a-go-program"&gt;3.5 Bootstrapping a Go Program&lt;/h1&gt;
&lt;p&gt;The &lt;code&gt;main&lt;/code&gt; function the reader writes is not the program&amp;rsquo;s true first instruction. When the
operating system hands control to a Go executable, what starts running is the runtime: it has
to lay out the execution stack on the main thread, bind thread-local storage, measure the CPU
core count and the physical page size of memory, wake up the memory allocator, the garbage
collector, and the scheduler one by one, and only then create the goroutine that carries &lt;code&gt;main&lt;/code&gt;
and hand it to the scheduling loop to run. In other words, a Go binary carries within it a
miniature operating system (&lt;a href="../../ch01intro/go"&gt;1.2&lt;/a&gt;), one that starts itself up ahead of user
code. This section walks the bootstrap chain from end to end, from the operating system&amp;rsquo;s entry
symbol to the moment the first goroutine is scheduled, so we can see clearly what happens
&amp;ldquo;before &lt;code&gt;main&lt;/code&gt;.&amp;rdquo;&lt;/p&gt;</description></item><item><title>3.6 The Life and Death of the Main Goroutine</title><link>https://golang.design/under-the-hood/en/part1overview/ch03life/main/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://golang.design/under-the-hood/en/part1overview/ch03life/main/</guid><description>&lt;h1 id="36-the-life-and-death-of-the-main-goroutine"&gt;3.6 The Life and Death of the Main Goroutine&lt;/h1&gt;
&lt;p&gt;In &lt;a href=".././boot"&gt;3.5&lt;/a&gt;, once &lt;code&gt;schedinit&lt;/code&gt; has assembled the runtime&amp;rsquo;s foundations, it does not call &lt;code&gt;runtime.main&lt;/code&gt; directly. Instead it pushes that function&amp;rsquo;s entry address onto the stack, hands it to &lt;code&gt;newproc&lt;/code&gt; to fabricate the first Goroutine, and then lets &lt;code&gt;mstart&lt;/code&gt; start the scheduling loop and pick this Goroutine out to run. The details of scheduling are left for &lt;a href="https://golang.design/under-the-hood/en/part3concurrency/ch09sched/"&gt;9 Scheduler&lt;/a&gt;. This section trains the camera on a single moment: &lt;strong&gt;the first Goroutine is already running, and it is about to execute &lt;code&gt;runtime.main&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;</description></item></channel></rss>