From 70827fe44d6844528e7dd56637de2f0f3e0cf847 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 1 Aug 2020 17:25:39 +0530 Subject: update irc nick, publish post --- docs/index.html | 19 ++- docs/index.xml | 115 ++++++++++++++ docs/posts/WPA_woes/index.html | 2 +- docs/posts/auto-currying_rust_functions/index.html | 2 +- docs/posts/bash_harder_with_vim/index.html | 2 +- docs/posts/bye_bye_BDFs/index.html | 2 +- docs/posts/call_to_ARMs/index.html | 2 +- docs/posts/color_conundrum/index.html | 2 +- docs/posts/font_size_fallacies/index.html | 2 +- .../index.html | 2 +- docs/posts/gripes_with_go/index.html | 173 +++++++++++++++++++++ docs/posts/hold_position!/index.html | 2 +- docs/posts/my_setup/index.html | 2 +- docs/posts/onivim_sucks/index.html | 2 +- docs/posts/pixel_art_in_GIMP/index.html | 2 +- docs/posts/rapid_refactoring_with_vim/index.html | 2 +- docs/posts/static_sites_with_bash/index.html | 2 +- docs/posts/termux_tandem/index.html | 2 +- docs/posts/turing_complete_type_systems/index.html | 2 +- 19 files changed, 322 insertions(+), 17 deletions(-) create mode 100644 docs/posts/gripes_with_go/index.html (limited to 'docs') diff --git a/docs/index.html b/docs/index.html index 68da119..6a70b78 100644 --- a/docs/index.html +++ b/docs/index.html @@ -30,9 +30,26 @@ I am a compsci undergrad, Rust programmer and an enthusiastic Vimmer. I write open-source stuff to pass time. I also design fonts: scientifica, curie.

-

Send me a mail at nerdy@peppe.rs or a message at nerd@irc.rizon.net.

+

Send me a mail at nerdy@peppe.rs or a message at nerdypepper@irc.rizon.net.

