Skip to content

Go R1 Day 31

progress๐Ÿ”—

  • Learned a bit about idiomatic patterns wtih error handling.
  • Learned about inline block intiailization of variables using if err := method(); err != nil {...} approach.
  • Considered a bit more idiomatic patterns when I noticed excessibe nested if blocks.
tfdir := tf.Params().String("tfdir")
if tfdir != "" {
  tf.Logf("tfdir set to: [%s]", tfdir)
} else {
  tf.Errorf("๐Ÿงช failed to get tfdir parameter: [%v]", tfdir)
}

This would probably be more in alignment with Go standards by writing as:

tfdir := tf.Params().String("tfdir")
if tfdir == "" {
  tf.Errorf("๐Ÿงช failed to get tfdir parameter: [%v]", tfdir)
  return
}
tf.Logf("tfdir set to: [%s]", tfdir)

This reduces the noise and keeps things pretty flat.

When Should I Use One Liner if...else Statements in Go?)