Can't read .env file without absolute path

王林
Release: 2024-02-05 11:49:35
forward
846 people have browsed it

无法读取没有绝对路径的 .env 文件

Question content

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()
}
Copy after login

I get panic: open .env: no such file or directory But the file is there

$ ls -a
.  ..  config.go  .env
Copy after login

Any clues?


Correct Answer


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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template