From 5ca76b7e5abf58f282869e67560b3e8c415b2781 Mon Sep 17 00:00:00 2001 From: Luis Davim Date: Sun, 22 May 2022 23:59:48 +0100 Subject: [PATCH] feat: add function to read directly from a file --- gotenv.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/gotenv.go b/gotenv.go index bcf4ba5..4b8c84e 100644 --- a/gotenv.go +++ b/gotenv.go @@ -116,6 +116,17 @@ func StrictParse(r io.Reader) (Env, error) { return strictParse(r, false) } +// Read is a function to parse a file line by line and returns the valid Env key/value pair of valid variables. +// It expands the value of a variable from the environment variable but does not set the value to the environment itself. +// This function is skipping any invalid lines and only processing the valid one. +func Read(filename string) (Env, error) { + f, err := os.Open(filename) + if err != nil { + return nil, err + } + return strictParse(f, false) +} + //Unmarshal reads a string line by line and returns the valid Env key/value pair of valid variables. // It expands the value of a variable from the environment variable but does not set the value to the environment itself. // This function is returning an error if there are any invalid lines.