+ + + + +
+
+ 01/08 — 2020 +
+ + Gripes With Go + +
+ + 4.9 + + min +
diff --git a/docs/index.xml b/docs/index.xml index 628c111..003d4d6 100644 --- a/docs/index.xml +++ b/docs/index.xml @@ -12,6 +12,121 @@ en-us Creative Commons BY-NC-SA 4.0 +Gripes With Go +<p>You’ve read a lot of posts about the shortcomings of the Go programming language, so what’s one more.</p> +<ol type="1"> +<li><a href="#lack-of-sum-types">Lack of sum types</a></li> +<li><a href="#type-assertions">Type assertions</a></li> +<li><a href="#date-and-time">Date and Time</a></li> +<li><a href="#statements-over-expressions">Statements over Expressions</a></li> +<li><a href="#erroring-out-on-unused-variables">Erroring out on unused variables</a></li> +<li><a href="#error-handling">Error handling</a></li> +</ol> +<h3 id="lack-of-sum-types">Lack of Sum types</h3> +<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> +<p>A type to represent gender for example:</p> +<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> +<span id="cb1-2"><a href="#cb1-2" aria-hidden="true"></a></span> +<span id="cb1-3"><a href="#cb1-3" aria-hidden="true"></a><span class="kw">const</span> (</span> +<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> +<span id="cb1-5"><a href="#cb1-5" aria-hidden="true"></a> Female <span class="co">// assigns Female to 1</span></span> +<span id="cb1-6"><a href="#cb1-6" aria-hidden="true"></a> Other <span class="co">// assigns Other to 2</span></span> +<span id="cb1-7"><a href="#cb1-7" aria-hidden="true"></a>)</span> +<span id="cb1-8"><a href="#cb1-8" aria-hidden="true"></a></span> +<span id="cb1-9"><a href="#cb1-9" aria-hidden="true"></a>fmt.Println(<span class="st">&quot;My gender is &quot;</span>, Male)</span> +<span id="cb1-10"><a href="#cb1-10" aria-hidden="true"></a><span class="co">// My gender is 0</span></span> +<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> +<span id="cb1-12"><a href="#cb1-12" aria-hidden="true"></a></span> +<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> +<span id="cb1-14"><a href="#cb1-14" aria-hidden="true"></a> <span class="kw">switch</span> (g) {</span> +<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">&quot;Male&quot;</span></span> +<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">&quot;Female&quot;</span></span> +<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">&quot;Other&quot;</span></span> +<span id="cb1-18"><a href="#cb1-18" aria-hidden="true"></a> }</span> +<span id="cb1-19"><a href="#cb1-19" aria-hidden="true"></a>}</span> +<span id="cb1-20"><a href="#cb1-20" aria-hidden="true"></a></span> +<span id="cb1-21"><a href="#cb1-21" aria-hidden="true"></a><span class="co">// You can accidentally do stupid stuff like:</span></span> +<span id="cb1-22"><a href="#cb1-22" aria-hidden="true"></a>gender := Male + <span class="dv">1</span></span></code></pre></div> +<p>The Haskell equivalent of the same:</p> +<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> +<span id="cb2-2"><a href="#cb2-2" aria-hidden="true"></a> <span class="op">|</span> <span class="dt">Female</span></span> +<span id="cb2-3"><a href="#cb2-3" aria-hidden="true"></a> <span class="op">|</span> <span class="dt">Other</span></span> +<span id="cb2-4"><a href="#cb2-4" aria-hidden="true"></a> <span class="kw">deriving</span> (<span class="dt">Show</span>)</span> +<span id="cb2-5"><a href="#cb2-5" aria-hidden="true"></a></span> +<span id="cb2-6"><a href="#cb2-6" aria-hidden="true"></a><span class="fu">print</span> <span class="op">$</span> <span class="st">&quot;My gender is &quot;</span> <span class="op">++</span> (<span class="fu">show</span> <span class="dt">Male</span>)</span></code></pre></div> +<h3 id="type-assertions">Type Assertions</h3> +<p>A downcast with an optional error check? What could go wrong?</p> +<p>Type assertions in Go allow you to do:</p> +<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> +<span id="cb3-2"><a href="#cb3-2" aria-hidden="true"></a>y, goodToGo := x.(<span class="dt">int</span>)</span> +<span id="cb3-3"><a href="#cb3-3" aria-hidden="true"></a><span class="kw">if</span> goodToGo {</span> +<span id="cb3-4"><a href="#cb3-4" aria-hidden="true"></a> fmt.Println(y)</span> +<span id="cb3-5"><a href="#cb3-5" aria-hidden="true"></a>}</span></code></pre></div> +<p>The error check however is optional:</p> +<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> +<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> +<span id="cb4-3"><a href="#cb4-3" aria-hidden="true"></a>fmt.Println(y)</span> +<span id="cb4-4"><a href="#cb4-4" aria-hidden="true"></a><span class="co">// results in a runtime error:</span></span> +<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> +<h3 id="date-and-time">Date and Time</h3> +<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> +<pre><code>Mon Jan 2 15:04:05 MST 2006</code></pre> +<p>Which is the date produced when you write the first seven natural numbers like so:</p> +<pre><code>01/02 03:04:05 &#39;06 -0700</code></pre> +<p>Parsing a string in <code>YYYY-MM-DD</code> format would look something like:</p> +<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">&quot;2006-01-02&quot;</span></span> +<span id="cb7-2"><a href="#cb7-2" aria-hidden="true"></a>time.Parse(layout, <span class="st">&quot;2020-08-01&quot;</span>)</span></code></pre></div> +<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> +<h3 id="statements-over-expressions">Statements over Expressions</h3> +<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> +<p>Rust allows you to create local namespaces, and treats blocks (<code>{}</code>) as expressions:</p> +<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> +<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> +<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> +<span id="cb8-4"><a href="#cb8-4" aria-hidden="true"></a> nine <span class="op">*</span> three</span> +<span id="cb8-5"><a href="#cb8-5" aria-hidden="true"></a><span class="op">};</span></span></code></pre></div> +<p>The Go equivalent of the same:</p> +<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> +<span id="cb9-2"><a href="#cb9-2" aria-hidden="true"></a></span> +<span id="cb9-3"><a href="#cb9-3" aria-hidden="true"></a>three := <span class="dv">1</span> + <span class="dv">2</span></span> +<span id="cb9-4"><a href="#cb9-4" aria-hidden="true"></a>nine := three * three</span> +<span id="cb9-5"><a href="#cb9-5" aria-hidden="true"></a>twenty_seven = nine * three</span></code></pre></div> +<h3 id="erroring-out-on-unused-variables">Erroring out on unused variables</h3> +<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> +<h3 id="error-handling">Error handling</h3> +<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> +<p>Need I say more? I will, for good measure:</p> +<ol type="1"> +<li>Error handling is optional</li> +<li>Errors are propagated via a clunky <code>if</code> + <code>return</code> statement</li> +</ol> +<p>I prefer Haskell’s “Monadic” error handling, which is employed by Rust as well:</p> +<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> +<span id="cb11-2"><a href="#cb11-2" aria-hidden="true"></a><span class="co">// 2. errors are propagated with the `?` operator</span></span> +<span id="cb11-3"><a href="#cb11-3" aria-hidden="true"></a><span class="kw">fn</span> foo() <span class="op">-&gt;</span> <span class="dt">Result</span><span class="op">&lt;</span><span class="dt">String</span><span class="op">,</span> <span class="pp">io::</span><span class="bu">Error</span><span class="op">&gt;</span> <span class="op">{</span></span> +<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">&quot;foo.txt&quot;</span>)<span class="op">?;</span> <span class="co">// return if error</span></span> +<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> +<span id="cb11-6"><a href="#cb11-6" aria-hidden="true"></a></span> +<span id="cb11-7"><a href="#cb11-7" aria-hidden="true"></a> f<span class="op">.</span>read_to_string(<span class="op">&amp;</span><span class="kw">mut</span> s)<span class="op">?;</span> <span class="co">// return if error</span></span> +<span id="cb11-8"><a href="#cb11-8" aria-hidden="true"></a></span> +<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> +<span id="cb11-10"><a href="#cb11-10" aria-hidden="true"></a><span class="op">}</span></span> +<span id="cb11-11"><a href="#cb11-11" aria-hidden="true"></a></span> +<span id="cb11-12"><a href="#cb11-12" aria-hidden="true"></a><span class="kw">fn</span> main() <span class="op">{</span></span> +<span id="cb11-13"><a href="#cb11-13" aria-hidden="true"></a> <span class="co">// `contents` is an enum known as Result:</span></span> +<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> +<span id="cb11-15"><a href="#cb11-15" aria-hidden="true"></a> <span class="kw">match</span> contents <span class="op">{</span></span> +<span id="cb11-16"><a href="#cb11-16" aria-hidden="true"></a> <span class="cn">Ok</span>(c) <span class="op">=&gt;</span> <span class="pp">println!</span>(c)<span class="op">,</span></span> +<span id="cb11-17"><a href="#cb11-17" aria-hidden="true"></a> <span class="cn">Err</span>(e) <span class="op">=&gt;</span> <span class="pp">eprintln!</span>(e)</span> +<span id="cb11-18"><a href="#cb11-18" aria-hidden="true"></a> <span class="op">}</span></span> +<span id="cb11-19"><a href="#cb11-19" aria-hidden="true"></a><span class="op">}</span></span></code></pre></div> +<h3 id="conclusion">Conclusion</h3> +<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> +https://peppe.rs/posts/gripes_with_go/ +Sat, 01 Aug 2020 11:55:00 +0000 +https://peppe.rs/posts/gripes_with_go/ + + Turing Complete Type Systems <p>Rust’s type system is Turing complete:</p> <ul> diff --git a/docs/posts/WPA_woes/index.html b/docs/posts/WPA_woes/index.html index 0c50385..5121434 100644 --- a/docs/posts/WPA_woes/index.html +++ b/docs/posts/WPA_woes/index.html @@ -76,7 +76,7 @@ $ sudo sv restart dhcpcd I am a compsci undergrad, Rust programmer and an enthusiastic Vimmer. I write open-source stuff to pass time. I also design fonts: scientifica, curie.

-

Send me a mail at nerdy@peppe.rs or a message at nerd@irc.rizon.net.

+

Send me a mail at nerdy@peppe.rs or a message at nerdypepper@irc.rizon.net.

⟵ Back diff --git a/docs/posts/auto-currying_rust_functions/index.html b/docs/posts/auto-currying_rust_functions/index.html index a86667c..62f92bc 100644 --- a/docs/posts/auto-currying_rust_functions/index.html +++ b/docs/posts/auto-currying_rust_functions/index.html @@ -540,7 +540,7 @@ test tests::works ... ok I am a compsci undergrad, Rust programmer and an enthusiastic Vimmer. I write open-source stuff to pass time. I also design fonts: scientifica, curie.

-

Send me a mail at nerdy@peppe.rs or a message at nerd@irc.rizon.net.

+

Send me a mail at nerdy@peppe.rs or a message at nerdypepper@irc.rizon.net.

⟵ Back diff --git a/docs/posts/bash_harder_with_vim/index.html b/docs/posts/bash_harder_with_vim/index.html index 7c9e5d6..303880d 100644 --- a/docs/posts/bash_harder_with_vim/index.html +++ b/docs/posts/bash_harder_with_vim/index.html @@ -86,7 +86,7 @@ Press ENTER or type command to continue I am a compsci undergrad, Rust programmer and an enthusiastic Vimmer. I write open-source stuff to pass time. I also design fonts: scientifica, curie.

-

Send me a mail at nerdy@peppe.rs or a message at nerd@irc.rizon.net.

+

Send me a mail at nerdy@peppe.rs or a message at nerdypepper@irc.rizon.net.

⟵ Back diff --git a/docs/posts/bye_bye_BDFs/index.html b/docs/posts/bye_bye_BDFs/index.html index a2e410d..43d2dfd 100644 --- a/docs/posts/bye_bye_BDFs/index.html +++ b/docs/posts/bye_bye_BDFs/index.html @@ -58,7 +58,7 @@ I am a compsci undergrad, Rust programmer and an enthusiastic Vimmer. I write open-source stuff to pass time. I also design fonts: scientifica, curie.

-

Send me a mail at nerdy@peppe.rs or a message at nerd@irc.rizon.net.

+

Send me a mail at nerdy@peppe.rs or a message at nerdypepper@irc.rizon.net.

⟵ Back diff --git a/docs/posts/call_to_ARMs/index.html b/docs/posts/call_to_ARMs/index.html index f882e6b..a05ed38 100644 --- a/docs/posts/call_to_ARMs/index.html +++ b/docs/posts/call_to_ARMs/index.html @@ -83,7 +83,7 @@ Reading symbols from main... # yay! I am a compsci undergrad, Rust programmer and an enthusiastic Vimmer. I write open-source stuff to pass time. I also design fonts: scientifica, curie.

-

Send me a mail at nerdy@peppe.rs or a message at nerd@irc.rizon.net.

+

Send me a mail at nerdy@peppe.rs or a message at nerdypepper@irc.rizon.net.

⟵ Back diff --git a/docs/posts/color_conundrum/index.html b/docs/posts/color_conundrum/index.html index 0ba7aa5..c7db8cd 100644 --- a/docs/posts/color_conundrum/index.html +++ b/docs/posts/color_conundrum/index.html @@ -60,7 +60,7 @@ I am a compsci undergrad, Rust programmer and an enthusiastic Vimmer. I write open-source stuff to pass time. I also design fonts: scientifica, curie.

-

Send me a mail at nerdy@peppe.rs or a message at nerd@irc.rizon.net.

+

Send me a mail at nerdy@peppe.rs or a message at nerdypepper@irc.rizon.net.

⟵ Back diff --git a/docs/posts/font_size_fallacies/index.html b/docs/posts/font_size_fallacies/index.html index c91eba5..af55661 100644 --- a/docs/posts/font_size_fallacies/index.html +++ b/docs/posts/font_size_fallacies/index.html @@ -82,7 +82,7 @@ Dimensions: 1920x1080 @ 13" (29.5x16.5 sq. cm) I am a compsci undergrad, Rust programmer and an enthusiastic Vimmer. I write open-source stuff to pass time. I also design fonts: scientifica, curie.

-

Send me a mail at nerdy@peppe.rs or a message at nerd@irc.rizon.net.

+

Send me a mail at nerdy@peppe.rs or a message at nerdypepper@irc.rizon.net.

⟵ Back diff --git a/docs/posts/get_better_at_yanking_and_putting_in_vim/index.html b/docs/posts/get_better_at_yanking_and_putting_in_vim/index.html index a009a15..e02f5d2 100644 --- a/docs/posts/get_better_at_yanking_and_putting_in_vim/index.html +++ b/docs/posts/get_better_at_yanking_and_putting_in_vim/index.html @@ -69,7 +69,7 @@ nnoremap gb `[v`] " "a quick map to perform the above< I am a compsci undergrad, Rust programmer and an enthusiastic Vimmer. I write open-source stuff to pass time. I also design fonts: scientifica, curie.

