diff --git a/godotenv.go b/godotenv.go index 84e2ff9..b811130 100644 --- a/godotenv.go +++ b/godotenv.go @@ -16,8 +16,8 @@ and all the env vars declared in .env will be avaiable through os.Getenv("SOME_E package godotenv import ( + "bufio" "errors" - "io/ioutil" "os" "strings" ) @@ -87,14 +87,21 @@ func loadFile(filename string) (err error) { } func readFile(filename string) (envMap map[string]string, err error) { - content, err := ioutil.ReadFile(filename) + file, err := os.Open(filename) + + // content, err := ioutil.ReadFile(filename) if err != nil { return } + defer file.Close() envMap = make(map[string]string) - lines := strings.Split(string(content), "\n") + var lines []string + scanner := bufio.NewScanner(file) + for scanner.Scan() { + lines = append(lines, scanner.Text()) + } for _, fullLine := range lines { if !isIgnoredLine(fullLine) { diff --git a/godotenv_test.go b/godotenv_test.go index c6d9f2d..705855c 100644 --- a/godotenv_test.go +++ b/godotenv_test.go @@ -32,7 +32,8 @@ func loadEnvAndCompareValues(t *testing.T, envFileName string, expectedValues ma func TestLoadWithNoArgsLoadsDotEnv(t *testing.T) { err := Load() - if err.Error() != "open .env: no such file or directory" { + pathError := err.(*os.PathError) + if pathError == nil || pathError.Op != "open" || pathError.Path != ".env"{ t.Errorf("Didn't try and open .env by default") } }