Skip to content

Commit

Permalink
Merge pull request #1 from jaavier/base-project
Browse files Browse the repository at this point in the history
Base project
  • Loading branch information
jaavier committed Nov 6, 2022
2 parents 5ebb603 + 5e53560 commit 00440d0
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
22 changes: 21 additions & 1 deletion README.md
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"))
}
}
```
31 changes: 31 additions & 0 deletions dotenv.go
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
}
}
16 changes: 16 additions & 0 deletions example/program.go
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"))
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/jaavier/dotenv

go 1.17

0 comments on commit 00440d0

Please sign in to comment.