-

Send me a mail at nerdy@peppe.rs or a message at nerd@irc.rizon.net.

+

Send me a mail at nerdy@peppe.rs or a message at nerdypepper@irc.rizon.net.

⟵ Back diff --git a/docs/posts/gripes_with_go/index.html b/docs/posts/gripes_with_go/index.html new file mode 100644 index 0000000..b527b4a --- /dev/null +++ b/docs/posts/gripes_with_go/index.html @@ -0,0 +1,173 @@ + + + + + + + + + + + + + + + Gripes With Go · peppe.rs + +
+
+ ⟵ Back + View Raw +
+
+ 01/08 — 2020 +
+ + 76.71 + + cm +   + + 4.9 + + min +
+
+

+ Gripes With Go +

+
+

You’ve read a lot of posts about the shortcomings of the Go programming language, so what’s one more.

+
    +
  1. Lack of sum types
  2. +
  3. Type assertions
  4. +
  5. Date and Time
  6. +
  7. Statements over Expressions
  8. +
  9. Erroring out on unused variables
  10. +
  11. Error handling
  12. +
+

Lack of Sum types

+

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 enum type in C. Go lacks enum types unfortunately, and you are forced to resort to crafting your own substitute.

+

A type to represent gender for example:

+
type Gender int
+
+const (
+    Male Gender = iota  // assigns Male to 0
+    Female              // assigns Female to 1
+    Other               // assigns Other to 2
+)
+
+fmt.Println("My gender is ", Male)
+// My gender is 0
+// Oops! We have to implement String() for Gender ...
+
+func (g Gender) String() string {
+    switch (g) {
+    case 0: return "Male"
+    case 1: return "Female"
+    default: return "Other"
+    }
+}
+
+// You can accidentally do stupid stuff like:
+gender := Male + 1
+

