-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from jaavier/base-project
Base project
- Loading branch information
Showing
4 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,21 @@ | ||
# dotenv | ||
# HOW TO USE | ||
Get the latest version of this module running `go get -d github.com/jaavier/dotenv` in your project directory | ||
|
||
```golang | ||
package example | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/jaavier/dotenv" | ||
) | ||
|
||
func main() { | ||
if err := dotenv.Load(".env"); err != nil { | ||
fmt.Println(err) | ||
} else { | ||
fmt.Println(os.Getenv("YOUR_SECRET_KEY_HERE")) | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package dotenv | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func Load(file string) error { | ||
fileContent, err := ioutil.ReadFile(file) | ||
if err != nil { | ||
return err | ||
} else { | ||
var variables = (strings.Split(string(fileContent), "\n")) | ||
for _, variable := range variables { | ||
var key, value string | ||
var content = strings.SplitAfter(variable, "=") | ||
if len(content) == 2 { | ||
key = strings.Replace(content[0], "=", "", -1) | ||
value = content[1] | ||
} else if len(content) > 2 { | ||
key = strings.Replace(content[0], "=", "", -1) | ||
value = strings.Join(content[1:], "") | ||
} | ||
if len(key) > 0 && len(value) > 0 { | ||
os.Setenv(key, value) | ||
} | ||
} | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package example | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/jaavier/dotenv" | ||
) | ||
|
||
func main() { | ||
if err := dotenv.Load(".env"); err != nil { | ||
fmt.Println(err) | ||
} else { | ||
fmt.Println(os.Getenv("YOUR_SECRET_KEY_HERE")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/jaavier/dotenv | ||
|
||
go 1.17 |