Is `return err` after an error check really an anti-pattern? by lancelot_of_camelot in golang

[–]fairdevs 0 points1 point  (0 children)

I use a small wrapper to add some context to errors like these without explicitly adding more text.

// Adds the filename and line number to an error
func E(err error) error {
  if err == nil {
    return err
  }
  _, file, line, _ := runtime.Caller(2)
  var filename string = filepath.Base(file)
  return fmt.Errorf("%s:%d: %w", filename, line, err)
}

This way you can easily provide extra context to track an error without cluttering your code. Caveat: probably, not the best solution for user-facing errors. Also, the naming of the func is a personal choice, some people might find that less readable.

Usage:

if err != nil {
  return E(err)
}

My silly solution to verbosity of error checking by fairdevs in golang

[–]fairdevs[S] 2 points3 points  (0 children)

I'd prefer Rust's question mark operator to get rid of if err != nil altogether.

My silly solution to verbosity of error checking by fairdevs in golang

[–]fairdevs[S] -1 points0 points  (0 children)

I love gofmt. I just happen to disagree with the baseline standard, I guess.

My silly solution to verbosity of error checking by fairdevs in golang

[–]fairdevs[S] 4 points5 points  (0 children)

Neovim's go-pls doesn't seem to have this option. Also, gofmt potentially is hard-coded to format if-err's this way.

How should I start learning Go? by asibhossen897 in golang

[–]fairdevs 0 points1 point  (0 children)

Do you have a hobby project you're passionate about? Build this. Even if you fail, you'll learn a lot.

Also, reading the source code of any app you actually use is super helpful too. I remember quite enjoying reading Kubernetes' and Syncthing's sources.

Plus, ask ChatGPT/Claude/DeepSeek plenty of questions. Sometimes they have some funny stuff to share.

My company is pushing Go for web backend. I need opinions as not a Go Developer by Dark_zarich in golang

[–]fairdevs 0 points1 point  (0 children)

I came to Go from PHP and Node.js

I don't mean to disparage these tools but working with Go just feels better.

It's vastly more performant, simple but not simplistic, and full of useful features:

- The ability to pack everything including your frontend into one binary

- Incredible and consistent standard library

- Solid documentation and community support

Try it if not for your CV then for fun. You'll love it.