The Haskell equivalent of the same:

+
data Gender = Male
+            | Female
+            | Other
+            deriving (Show)
+
+print $ "My gender is " ++ (show Male)
+

Type Assertions

+

A downcast with an optional error check? What could go wrong?

+

Type assertions in Go allow you to do:

+
var x interface{} = 7
+y, goodToGo := x.(int)
+if goodToGo {
+    fmt.Println(y)
+}
+

The error check however is optional:

+
var x interface{} = 7
+var y := x.(float64)
+fmt.Println(y)
+// results in a runtime error:
+// panic: interface conversion: interface {} is int, not float64
+

Date and Time

+

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:

+
Mon Jan 2 15:04:05 MST 2006
+

Which is the date produced when you write the first seven natural numbers like so:

+
01/02 03:04:05 '06 -0700
+

Parsing a string in YYYY-MM-DD format would look something like:

+
const layout = "2006-01-02"
+time.Parse(layout, "2020-08-01")
+

This so-called “intuitive” method of formatting dates doesn’t allow you to print 0000 hrs as 2400 hrs, it doesn’t allow you to omit the leading zero in 24 hour formats. It is rife with inconveniences, if only there were a tried and tested date formatting convention …

+

Statements over Expressions

+

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.

+

Rust allows you to create local namespaces, and treats blocks ({}) as expressions:

