diff options
author | Akshay <[email protected]> | 2020-09-01 08:19:49 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2020-09-01 08:19:49 +0100 |
commit | c9c8db0f308eb5dcb552d15ecfd403d623dc40de (patch) | |
tree | e88293873d5480fe2a932ade77cb9b610c53d3f4 /docs/posts/gripes_with_go | |
parent | 70827fe44d6844528e7dd56637de2f0f3e0cf847 (diff) |
new post: nixos
Diffstat (limited to 'docs/posts/gripes_with_go')
-rw-r--r-- | docs/posts/gripes_with_go/index.html | 140 |
1 files changed, 70 insertions, 70 deletions
diff --git a/docs/posts/gripes_with_go/index.html b/docs/posts/gripes_with_go/index.html index b527b4a..4d37038 100644 --- a/docs/posts/gripes_with_go/index.html +++ b/docs/posts/gripes_with_go/index.html | |||
@@ -50,101 +50,101 @@ | |||
50 | <h3 id="lack-of-sum-types">Lack of Sum types</h3> | 50 | <h3 id="lack-of-sum-types">Lack of Sum types</h3> |
51 | <p>A “Sum” type is a data type that can hold one of many states at a given time, similar to how a boolean can hold a true or a false, not too different from an <code>enum</code> type in C. Go lacks <code>enum</code> types unfortunately, and you are forced to resort to crafting your own substitute.</p> | 51 | <p>A “Sum” type is a data type that can hold one of many states at a given time, similar to how a boolean can hold a true or a false, not too different from an <code>enum</code> type in C. Go lacks <code>enum</code> types unfortunately, and you are forced to resort to crafting your own substitute.</p> |
52 | <p>A type to represent gender for example:</p> | 52 | <p>A type to represent gender for example:</p> |
53 | <div class="sourceCode" id="cb1"><pre class="sourceCode go"><code class="sourceCode go"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true"></a><span class="kw">type</span> Gender <span class="dt">int</span></span> | 53 | <div class="sourceCode" id="cb1"><pre class="sourceCode go"><code class="sourceCode go"><span id="cb1-1"><a href="#cb1-1"></a><span class="kw">type</span> Gender <span class="dt">int</span></span> |
54 | <span id="cb1-2"><a href="#cb1-2" aria-hidden="true"></a></span> | 54 | <span id="cb1-2"><a href="#cb1-2"></a></span> |
55 | <span id="cb1-3"><a href="#cb1-3" aria-hidden="true"></a><span class="kw">const</span> (</span> | 55 | <span id="cb1-3"><a href="#cb1-3"></a><span class="kw">const</span> (</span> |
56 | <span id="cb1-4"><a href="#cb1-4" aria-hidden="true"></a> Male Gender = <span class="ot">iota</span> <span class="co">// assigns Male to 0</span></span> | 56 | <span id="cb1-4"><a href="#cb1-4"></a> Male Gender = <span class="ot">iota</span> <span class="co">// assigns Male to 0</span></span> |
57 | <span id="cb1-5"><a href="#cb1-5" aria-hidden="true"></a> Female <span class="co">// assigns Female to 1</span></span> | 57 | <span id="cb1-5"><a href="#cb1-5"></a> Female <span class="co">// assigns Female to 1</span></span> |
58 | <span id="cb1-6"><a href="#cb1-6" aria-hidden="true"></a> Other <span class="co">// assigns Other to 2</span></span> | 58 | <span id="cb1-6"><a href="#cb1-6"></a> Other <span class="co">// assigns Other to 2</span></span> |
59 | <span id="cb1-7"><a href="#cb1-7" aria-hidden="true"></a>)</span> | 59 | <span id="cb1-7"><a href="#cb1-7"></a>)</span> |
60 | <span id="cb1-8"><a href="#cb1-8" aria-hidden="true"></a></span> | 60 | <span id="cb1-8"><a href="#cb1-8"></a></span> |
61 | <span id="cb1-9"><a href="#cb1-9" aria-hidden="true"></a>fmt.Println(<span class="st">"My gender is "</span>, Male)</span> | 61 | <span id="cb1-9"><a href="#cb1-9"></a>fmt.Println(<span class="st">"My gender is "</span>, Male)</span> |
62 | <span id="cb1-10"><a href="#cb1-10" aria-hidden="true"></a><span class="co">// My gender is 0</span></span> | 62 | <span id="cb1-10"><a href="#cb1-10"></a><span class="co">// My gender is 0</span></span> |
63 | <span id="cb1-11"><a href="#cb1-11" aria-hidden="true"></a><span class="co">// Oops! We have to implement String() for Gender ...</span></span> | 63 | <span id="cb1-11"><a href="#cb1-11"></a><span class="co">// Oops! We have to implement String() for Gender ...</span></span> |
64 | <span id="cb1-12"><a href="#cb1-12" aria-hidden="true"></a></span> | 64 | <span id="cb1-12"><a href="#cb1-12"></a></span> |
65 | <span id="cb1-13"><a href="#cb1-13" aria-hidden="true"></a><span class="kw">func</span> (g Gender) String() <span class="dt">string</span> {</span> | 65 | <span id="cb1-13"><a href="#cb1-13"></a><span class="kw">func</span> (g Gender) String() <span class="dt">string</span> {</span> |
66 | <span id="cb1-14"><a href="#cb1-14" aria-hidden="true"></a> <span class="kw">switch</span> (g) {</span> | 66 | <span id="cb1-14"><a href="#cb1-14"></a> <span class="kw">switch</span> (g) {</span> |
67 | <span id="cb1-15"><a href="#cb1-15" aria-hidden="true"></a> <span class="kw">case</span> <span class="dv">0</span>: <span class="kw">return</span> <span class="st">"Male"</span></span> | 67 | <span id="cb1-15"><a href="#cb1-15"></a> <span class="kw">case</span> <span class="dv">0</span>: <span class="kw">return</span> <span class="st">"Male"</span></span> |
68 | <span id="cb1-16"><a href="#cb1-16" aria-hidden="true"></a> <span class="kw">case</span> <span class="dv">1</span>: <span class="kw">return</span> <span class="st">"Female"</span></span> | 68 | <span id="cb1-16"><a href="#cb1-16"></a> <span class="kw">case</span> <span class="dv">1</span>: <span class="kw">return</span> <span class="st">"Female"</span></span> |
69 | <span id="cb1-17"><a href="#cb1-17" aria-hidden="true"></a> <span class="kw">default</span>: <span class="kw">return</span> <span class="st">"Other"</span></span> | 69 | <span id="cb1-17"><a href="#cb1-17"></a> <span class="kw">default</span>: <span class="kw">return</span> <span class="st">"Other"</span></span> |
70 | <span id="cb1-18"><a href="#cb1-18" aria-hidden="true"></a> }</span> | 70 | <span id="cb1-18"><a href="#cb1-18"></a> }</span> |
71 | <span id="cb1-19"><a href="#cb1-19" aria-hidden="true"></a>}</span> | 71 | <span id="cb1-19"><a href="#cb1-19"></a>}</span> |
72 | <span id="cb1-20"><a href="#cb1-20" aria-hidden="true"></a></span> | 72 | <span id="cb1-20"><a href="#cb1-20"></a></span> |
73 | <span id="cb1-21"><a href="#cb1-21" aria-hidden="true"></a><span class="co">// You can accidentally do stupid stuff like:</span></span> | 73 | <span id="cb1-21"><a href="#cb1-21"></a><span class="co">// You can accidentally do stupid stuff like:</span></span> |
74 | <span id="cb1-22"><a href="#cb1-22" aria-hidden="true"></a>gender := Male + <span class="dv">1</span></span></code></pre></div> | 74 | <span id="cb1-22"><a href="#cb1-22"></a>gender := Male + <span class="dv">1</span></span></code></pre></div> |
75 | <p>The Haskell equivalent of the same:</p> | 75 | <p>The Haskell equivalent of the same:</p> |
76 | <div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true"></a><span class="kw">data</span> <span class="dt">Gender</span> <span class="ot">=</span> <span class="dt">Male</span></span> | 76 | <div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb2-1"><a href="#cb2-1"></a><span class="kw">data</span> <span class="dt">Gender</span> <span class="ot">=</span> <span class="dt">Male</span></span> |
77 | <span id="cb2-2"><a href="#cb2-2" aria-hidden="true"></a> <span class="op">|</span> <span class="dt">Female</span></span> | 77 | <span id="cb2-2"><a href="#cb2-2"></a> <span class="op">|</span> <span class="dt">Female</span></span> |
78 | <span id="cb2-3"><a href="#cb2-3" aria-hidden="true"></a> <span class="op">|</span> <span class="dt">Other</span></span> | 78 | <span id="cb2-3"><a href="#cb2-3"></a> <span class="op">|</span> <span class="dt">Other</span></span> |
79 | <span id="cb2-4"><a href="#cb2-4" aria-hidden="true"></a> <span class="kw">deriving</span> (<span class="dt">Show</span>)</span> | 79 | <span id="cb2-4"><a href="#cb2-4"></a> <span class="kw">deriving</span> (<span class="dt">Show</span>)</span> |
80 | <span id="cb2-5"><a href="#cb2-5" aria-hidden="true"></a></span> | 80 | <span id="cb2-5"><a href="#cb2-5"></a></span> |
81 | <span id="cb2-6"><a href="#cb2-6" aria-hidden="true"></a><span class="fu">print</span> <span class="op">$</span> <span class="st">"My gender is "</span> <span class="op">++</span> (<span class="fu">show</span> <span class="dt">Male</span>)</span></code></pre></div> | 81 | <span id="cb2-6"><a href="#cb2-6"></a><span class="fu">print</span> <span class="op">$</span> <span class="st">"My gender is "</span> <span class="op">++</span> (<span class="fu">show</span> <span class="dt">Male</span>)</span></code></pre></div> |
82 | <h3 id="type-assertions">Type Assertions</h3> | 82 | <h3 id="type-assertions">Type Assertions</h3> |
83 | <p>A downcast with an optional error check? What could go wrong?</p> | 83 | <p>A downcast with an optional error check? What could go wrong?</p> |
84 | <p>Type assertions in Go allow you to do:</p> | 84 | <p>Type assertions in Go allow you to do:</p> |
85 | <div class="sourceCode" id="cb3"><pre class="sourceCode go"><code class="sourceCode go"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true"></a><span class="kw">var</span> x <span class="kw">interface</span>{} = <span class="dv">7</span></span> | 85 | <div class="sourceCode" id="cb3"><pre class="sourceCode go"><code class="sourceCode go"><span id="cb3-1"><a href="#cb3-1"></a><span class="kw">var</span> x <span class="kw">interface</span>{} = <span class="dv">7</span></span> |
86 | <span id="cb3-2"><a href="#cb3-2" aria-hidden="true"></a>y, goodToGo := x.(<span class="dt">int</span>)</span> | 86 | <span id="cb3-2"><a href="#cb3-2"></a>y, goodToGo := x.(<span class="dt">int</span>)</span> |
87 | <span id="cb3-3"><a href="#cb3-3" aria-hidden="true"></a><span class="kw">if</span> goodToGo {</span> | 87 | <span id="cb3-3"><a href="#cb3-3"></a><span class="kw">if</span> goodToGo {</span> |
88 | <span id="cb3-4"><a href="#cb3-4" aria-hidden="true"></a> fmt.Println(y)</span> | 88 | <span id="cb3-4"><a href="#cb3-4"></a> fmt.Println(y)</span> |
89 | <span id="cb3-5"><a href="#cb3-5" aria-hidden="true"></a>}</span></code></pre></div> | 89 | <span id="cb3-5"><a href="#cb3-5"></a>}</span></code></pre></div> |
90 | <p>The error check however is optional:</p> | 90 | <p>The error check however is optional:</p> |
91 | <div class="sourceCode" id="cb4"><pre class="sourceCode go"><code class="sourceCode go"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true"></a><span class="kw">var</span> x <span class="kw">interface</span>{} = <span class="dv">7</span></span> | 91 | <div class="sourceCode" id="cb4"><pre class="sourceCode go"><code class="sourceCode go"><span id="cb4-1"><a href="#cb4-1"></a><span class="kw">var</span> x <span class="kw">interface</span>{} = <span class="dv">7</span></span> |
92 | <span id="cb4-2"><a href="#cb4-2" aria-hidden="true"></a><span class="kw">var</span> y := x.(<span class="dt">float64</span>)</span> | 92 | <span id="cb4-2"><a href="#cb4-2"></a><span class="kw">var</span> y := x.(<span class="dt">float64</span>)</span> |
93 | <span id="cb4-3"><a href="#cb4-3" aria-hidden="true"></a>fmt.Println(y)</span> | 93 | <span id="cb4-3"><a href="#cb4-3"></a>fmt.Println(y)</span> |
94 | <span id="cb4-4"><a href="#cb4-4" aria-hidden="true"></a><span class="co">// results in a runtime error:</span></span> | 94 | <span id="cb4-4"><a href="#cb4-4"></a><span class="co">// results in a runtime error:</span></span> |
95 | <span id="cb4-5"><a href="#cb4-5" aria-hidden="true"></a><span class="co">// panic: interface conversion: interface {} is int, not float64</span></span></code></pre></div> | 95 | <span id="cb4-5"><a href="#cb4-5"></a><span class="co">// panic: interface conversion: interface {} is int, not float64</span></span></code></pre></div> |
96 | <h3 id="date-and-time">Date and Time</h3> | 96 | <h3 id="date-and-time">Date and Time</h3> |
97 | <p>Anyone that has written Go previously, will probably already know what I am getting at here. For the uninitiated, parsing and formatting dates in Go requires a “layout”. This “layout” is based on magical reference date:</p> | 97 | <p>Anyone that has written Go previously, will probably already know what I am getting at here. For the uninitiated, parsing and formatting dates in Go requires a “layout”. This “layout” is based on magical reference date:</p> |
98 | <pre><code>Mon Jan 2 15:04:05 MST 2006</code></pre> | 98 | <pre><code>Mon Jan 2 15:04:05 MST 2006</code></pre> |
99 | <p>Which is the date produced when you write the first seven natural numbers like so:</p> | 99 | <p>Which is the date produced when you write the first seven natural numbers like so:</p> |
100 | <pre><code>01/02 03:04:05 '06 -0700</code></pre> | 100 | <pre><code>01/02 03:04:05 '06 -0700</code></pre> |
101 | <p>Parsing a string in <code>YYYY-MM-DD</code> format would look something like:</p> | 101 | <p>Parsing a string in <code>YYYY-MM-DD</code> format would look something like:</p> |
102 | <div class="sourceCode" id="cb7"><pre class="sourceCode go"><code class="sourceCode go"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true"></a><span class="kw">const</span> layout = <span class="st">"2006-01-02"</span></span> | 102 | <div class="sourceCode" id="cb7"><pre class="sourceCode go"><code class="sourceCode go"><span id="cb7-1"><a href="#cb7-1"></a><span class="kw">const</span> layout = <span class="st">"2006-01-02"</span></span> |
103 | <span id="cb7-2"><a href="#cb7-2" aria-hidden="true"></a>time.Parse(layout, <span class="st">"2020-08-01"</span>)</span></code></pre></div> | 103 | <span id="cb7-2"><a href="#cb7-2"></a>time.Parse(layout, <span class="st">"2020-08-01"</span>)</span></code></pre></div> |
104 | <p>This so-called “intuitive” method of formatting dates doesn’t allow you to print <code>0000 hrs</code> as <code>2400 hrs</code>, it doesn’t allow you to omit the leading zero in 24 hour formats. It is rife with inconveniences, if only there were a <a href="https://man7.org/linux/man-pages/man3/strftime.3.html">tried and tested</a> date formatting convention …</p> | 104 | <p>This so-called “intuitive” method of formatting dates doesn’t allow you to print <code>0000 hrs</code> as <code>2400 hrs</code>, it doesn’t allow you to omit the leading zero in 24 hour formats. It is rife with inconveniences, if only there were a <a href="https://man7.org/linux/man-pages/man3/strftime.3.html">tried and tested</a> date formatting convention …</p> |
105 | <h3 id="statements-over-expressions">Statements over Expressions</h3> | 105 | <h3 id="statements-over-expressions">Statements over Expressions</h3> |
106 | <p>Statements have side effects, expressions return values. More often than not, expressions are easier to understand at a glance: evaluate the LHS and assign the same to the RHS.</p> | 106 | <p>Statements have side effects, expressions return values. More often than not, expressions are easier to understand at a glance: evaluate the LHS and assign the same to the RHS.</p> |
107 | <p>Rust allows you to create local namespaces, and treats blocks (<code>{}</code>) as expressions:</p> | 107 | <p>Rust allows you to create local namespaces, and treats blocks (<code>{}</code>) as expressions:</p> |
108 | <div class="sourceCode" id="cb8"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true"></a><span class="kw">let</span> twenty_seven <span class="op">=</span> <span class="op">{</span></span> | 108 | <div class="sourceCode" id="cb8"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb8-1"><a href="#cb8-1"></a><span class="kw">let</span> twenty_seven = <span class="op">{</span></span> |
109 | <span id="cb8-2"><a href="#cb8-2" aria-hidden="true"></a> <span class="kw">let</span> three <span class="op">=</span> <span class="dv">1</span> <span class="op">+</span> <span class="dv">2</span><span class="op">;</span></span> | 109 | <span id="cb8-2"><a href="#cb8-2"></a> <span class="kw">let</span> three = <span class="dv">1</span> + <span class="dv">2</span>;</span> |
110 | <span id="cb8-3"><a href="#cb8-3" aria-hidden="true"></a> <span class="kw">let</span> nine <span class="op">=</span> three <span class="op">*</span> three<span class="op">;</span></span> | 110 | <span id="cb8-3"><a href="#cb8-3"></a> <span class="kw">let</span> nine = three * three;</span> |
111 | <span id="cb8-4"><a href="#cb8-4" aria-hidden="true"></a> nine <span class="op">*</span> three</span> | 111 | <span id="cb8-4"><a href="#cb8-4"></a> nine * three</span> |
112 | <span id="cb8-5"><a href="#cb8-5" aria-hidden="true"></a><span class="op">};</span></span></code></pre></div> | 112 | <span id="cb8-5"><a href="#cb8-5"></a><span class="op">}</span>;</span></code></pre></div> |
113 | <p>The Go equivalent of the same:</p> | 113 | <p>The Go equivalent of the same:</p> |
114 | <div class="sourceCode" id="cb9"><pre class="sourceCode go"><code class="sourceCode go"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true"></a>twenty_seven := <span class="ot">nil</span></span> | 114 | <div class="sourceCode" id="cb9"><pre class="sourceCode go"><code class="sourceCode go"><span id="cb9-1"><a href="#cb9-1"></a>twenty_seven := <span class="ot">nil</span></span> |
115 | <span id="cb9-2"><a href="#cb9-2" aria-hidden="true"></a></span> | 115 | <span id="cb9-2"><a href="#cb9-2"></a></span> |
116 | <span id="cb9-3"><a href="#cb9-3" aria-hidden="true"></a>three := <span class="dv">1</span> + <span class="dv">2</span></span> | 116 | <span id="cb9-3"><a href="#cb9-3"></a>three := <span class="dv">1</span> + <span class="dv">2</span></span> |
117 | <span id="cb9-4"><a href="#cb9-4" aria-hidden="true"></a>nine := three * three</span> | 117 | <span id="cb9-4"><a href="#cb9-4"></a>nine := three * three</span> |
118 | <span id="cb9-5"><a href="#cb9-5" aria-hidden="true"></a>twenty_seven = nine * three</span></code></pre></div> | 118 | <span id="cb9-5"><a href="#cb9-5"></a>twenty_seven = nine * three</span></code></pre></div> |
119 | <h3 id="erroring-out-on-unused-variables">Erroring out on unused variables</h3> | 119 | <h3 id="erroring-out-on-unused-variables">Erroring out on unused variables</h3> |
120 | <p>Want to quickly prototype something? Go says no! In all seriousness, a warning would suffice, I don’t want to have to go back and comment each unused import out, only to come back and uncomment them a few seconds later.</p> | 120 | <p>Want to quickly prototype something? Go says no! In all seriousness, a warning would suffice, I don’t want to have to go back and comment each unused import out, only to come back and uncomment them a few seconds later.</p> |
121 | <h3 id="error-handling">Error handling</h3> | 121 | <h3 id="error-handling">Error handling</h3> |
122 | <div class="sourceCode" id="cb10"><pre class="sourceCode go"><code class="sourceCode go"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true"></a><span class="kw">if</span> err != <span class="ot">nil</span> { ... }</span></code></pre></div> | 122 | <div class="sourceCode" id="cb10"><pre class="sourceCode go"><code class="sourceCode go"><span id="cb10-1"><a href="#cb10-1"></a><span class="kw">if</span> err != <span class="ot">nil</span> { ... }</span></code></pre></div> |
123 | <p>Need I say more? I will, for good measure:</p> | 123 | <p>Need I say more? I will, for good measure:</p> |
124 | <ol type="1"> | 124 | <ol type="1"> |
125 | <li>Error handling is optional</li> | 125 | <li>Error handling is optional</li> |
126 | <li>Errors are propagated via a clunky <code>if</code> + <code>return</code> statement</li> | 126 | <li>Errors are propagated via a clunky <code>if</code> + <code>return</code> statement</li> |
127 | </ol> | 127 | </ol> |
128 | <p>I prefer Haskell’s “Monadic” error handling, which is employed by Rust as well:</p> | 128 | <p>I prefer Haskell’s “Monadic” error handling, which is employed by Rust as well:</p> |
129 | <div class="sourceCode" id="cb11"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true"></a><span class="co">// 1. error handling is compulsory</span></span> | 129 | <div class="sourceCode" id="cb11"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb11-1"><a href="#cb11-1"></a><span class="co">// 1. error handling is compulsory</span></span> |
130 | <span id="cb11-2"><a href="#cb11-2" aria-hidden="true"></a><span class="co">// 2. errors are propagated with the `?` operator</span></span> | 130 | <span id="cb11-2"><a href="#cb11-2"></a><span class="co">// 2. errors are propagated with the `?` operator</span></span> |
131 | <span id="cb11-3"><a href="#cb11-3" aria-hidden="true"></a><span class="kw">fn</span> foo() <span class="op">-></span> <span class="dt">Result</span><span class="op"><</span><span class="dt">String</span><span class="op">,</span> <span class="pp">io::</span><span class="bu">Error</span><span class="op">></span> <span class="op">{</span></span> | 131 | <span id="cb11-3"><a href="#cb11-3"></a><span class="kw">fn</span> foo() -> <span class="dt">Result</span><<span class="dt">String</span>, <span class="pp">io::</span>Error> <span class="op">{</span></span> |
132 | <span id="cb11-4"><a href="#cb11-4" aria-hidden="true"></a> <span class="kw">let</span> <span class="kw">mut</span> f <span class="op">=</span> <span class="pp">File::</span>open(<span class="st">"foo.txt"</span>)<span class="op">?;</span> <span class="co">// return if error</span></span> | 132 | <span id="cb11-4"><a href="#cb11-4"></a> <span class="kw">let</span> <span class="kw">mut</span> f = <span class="pp">File::</span>open(<span class="st">"foo.txt"</span>)?; <span class="co">// return if error</span></span> |
133 | <span id="cb11-5"><a href="#cb11-5" aria-hidden="true"></a> <span class="kw">let</span> <span class="kw">mut</span> s <span class="op">=</span> <span class="dt">String</span><span class="pp">::</span>new()<span class="op">;</span></span> | 133 | <span id="cb11-5"><a href="#cb11-5"></a> <span class="kw">let</span> <span class="kw">mut</span> s = <span class="dt">String</span>::new();</span> |
134 | <span id="cb11-6"><a href="#cb11-6" aria-hidden="true"></a></span> | 134 | <span id="cb11-6"><a href="#cb11-6"></a></span> |
135 | <span id="cb11-7"><a href="#cb11-7" aria-hidden="true"></a> f<span class="op">.</span>read_to_string(<span class="op">&</span><span class="kw">mut</span> s)<span class="op">?;</span> <span class="co">// return if error</span></span> | 135 | <span id="cb11-7"><a href="#cb11-7"></a> f.read_to_string(&<span class="kw">mut</span> s)?; <span class="co">// return if error</span></span> |
136 | <span id="cb11-8"><a href="#cb11-8" aria-hidden="true"></a></span> | 136 | <span id="cb11-8"><a href="#cb11-8"></a></span> |
137 | <span id="cb11-9"><a href="#cb11-9" aria-hidden="true"></a> <span class="cn">Ok</span>(s) <span class="co">// all good, return a string inside a `Result` context</span></span> | 137 | <span id="cb11-9"><a href="#cb11-9"></a> <span class="cn">Ok</span>(s) <span class="co">// all good, return a string inside a `Result` context</span></span> |
138 | <span id="cb11-10"><a href="#cb11-10" aria-hidden="true"></a><span class="op">}</span></span> | 138 | <span id="cb11-10"><a href="#cb11-10"></a><span class="op">}</span></span> |
139 | <span id="cb11-11"><a href="#cb11-11" aria-hidden="true"></a></span> | 139 | <span id="cb11-11"><a href="#cb11-11"></a></span> |
140 | <span id="cb11-12"><a href="#cb11-12" aria-hidden="true"></a><span class="kw">fn</span> main() <span class="op">{</span></span> | 140 | <span id="cb11-12"><a href="#cb11-12"></a><span class="kw">fn</span> main() <span class="op">{</span></span> |
141 | <span id="cb11-13"><a href="#cb11-13" aria-hidden="true"></a> <span class="co">// `contents` is an enum known as Result:</span></span> | 141 | <span id="cb11-13"><a href="#cb11-13"></a> <span class="co">// `contents` is an enum known as Result:</span></span> |
142 | <span id="cb11-14"><a href="#cb11-14" aria-hidden="true"></a> <span class="kw">let</span> contents <span class="op">=</span> foo()<span class="op">;</span></span> | 142 | <span id="cb11-14"><a href="#cb11-14"></a> <span class="kw">let</span> contents = foo();</span> |
143 | <span id="cb11-15"><a href="#cb11-15" aria-hidden="true"></a> <span class="kw">match</span> contents <span class="op">{</span></span> | 143 | <span id="cb11-15"><a href="#cb11-15"></a> <span class="kw">match</span> contents <span class="op">{</span></span> |
144 | <span id="cb11-16"><a href="#cb11-16" aria-hidden="true"></a> <span class="cn">Ok</span>(c) <span class="op">=></span> <span class="pp">println!</span>(c)<span class="op">,</span></span> | 144 | <span id="cb11-16"><a href="#cb11-16"></a> <span class="cn">Ok</span>(c) => <span class="pp">println!</span>(c),</span> |
145 | <span id="cb11-17"><a href="#cb11-17" aria-hidden="true"></a> <span class="cn">Err</span>(e) <span class="op">=></span> <span class="pp">eprintln!</span>(e)</span> | 145 | <span id="cb11-17"><a href="#cb11-17"></a> <span class="cn">Err</span>(e) => <span class="pp">eprintln!</span>(e)</span> |
146 | <span id="cb11-18"><a href="#cb11-18" aria-hidden="true"></a> <span class="op">}</span></span> | 146 | <span id="cb11-18"><a href="#cb11-18"></a> <span class="op">}</span></span> |
147 | <span id="cb11-19"><a href="#cb11-19" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> | 147 | <span id="cb11-19"><a href="#cb11-19"></a><span class="op">}</span></span></code></pre></div> |
148 | <h3 id="conclusion">Conclusion</h3> | 148 | <h3 id="conclusion">Conclusion</h3> |
149 | <p>I did not want to conclude without talking about stylistic choices, lack of metaprogramming, bizzare export rules, but, I am too busy converting my <code>interface{}</code> types into actual generic code for Go v2.</p> | 149 | <p>I did not want to conclude without talking about stylistic choices, lack of metaprogramming, bizzare export rules, but, I am too busy converting my <code>interface{}</code> types into actual generic code for Go v2.</p> |
150 | 150 | ||