Are these errors happend?

While reading configuration from yaml I noticed that if file not present I’ve got any errors in the foolowing example.

	viper.SetConfigType("yaml")

	file, _ := os.Open("example.yaml") //this file does not exist
	defer file.Close()

	err := viper.ReadConfig(file)
	if err != nil {
		if _, ok := err.(viper.ConfigFileNotFoundError); ok {
			log.Fatal("configuration file not found")
		} else {
			log.Fatal(err)
		}
	}

None of the above errors happens. Is it not a ConfigFileNotFoundError if file not present?

2 Likes

Os.Open return a boolean value indicating if file exists but you are ignoring in your call.
You can then change the call to

file,err := os.Open("example.yaml") //this file does not exist
  if err != nil {
    log.Fatal(err)
  }
  defer file.Close()
4 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.