+
let twenty_seven = {
+    let three = 1 + 2;
+    let nine = three * three;
+    nine * three
+};
+

The Go equivalent of the same:

+
twenty_seven := nil
+
+three := 1 + 2
+nine := three * three
+twenty_seven = nine * three
+

Erroring out on unused variables

+

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.

+

Error handling

+
if err != nil { ... }
+

Need I say more? I will, for good measure:

+
    +
  1. Error handling is optional
  2. +
  3. Errors are propagated via a clunky if + return statement
  4. +
+

I prefer Haskell’s “Monadic” error handling, which is employed by Rust as well:

+
// 1. error handling is compulsory
+// 2. errors are propagated with the `?` operator
+fn foo() -> Result<String, io::Error> {
+    let mut f = File::open("foo.txt")?; // return if error
+    let mut s = String::new();
+
+    f.read_to_string(&mut s)?; // return if error
+
+    Ok(s) // all good, return a string inside a `Result` context
+}
+
+fn main() {
+    // `contents` is an enum known as Result:
+    let contents = foo();
+    match contents {
+        Ok(c) => println!(c),
+        Err(e) => eprintln!(e)
+    }
+}
+

Conclusion

+

I did not want to conclude without talking about stylistic choices, lack of metaprogramming, bizzare export rules, but, I am too busy converting my interface{} types into actual generic code for Go v2.

