Are these errors happend?


(cinematik) #1

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?


(Yamil Bracho) #2

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()