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:
errors
’ New
methodfmt
’s Errorf
methodpackage 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) return h, errors.New(errMsg) }
h[c]++
}
return h, nil
}
func main() {
fmt.Println(DNA.Counts("AGCTTA"))
fmt.Println(DNA.Counts("AGCHHATTA"))
}
Here’s an interactive playground.
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:
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.
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!