+ +
+ +
+ Hi. + +

I'm Akshay, I go by nerd or nerdypepper on the internet.

+

+ I am a compsci undergrad, Rust programmer and an enthusiastic Vimmer. + I write open-source stuff to pass time. I also design fonts: scientifica, curie. +

+

Send me a mail at nerdy@peppe.rs or a message at nerdypepper@irc.rizon.net.

+
+ + ⟵ Back + View Raw +
+
+ + diff --git a/docs/posts/hold_position!/index.html b/docs/posts/hold_position!/index.html index 61e4768..8948370 100644 --- a/docs/posts/hold_position!/index.html +++ b/docs/posts/hold_position!/index.html @@ -60,7 +60,7 @@ winrestview(view) " restore our original view! I am a compsci undergrad, Rust programmer and an enthusiastic Vimmer. I write open-source stuff to pass time. I also design fonts: scientifica, curie.

-

Send me a mail at nerdy@peppe.rs or a message at nerd@irc.rizon.net.

+

Send me a mail at nerdy@peppe.rs or a message at nerdypepper@irc.rizon.net.

⟵ Back diff --git a/docs/posts/my_setup/index.html b/docs/posts/my_setup/index.html index 996a459..c24c765 100644 --- a/docs/posts/my_setup/index.html +++ b/docs/posts/my_setup/index.html @@ -58,7 +58,7 @@ I am a compsci undergrad, Rust programmer and an enthusiastic Vimmer. I write open-source stuff to pass time. I also design fonts: scientifica, curie.

-

Send me a mail at nerdy@peppe.rs or a message at nerd@irc.rizon.net.

+

Send me a mail at nerdy@peppe.rs or a message at nerdypepper@irc.rizon.net.

⟵ Back diff --git a/docs/posts/onivim_sucks/index.html b/docs/posts/onivim_sucks/index.html index ded6fd9..1549e3e 100644 --- a/docs/posts/onivim_sucks/index.html +++ b/docs/posts/onivim_sucks/index.html @@ -57,7 +57,7 @@ I am a compsci undergrad, Rust programmer and an enthusiastic Vimmer. I write open-source stuff to pass time. I also design fonts: scientifica, curie.

