Scripting with Go
There was a recent discussion in &#x2F;r&#x2F;golang about chaining methods like this. The OP had a hard time writing tests for their implementation since each part of the chain returned interfaces that were relied on by following links within. The top answer in the thread was that Golang wasn&#x27;t really meant to make &quot;Promise-&quot; type workflows possible due to it leaning heavily on code being intentional and explicit.<p>I agree with them.<p>One of the problems I have with things like LINQ or Promises is that they are immensely powerful but can be really difficult to debug when things go wrong because of implicit errors within the pipeline bubble up the stack without providing location context. While the standard boilerplate around errors in Go is annoying, I actually prefer it for two reasons:<p>1. It provides exactly where things went wrong while looking at an exception trace, and
2. From a readability perspective, it is much easier to understand what the author of the code meant to do and, more importantly, what the follow-on error s mean. Sometimes, errors aren&#x27;t actually errors.<p>Either way, OCaml&#x2F;Haskell&#x2F;F#&#x27;s approach with match expressions and the `Result` type is the best way to deal with this, IMO. You get the best of both worlds: explicit declaration with a very expressive (triangular-like) &quot;shape&quot; to the code.<p>So something like this:<p><pre><code>  var data string = script.File(&quot;test.txt&quot;).Match(&quot;Error&quot;).CountLines()
</code></pre>
Can be expressed like this:<p><pre><code>  var foundLines int32 = 0
  var numLines int32 = match file.Open(&quot;test.txt&quot;) with
    | Error e -&gt; return e
    | Ok f -&gt; match f.Readlines() with
      | Error e -&gt; return e
      | Ok lines -&gt; match lines with
        | &#x2F;Error.*&#x2F; -&gt; foundLines++
        | _ -&gt; &#x2F;&#x2F; do nothign
  return foundLines
</code></pre>
instead of:<p><pre><code>  foundLines := 0
  lines, err := ioutil.ReadFile(&quot;test.txt&quot;)
  if err != nil {
    return 0, err
  }
  for _, l := range lines {
    re, err := regexp.MustCompile(`Error.*`)
    if err != nil {
      return 0, err
    }
    matches := re.FindStringSubmatch(`Error.*`)
    if len(matches) &gt; 0 {
      foundLines++
    }
  }
  return foundLines, nil
</code></pre>
This way, you can see exactly where the error occurred but can still see that `numLines` is generated through a pipeline.<p>As far as the library itself, I personally wouldn&#x27;t use something like this, though I see the appeal. I turn to statically-typed languages like Golang when a Bash script becomes too kludgey to stand on its own (usually when I need to begin relying on mild data structures) and when I care about type safety and portability more than what Ruby or Python can give me. When I&#x27;m writing a Go program, I want something that&#x27;s testable that I know can work anywhere. With Ruby or Python, unless I&#x27;m distributing the script as a Docker image, I have to worry about versions, environments, etc., none of which are pleasant.<p>However, writing the error boilerplate is annoying, and I can see developers who want to write something quick but spend 99% of their time in Go using this to get something done with a tool they know well. I&#x27;ve seen similar things in the Java world; hell, that&#x27;s the reason why Groovy and Kotlin exist!<p>TL;DR: The &quot;wrong thing&quot; in the Bitfield article isn&#x27;t necessarily wrong; their library is useful, but niche; all hail pattern-matching and the Result type.