throwing errors in golang

2020-06-23

 | 

~2 min read

 | 

346 words

Imagine we’re counting the nucleotides in a given strand of DNA. If there’s an invalid nucleotide, we want to abort the process. How might we do that?

In Golang, here are two approaches:

  1. Use errorsNew method
  2. Use fmt’s Errorf method

Approach 1: errors.New()

package main

import (
	"errors"
	"fmt"
)

type Histogram map[rune]int

type DNA string

func (d DNA) Counts() (Histogram, error) {
	h := Histogram{
		'A': 0,
		'C': 0,
		'G': 0,
		'T': 0,
	}
	fmt.Println(d)
	for _, c := range d {
		if _, present := h[c]; !present {
			errMsg := fmt.Sprintf("Invalid nucleotide! %q is not valid", c) //highlight-line
			return h, errors.New(errMsg) //highlight-line
		}
		h[c]++

	}
	return h, nil
}

func main() {
	fmt.Println(DNA.Counts("AGCTTA"))
	fmt.Println(DNA.Counts("AGCHHATTA"))
}

Here’s an interactive playground.

Approach 2: fmt.Errorf

The fmt package’s Errorf function allows for formatting descriptive error messages and is noted as an alternative to using errors.New in the docs:

nucleotide_count.go
package dna

import (
	"fmt"
)

// Histogram is a mapping from nucleotide to its count in given DNA.
type Histogram map[rune]int

// DNA is the the incoming string received by the function
type DNA string

// Counts generates a histogram of valid nucleotides in the given DNA.
// Returns an error if d contains an invalid nucleotide.
func (d DNA) Counts() (Histogram, error) {
	h := Histogram{
		'A': 0,
		'C': 0,
		'G': 0,
		'T': 0,
	}
	fmt.Println(d)
	for _, c := range d {
		if _, present := h[c]; !present {
			return h, fmt.Errorf("Invalid nucleotide! %q is not valid", c)		}
		h[c]++

	}
	return h, nil
}

Here’s a playground to play around with it yourself.

Conclusion

When the error is this simple, there’s not much advantage to bringing in a whole new package, but the error’s package has more to it than just creating a new error. Something to keep in mind.

On an unrelated note, you may notice that the result h does not have any letters - this is because it is keyed by runes, and each key is the Unicode code point value.


Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!