-

Send me a mail at nerdy@peppe.rs or a message at nerd@irc.rizon.net.

+

Send me a mail at nerdy@peppe.rs or a message at nerdypepper@irc.rizon.net.

⟵ Back diff --git a/docs/posts/pixel_art_in_GIMP/index.html b/docs/posts/pixel_art_in_GIMP/index.html index e6661b2..f767de8 100644 --- a/docs/posts/pixel_art_in_GIMP/index.html +++ b/docs/posts/pixel_art_in_GIMP/index.html @@ -101,7 +101,7 @@ I am a compsci undergrad, Rust programmer and an enthusiastic Vimmer. I write open-source stuff to pass time. I also design fonts: scientifica, curie.

-

Send me a mail at nerdy@peppe.rs or a message at nerd@irc.rizon.net.

+

Send me a mail at nerdy@peppe.rs or a message at nerdypepper@irc.rizon.net.

⟵ Back diff --git a/docs/posts/rapid_refactoring_with_vim/index.html b/docs/posts/rapid_refactoring_with_vim/index.html index ee05dc0..b0ed0ad 100644 --- a/docs/posts/rapid_refactoring_with_vim/index.html +++ b/docs/posts/rapid_refactoring_with_vim/index.html @@ -141,7 +141,7 @@ BUFFER: json!( ... ); I am a compsci undergrad, Rust programmer and an enthusiastic Vimmer. I write open-source stuff to pass time. I also design fonts: scientifica, curie.

-

Send me a mail at nerdy@peppe.rs or a message at nerd@irc.rizon.net.

+

Send me a mail at nerdy@peppe.rs or a message at nerdypepper@irc.rizon.net.

⟵ Back diff --git a/docs/posts/static_sites_with_bash/index.html b/docs/posts/static_sites_with_bash/index.html index b2a90a7..80bfe37 100644 --- a/docs/posts/static_sites_with_bash/index.html +++ b/docs/posts/static_sites_with_bash/index.html @@ -79,7 +79,7 @@ I am a compsci undergrad, Rust programmer and an enthusiastic Vimmer. I write open-source stuff to pass time. I also design fonts: scientifica, curie.

-

Send me a mail at nerdy@peppe.rs or a message at nerd@irc.rizon.net.

+

Send me a mail at nerdy@peppe.rs or a message at nerdypepper@irc.rizon.net.

⟵ Back diff --git a/docs/posts/termux_tandem/index.html b/docs/posts/termux_tandem/index.html index b4818cf..37088bb 100644 --- a/docs/posts/termux_tandem/index.html +++ b/docs/posts/termux_tandem/index.html @@ -70,7 +70,7 @@ mtZabXG.jpg p8d5c584f2841.jpg vjUxGjq.jpg I am a compsci undergrad, Rust programmer and an enthusiastic Vimmer. I write open-source stuff to pass time. I also design fonts: scientifica, curie.

-

Send me a mail at nerdy@peppe.rs or a message at nerd@irc.rizon.net.

+

Send me a mail at nerdy@peppe.rs or a message at nerdypepper@irc.rizon.net.

⟵ Back diff --git a/docs/posts/turing_complete_type_systems/index.html b/docs/posts/turing_complete_type_systems/index.html index 3e572e3..cc51210 100644 --- a/docs/posts/turing_complete_type_systems/index.html +++ b/docs/posts/turing_complete_type_systems/index.html @@ -61,7 +61,7 @@ I am a compsci undergrad, Rust programmer and an enthusiastic Vimmer. I write open-source stuff to pass time. I also design fonts: scientifica, curie.

-

Send me a mail at nerdy@peppe.rs or a message at nerd@irc.rizon.net.

+

Send me a mail at nerdy@peppe.rs or a message at nerdypepper@irc.rizon.net.

⟵ Back -- cgit v1.2.3