相关文章推荐
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm calling the method with error type value (foo() in code example). I don't care this result. What's the rigth code style way to write? Errcheck linter makes me check this error.

//for example, same method may be called from imported entity
func foo() error {
   if err := someFunction(); err != nil {
       return err
   return nil
func process() {
   //linter doesn't like this
   foo()
   //this way leads to unused variable error
   err := foo()
   //is this clean way?
   _ = foo()
  return 

Yes, assigning it to a wildcard variable would be a good way to ignore the error. But the whole practice (of ignoring errors) is strongly discouraged. Here's what "Effective Go" has to say about this:

Occasionally you'll see code that discards the error value in order to ignore the error; this is terrible practice. Always check error returns; they're provided for a reason.

   // Bad! This code will crash if path does not exist.
    fi, _ := os.Stat(path)
    if fi.IsDir() {
        fmt.Printf("%s is a directory\n", path)
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

 
推荐文章