Home >Backend Development >Golang >Can't read .env file without absolute path
When I try to read the .env file, it doesn't work without using an absolute path.
func init() { err := godotenv.load(".env") //<--fails //err := godotenv.load("./.env") //<--fails //err := godotenv.load("/home/peter/documents/tests/configuration/.env") //<--works if err != nil { panic(err) } config = getconfig() }
I get panic: open .env: no such file or directory
But the file is there
$ ls -a . .. config.go .env
Any clues?
One potential problem is that the directory where the executable is run is different from the directory where the .env file is located. Can you verify this?
The above is the detailed content of Can't read .env file without absolute path. For more information, please follow other related articles on the PHP Chinese website!