<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>I/O on 100 Exercises to Learn Go</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/12_io/</link><description>Recent content in I/O on 100 Exercises to Learn Go</description><generator>Hugo</generator><language>en</language><atom:link href="https://hamzahraihan.github.io/100-exercises-to-learn-go/12_io/index.xml" rel="self" type="application/rss+xml"/><item><title>Reading Everything</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/12_io/reader/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/12_io/reader/</guid><description>&lt;h1 id="reading-everything"&gt;Reading Everything&lt;a class="anchor" href="#reading-everything"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Files, network connections, request bodies, decompressors — wildly
different sources, one shared shape. Go funnels them all through an
interface with a single method, and this exercise drains such a stream to
its end.&lt;/p&gt;
&lt;h2 id="the-one-method-contract"&gt;The one-method contract&lt;a class="anchor" href="#the-one-method-contract"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// The entire io.Reader contract:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;type&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Reader&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;interface&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;Read&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;p&lt;/span&gt; []&lt;span style="color:#66d9ef"&gt;byte&lt;/span&gt;) (&lt;span style="color:#a6e22e"&gt;n&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;err&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;error&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;Read&lt;/code&gt; fills your buffer &lt;code&gt;p&lt;/code&gt; and reports how many bytes it took. Call it
repeatedly and the stream empties chunk by chunk — the final call reports
&lt;code&gt;io.EOF&lt;/code&gt; instead of data. Note the philosophy: end-of-stream arrives as an
&lt;em&gt;error value&lt;/em&gt;, not an exception, not a boolean. &lt;code&gt;EOF&lt;/code&gt; isn&amp;rsquo;t failure; it&amp;rsquo;s
the stream&amp;rsquo;s last word, and every reader loop treats it as the exit sign
rather than a problem. Helpers like the one below swallow it silently so
you rarely touch it directly.&lt;/p&gt;</description></item><item><title>Writing Lines</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/12_io/writer/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/12_io/writer/</guid><description>&lt;h1 id="writing-lines"&gt;Writing Lines&lt;a class="anchor" href="#writing-lines"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Readers drain; writers fill. The mirror interface takes your bytes and
reports what it accepted — and the gap between &amp;ldquo;accepted&amp;rdquo; and &amp;ldquo;all of it&amp;rdquo;
is where careful writers earn their keep.&lt;/p&gt;
&lt;h2 id="the-mirror-contract"&gt;The mirror contract&lt;a class="anchor" href="#the-mirror-contract"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// The entire io.Writer contract:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;type&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Writer&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;interface&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;Write&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;p&lt;/span&gt; []&lt;span style="color:#66d9ef"&gt;byte&lt;/span&gt;) (&lt;span style="color:#a6e22e"&gt;n&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;err&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;error&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;Write&lt;/code&gt; accepts up to &lt;code&gt;len(p)&lt;/code&gt; bytes and reports how many landed. A
&lt;code&gt;nil&lt;/code&gt; error with &lt;code&gt;n &amp;lt; len(p)&lt;/code&gt; is legal — short writes happen on sockets
and pipes — which is why robust code loops or delegates rather than
assuming full acceptance. For this exercise the writes are small and local,
so one call per line suffices; the &lt;em&gt;shape&lt;/em&gt; still returns the first error
met:&lt;/p&gt;</description></item><item><title>Scanning Words</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/12_io/scanner/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/12_io/scanner/</guid><description>&lt;h1 id="scanning-words"&gt;Scanning Words&lt;a class="anchor" href="#scanning-words"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Reading everything works until the input doesn&amp;rsquo;t fit the plan — a
two-gigabyte log, a never-ending pipe. Then &amp;ldquo;drain it all&amp;rdquo; becomes the
bug, and the fix is processing &lt;em&gt;pieces&lt;/em&gt;: lines, words, tokens, one at a
time. Doing that by hand means buffers, partial tokens, and refill logic.
&lt;code&gt;bufio.Scanner&lt;/code&gt; means not doing any of it.&lt;/p&gt;
&lt;h2 id="tokens-without-bookkeeping"&gt;Tokens without bookkeeping&lt;a class="anchor" href="#tokens-without-bookkeeping"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;func&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;WordCount&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;r&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;io&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;Reader&lt;/span&gt;) (&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt;, &lt;span style="color:#66d9ef"&gt;error&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;sc&lt;/span&gt; &lt;span style="color:#f92672"&gt;:=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;bufio&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;NewScanner&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;r&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;sc&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;Split&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;bufio&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;ScanWords&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;count&lt;/span&gt; &lt;span style="color:#f92672"&gt;:=&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;for&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;sc&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;Scan&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;count&lt;/span&gt;&lt;span style="color:#f92672"&gt;++&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;count&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;sc&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;Err&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Three moves. Wrap the reader in a &lt;code&gt;Scanner&lt;/code&gt; — the &lt;code&gt;bufio&lt;/code&gt; package owns the
buffer from here. Choose the token shape with a &lt;strong&gt;split function&lt;/strong&gt;:
&lt;code&gt;ScanWords&lt;/code&gt; for whitespace-separated words (cousin to &lt;code&gt;strings.Fields&lt;/code&gt;,
streaming), &lt;code&gt;ScanLines&lt;/code&gt; the default for lines, custom functions for
anything stranger. Then &lt;code&gt;Scan()&lt;/code&gt; advances token by token, &lt;code&gt;false&lt;/code&gt; when done,
&lt;code&gt;Text()&lt;/code&gt; (or &lt;code&gt;Bytes()&lt;/code&gt;) revealing each piece.&lt;/p&gt;</description></item><item><title>Saving Tickets to Disk</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/12_io/file/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/12_io/file/</guid><description>&lt;h1 id="saving-tickets-to-disk"&gt;Saving Tickets to Disk&lt;a class="anchor" href="#saving-tickets-to-disk"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Streams and scanners handle flow; sometimes you just want the whole file,
right now, in memory. The &lt;code&gt;os&lt;/code&gt; package reduces that to two calls — and
this exercise pairs them into the oldest reliable pattern in storage: save,
then load back, then compare.&lt;/p&gt;
&lt;h2 id="disk-in-two-calls"&gt;Disk in two calls&lt;a class="anchor" href="#disk-in-two-calls"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Save: path, bytes, permissions&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;err&lt;/span&gt; &lt;span style="color:#f92672"&gt;:=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;os&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;WriteFile&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;path&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;data&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;&lt;span style="color:#a6e22e"&gt;o644&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Load: path back into bytes&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;got&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;err&lt;/span&gt; &lt;span style="color:#f92672"&gt;:=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;os&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;ReadFile&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;path&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;os.WriteFile&lt;/code&gt; creates or truncates-then-writes in one step; &lt;code&gt;os.ReadFile&lt;/code&gt;
slurps the entire content back. Small configs, JSON tickets, test fixtures
— whole-file operations cover an enormous share of real file I/O, and both
directions fit on one line each. (&lt;code&gt;LoadTicket&lt;/code&gt; here is already written;
your half is the save.)&lt;/p&gt;</description></item><item><title>Copying Streams</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/12_io/copy/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/12_io/copy/</guid><description>&lt;h1 id="copying-streams"&gt;Copying Streams&lt;a class="anchor" href="#copying-streams"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;&lt;code&gt;io.ReadAll&lt;/code&gt; slurps everything into memory — perfect for configs, fatal
for gigabytes. But moving bytes from a reader to a writer shouldn&amp;rsquo;t
require holding them: the data passes &lt;em&gt;through&lt;/em&gt;, and memory should stay
flat no matter the size. &lt;code&gt;io.Copy&lt;/code&gt; is that idea, pre-written and
battle-hardened.&lt;/p&gt;
&lt;h2 id="never-hold-it-all"&gt;Never hold it all&lt;a class="anchor" href="#never-hold-it-all"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;func&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;CopyN&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;dst&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;io&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;Writer&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;src&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;io&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;Reader&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;n&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;int64&lt;/span&gt;) (&lt;span style="color:#66d9ef"&gt;int64&lt;/span&gt;, &lt;span style="color:#66d9ef"&gt;error&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;io&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;CopyN&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;dst&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;src&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;n&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;io.CopyN&lt;/code&gt; shuttles exactly &lt;code&gt;n&lt;/code&gt; bytes from source to destination in
chunks, returning the count moved. The test moves 5 bytes of &lt;code&gt;&amp;quot;hello world&amp;quot;&lt;/code&gt; and asserts all three outcomes — count 5, buffer &lt;code&gt;&amp;quot;hello&amp;quot;&lt;/code&gt;, error
nil. Nothing larger than a chunk ever resides in memory: terabyte inputs
cost kilobytes of RAM. Its sibling &lt;code&gt;io.Copy&lt;/code&gt; runs until &lt;code&gt;EOF&lt;/code&gt; with no
limit; same streaming, open-ended.&lt;/p&gt;</description></item><item><title>Ticket Logs (JSON Lines)</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/12_io/jsonlog/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/12_io/jsonlog/</guid><description>&lt;h1 id="ticket-logs-json-lines"&gt;Ticket Logs (JSON Lines)&lt;a class="anchor" href="#ticket-logs-json-lines"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;A ticket store that forgets on restart is a demo, not a system. This
exercise — the section&amp;rsquo;s capstone — persists tickets durably in the
simplest format that works: &lt;strong&gt;JSON lines&lt;/strong&gt;, one object per line, appended
forever and read back in order.&lt;/p&gt;
&lt;h2 id="the-format-that-appends"&gt;The format that appends&lt;a class="anchor" href="#the-format-that-appends"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-json" data-lang="json"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&lt;span style="color:#f92672"&gt;&amp;#34;id&amp;#34;&lt;/span&gt;:&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;,&lt;span style="color:#f92672"&gt;&amp;#34;title&amp;#34;&lt;/span&gt;:&lt;span style="color:#e6db74"&gt;&amp;#34;a&amp;#34;&lt;/span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&lt;span style="color:#f92672"&gt;&amp;#34;id&amp;#34;&lt;/span&gt;:&lt;span style="color:#ae81ff"&gt;2&lt;/span&gt;,&lt;span style="color:#f92672"&gt;&amp;#34;title&amp;#34;&lt;/span&gt;:&lt;span style="color:#e6db74"&gt;&amp;#34;b&amp;#34;&lt;/span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Each line is a complete, independent JSON document. Writers append without
reading or rewriting anything; readers decode incrementally, line by line.
Corruption in one line can&amp;rsquo;t cascade into its neighbors; &lt;code&gt;grep&lt;/code&gt; works on
the log; humans can read it raw. Compared against a single JSON array,
JSON lines trades pretty-printing for appendability — and for logs,
appendability wins every argument.&lt;/p&gt;</description></item><item><title>Structured Logging with slog</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/12_io/slog/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/12_io/slog/</guid><description>&lt;h1 id="structured-logging-with-slog"&gt;Structured Logging with slog&lt;a class="anchor" href="#structured-logging-with-slog"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;&lt;code&gt;fmt.Fprintf(os.Stderr, &amp;quot;error: %v\n&amp;quot;, err)&lt;/code&gt; is honest but unstructured — a log pipeline can&amp;rsquo;t parse it, filter by severity, or extract fields. &lt;code&gt;log/slog&lt;/code&gt; (Go 1.21+) is the stdlib answer: structured, levelled, with JSON output built in.&lt;/p&gt;
&lt;h2 id="concept-1--the-three-layer-model"&gt;Concept 1 — The three-layer model&lt;a class="anchor" href="#concept-1--the-three-layer-model"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;handler&lt;/span&gt; &lt;span style="color:#f92672"&gt;:=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;slog&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;NewJSONHandler&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;w&lt;/span&gt;, &lt;span style="color:#f92672"&gt;&amp;amp;&lt;/span&gt;&lt;span style="color:#a6e22e"&gt;slog&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;HandlerOptions&lt;/span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;Level&lt;/span&gt;: &lt;span style="color:#a6e22e"&gt;slog&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;LevelInfo&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;})
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;logger&lt;/span&gt; &lt;span style="color:#f92672"&gt;:=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;slog&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;New&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;handler&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;Handler&lt;/code&gt; decides where and how to write (JSON to &lt;code&gt;w&lt;/code&gt;, minimum level). &lt;code&gt;Logger&lt;/code&gt; is the API you call. They compose: swap &lt;code&gt;NewJSONHandler&lt;/code&gt; for &lt;code&gt;NewTextHandler&lt;/code&gt; and the logger API is identical.&lt;/p&gt;</description></item></channel></rss>