ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - sakirsensoy/genv: Genv is a library for Go (golang) that makes it easy to read and use environment variables in your projects. It also allows environment variables to be loaded from the .env file.
Genv is a library for Go (golang) that makes it easy to read and use environment variables in your projects. It also allows environment variables to be loaded from the .env file. - sakirsensoy/genv
Visit Site

GitHub - sakirsensoy/genv: Genv is a library for Go (golang) that makes it easy to read and use environment variables in your projects. It also allows environment variables to be loaded from the .env file.

GitHub - sakirsensoy/genv: Genv is a library for Go (golang) that makes it easy to read and use environment variables in your projects. It also allows environment variables to be loaded from the .env file.

genv

Build Status Go Report Card Coverage Status GoDoc

Genv is a library for Go (golang) that makes it easy to read and use environment variables in your projects. It also allows environment variables to be loaded from the .env file.

Installation

go get github.com/sakirsensoy/genv

Usage

Create a .env file in the root directory of your project and enter the environment variables you want to use:

# .env
APP_HOST=localhost
APP_PORT=1234
APP_DEBUG=true

In the meantime, it is optional to use the .env file. You can also send environment variables to your project in classic ways:

APP_HOST=localhost ./myproject

Rather than using your environment variables directly in your project, it is better to map and match them with a struct. Below you can see how we get our application parameters from environment variables:

// config/config.go
package config

import "github.com/sakirsensoy/genv"

type appConfig struct {
	Host string
	Port int
	Debug bool
}

var  App = &appConfig{
	Host: genv.Key("APP_HOST").String(),
	Port: genv.Key("APP_PORT").Default(8080).Int(),
	Debug: genv.Key("APP_DEBUG").Default(false).Bool(),
}

In main.go we first include the package that allows you to automatically load the environment variables from the .env file. Then we can include and use the parameters defined in config.go:

// main.go
package main

import (
	_ "github.com/sakirsensoy/genv/dotenv/autoload"

	"fmt"
	"myproject/config"
)

func  main() {
	fmt.Println(config.App.Host) // localhost
	fmt.Println(config.App.Port) // 1234
	fmt.Println(config.App.Debug) // true
}

Accessing Environment Variables

Genv provides an easy-to-use API for accessing environment variables.

First we specify the key to the variable want to access

var env = genv.Key("MY_VARIABLE")

Define default value (optional)

env = env.Default("default_value")

Finally, we specify the type of the environment variable and pass its contents to another variable

var myVariable = env.String()

Supported Variable Types

Genv provides support for the following data types:

  • String(): Returns data of String type
  • Int(): Returns data of Int32 type
  • Float(): Returns data of Float64 type
  • Bool(): Returns data of Bool type

For other types, you can use type conversion:

var stringValue = genv.Key("KEY").String()
var byteArrayValue = []byte(stringValue)

Contributing

Thanks in advance for your contributions :) I would appreciate it if you make sure that the API remains simple when developing.

code changes without tests will not be accepted

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

© Şakir Şensoy, 2019 ~ time.Now()

Released under the MIT License

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory