How do you check for errors when working with files in Go?

  • if err != nil { log.Fatal(err) }
  • if error != nil { panic(error) }
  • if error != nil { return error }
  • if err { return err }
When working with files in Go, you should check for errors by using conditional statements. The correct option is if err != nil { log.Fatal(err) }. This checks if the err variable (commonly used for error handling) is not nil and, if not, logs the error and exits the program using log.Fatal(). Proper error handling is essential when dealing with file operations in Go.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *