I want to add a configuration to my project and i have such a project structure:
app:
-cmd
- main
-app.go
-internal
- config
-config.go
-pkg
config.yaml
go.mod
go.sum
In the config file, I want to read config.yaml:
func GetConfig() *Config {
once.Do(func() {
instance = &Config{}
logger := logging.GetLogger("info")
logger.Info("start config initialisation")
if err := cleanenv.ReadConfig("config.yaml", instance); err != nil {
help, _ := cleanenv.GetDescription(instance, nil)
logger.Info(help)
logger.Fatal(err)
}
})
return instance
}
And I get an error: system cannot find the file specified. And to fix it, I need to move the configuration file to the folder where the application is launched, that is, to cmd/main And I wonder if it is possible to fix it somehow so that the file lies together with the initialization of the project along with go.mod and so on.I tried to specify different paths in the place of reading the file, but nothing worked.
I use the cleanenv library for reading
CodePudding user response:
wd,err := os.Getwd()
if err != nil {
// handle error
}
parentTop := filepath.Dir(wd) // if your working directory is top level you can skip this step
// and wd instead
then use the parent and parentTop "/pkg/config.yaml"