Env

package module
v0.0.0-...-000be02 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2019 License: MIT Imports: 10 Imported by: 0

README

环境遍历读取包

pakcage : hewei-github/go-dotenv

license go package function test

支持.env 文件 .ini 文件读取 支持自动读取环境变量,支持配置动态变量(${xxx})

.env 
 APP_NAME=test
 APP_ROOT=./
 APP_ENV=${APP_NAME}

#代码
eg : 
    
    import (
        "github.com/hewei-github/go-dotenv"
        "os"
     )
     
     var file :=".env"
     app :=Env.RootEnv.Load(file)
     if app == nil{
        // failed read env key value config
        return 
     }
     
     if "test"==app.Get("APP_NAME") {
         // ok
     }
     
     if app.IsSet("test") {
        // ok
     }
     
     if app.Get("os") == os.GetEnv("os") {
        // ok
     }

Documentation

Index

Constants

View Source
const DefAssignOpt = "=" // 键值分隔符

包常量

View Source
const DefVarExpress = "(\\$\\{([^(\\{\\})]+)\\})" // 动态模版变量匹配
View Source
const LineDiv = "\r\n" // 换行分隔符

Variables

View Source
var SupportExtArr = [2]string{"env", "ini"} // 支持文件后缀

Functions

func FileGetContent

func FileGetContent(file string) (content string, err error)

读取文件

func FilePutContent

func FilePutContent(file string, data string) int

写文件

func GetType

func GetType(value interface{}) string

获取类型

func IsType

func IsType(value interface{}, ty string) bool

是否对应类型

func ParseKeyValueByExpressStr

func ParseKeyValueByExpressStr(data string, assignOpt string) (map[string]string, bool)

解析表达式字符串

func ParseStrLine

func ParseStrLine(data string, assignOpt string) (map[string]string, error)

换行拆解

func Read

func Read(file string, buf interface{}) (data []byte, err error)

读文件

func StrToAny

func StrToAny(value string) interface{}

@todo 待完善

func ToString

func ToString(value interface{}) string

转字符串

func Write

func Write(file string, data string) int

写文件

Types

type DefaultParser

type DefaultParser struct{}

默认解析器

func (DefaultParser) GetContent

func (_ DefaultParser) GetContent(file string, assignOpt string, varExpress string) (map[string]interface{}, error)

解析器

type File

type File struct {
	// contains filtered or unexported fields
}

文件

func GetFile

func GetFile(path string) *File

创建

func (*File) AbsolutePath

func (file *File) AbsolutePath() string

获取绝对路径

func (*File) BaseName

func (file *File) BaseName() string

获取基础文件信息

func (*File) Extension

func (file *File) Extension() string

文件扩展类型

func (*File) FileType

func (file *File) FileType() string

文件类型

func (*File) IsDir

func (file *File) IsDir() (bool, error)

是否文件目录

func (*File) IsExist

func (file *File) IsExist() bool

文件是否存在

func (*File) PathInfo

func (file *File) PathInfo() map[string]string

path-info

type FileSchema

type FileSchema interface {
	IsExist() bool
	PathInfo() map[string]string
	IsDir() (bool, error)
	BaseName() string
	AbsolutePath() string
	FileType() string
	Extension() string
}

文件协议

type IEnvironment

type IEnvironment interface {
	Get(key string, def interface{}) interface{}
	Set(key string, value interface{}) *ObjectEnv
	Load(path string) *ObjectEnv
}

环境接口

type ObjectEnv

type ObjectEnv struct {
	// contains filtered or unexported fields
}

环境变量

var RootEnv *ObjectEnv // 引用根

func GetEnvApp

func GetEnvApp(path string) (*ObjectEnv, error)

创建

func (*ObjectEnv) Get

func (app *ObjectEnv) Get(key string, def interface{}) interface{}

获取

func (*ObjectEnv) GetAll

func (app *ObjectEnv) GetAll() map[string]string

获取所有

func (*ObjectEnv) IsSet

func (app *ObjectEnv) IsSet(key string) bool

是否存在

func (*ObjectEnv) IsSupport

func (app *ObjectEnv) IsSupport(path string) bool

是否支持

func (ObjectEnv) Load

func (app ObjectEnv) Load(path string) *ObjectEnv

加载

func (*ObjectEnv) Save

func (app *ObjectEnv) Save(file string, mode os.FileMode) int

保存

func (*ObjectEnv) Set

func (app *ObjectEnv) Set(key string, value interface{}) *ObjectEnv

设置

func (*ObjectEnv) Size

func (app *ObjectEnv) Size() int

配置键个数

func (*ObjectEnv) Unset

func (app *ObjectEnv) Unset(key string) bool

删除键

type ParserService

type ParserService interface {
	GetContent(file string, assignOpt string, varExpress string) (map[string]interface{}, error)
}

解析器

type Str

type Str string

字符串

func (Str) StrToAny

func (_ Str) StrToAny(value string) interface{}

字符串转其他类型

func (Str) ToString

func (_ Str) ToString(value interface{}) string

其他类型转字符串

type StrInterface

type StrInterface interface {
	ToString(value interface{}) string
	StrToAny(value string) interface{}
}

字符串 类型

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL