Documentation ¶
Overview ¶
Package ess provides a essentials and helper for the application development and usage. Such encoding, secure string, secure random string, filepath access, GUID, etc.
Index ¶
- Constants
- Variables
- func ApplyFileMode(target string, mode os.FileMode) error
- func BytesToStr(value int64) string
- func CloseQuietly(c ...interface{})
- func CopyDir(dest, src string, excludes Excludes) error
- func CopyFile(dest, src string) (int64, error)
- func DecodeBase64(v []byte) ([]byte, error)
- func DeleteFiles(files ...string) (errs []error)
- func DirsPath(basePath string, recursive bool) (pdirs []string, err error)
- func DirsPathExcludes(basePath string, recursive bool, excludes Excludes) (pdirs []string, err error)
- func EncodeToBase64(v []byte) []byte
- func FilesPath(basePath string, recursive bool) (files []string, err error)
- func FilesPathExcludes(basePath string, recursive bool, excludes Excludes) (files []string, err error)
- func GenerateRandomKey(length int) []byte
- func GenerateSecureRandomKey(length int) []byte
- func GoPath() (string, error)
- func IsAbsURL(rawurl string) bool
- func IsDir(path string) bool
- func IsDirEmpty(path string) bool
- func IsFileExists(filename string) bool
- func IsImportPathExists(path string) bool
- func IsInGoRoot(path string) bool
- func IsRelativeURL(rawurl string) bool
- func IsSliceContainsString(strSlice []string, search string) bool
- func IsStrEmpty(v string) bool
- func IsVaildURL(rawurl string) bool
- func LineCnt(fileName string) int
- func LineCntr(r io.Reader) int
- func LookExecutable(name string) bool
- func MkDirAll(path string, mode os.FileMode) error
- func NewGUID() string
- func RandomString(length int) string
- func SecureRandomString(length int) string
- func StrToBytes(value string) (int64, error)
- func StripExt(name string) string
- func Walk(srcDir string, walkFn filepath.WalkFunc) error
- func Zip(dest, src string) error
- type CallerInfo
- type Excludes
- type FmtFlag
- type FmtFlagPart
- type FunctionInfo
- type Valuer
Constants ¶
const ( ByteSize = 1.0 KiloByteSize = 1024 * ByteSize MegaByteSize = 1024 * KiloByteSize GigaByteSize = 1024 * MegaByteSize TeraByteSize = 1024 * GigaByteSize )
Byte unit value
const StringEmpty = ""
StringEmpty is empty string constant. Using `ess.StringEmpty` instead of "".
Variables ¶
var ( // FmtFlagSeparator is used parse flags pattern. FmtFlagSeparator = "%" // FmtFlagValueSeparator is used to parse into flag and value. FmtFlagValueSeparator = ":" )
var ( ErrGoPathIsNotSet = errors.New("GOPATH environment variable is not set. " + "Please refer to https://golang.org/doc/code.html to configure your Go environment") ErrDirNotInGoPath = errors.New("current directory is outside of GOPATH") )
Required variables
var ErrBase64Decode = errors.New("encoding/base64: decode error")
ErrBase64Decode returned when given string unable to do base64 decode.
Functions ¶
func ApplyFileMode ¶
ApplyFileMode applies the given file mode to the target{file|directory}
func BytesToStr ¶
BytesToStr method return given bytes size into readable string format.
For e.g.: 2097152 bytes ==> 2MB
func CloseQuietly ¶
func CloseQuietly(c ...interface{})
CloseQuietly closes `io.Closer` quietly. Very handy, where you do not care about error while `Close()` and helpful for code quality too.
func CopyDir ¶
CopyDir copies entire directory, sub directories and files into destination and it excludes give file matches
func DecodeBase64 ¶
DecodeBase64 method decodes given base64 into bytes. Reference: https://github.com/golang/go/blob/master/src/encoding/base64/base64.go#L384
func DeleteFiles ¶
DeleteFiles method deletes give files or directories. ensure your supplying appropriate paths.
func DirsPath ¶
DirsPath method returns all directories absolute path from given base path recursively.
func DirsPathExcludes ¶
func DirsPathExcludes(basePath string, recursive bool, excludes Excludes) (pdirs []string, err error)
DirsPathExcludes method returns all directories absolute path from given base path recursively excluding the excludes list.
func EncodeToBase64 ¶
EncodeToBase64 method encodes given bytes into base64 bytes. Reference: https://github.com/golang/go/blob/master/src/encoding/base64/base64.go#L169
func FilesPathExcludes ¶
func FilesPathExcludes(basePath string, recursive bool, excludes Excludes) (files []string, err error)
FilesPathExcludes method returns all files absolute path from given base path recursively excluding the excludes list.
func GenerateRandomKey ¶
GenerateRandomKey method generates the random bytes for given length using `math/rand.Source` and byte mask. StackOverflow Ref - http://stackoverflow.com/a/31832326
func GenerateSecureRandomKey ¶
GenerateSecureRandomKey method generates the random bytes for given length using `crypto/rand`.
func GoPath ¶
GoPath returns GOPATH in context with current working directory otherwise it returns first directory from GOPATH
func IsDir ¶
IsDir returns true if the given `path` is directory otherwise returns false. Also returns false if path is not exists
func IsDirEmpty ¶
IsDirEmpty returns true if the given directory is empty also returns true if directory not exists. Otherwise returns false
func IsFileExists ¶
IsFileExists return true is file or directory is exists, otherwise returns false. It also take cares of symlink path as well
func IsImportPathExists ¶
IsImportPathExists returns true if import path found in the GOPATH otherwise returns false
func IsInGoRoot ¶
IsInGoRoot returns true if given path has prefix of GOROOT otherwise false
func IsRelativeURL ¶
IsRelativeURL method returns true if given raw URL is relative URL otherwise false.
func IsSliceContainsString ¶
IsSliceContainsString method checks given string in the slice if found returns true otherwise false.
func IsStrEmpty ¶
IsStrEmpty returns true if strings is empty otherwise false
func IsVaildURL ¶
IsVaildURL method returns true if given raw URL gets parsed without any errors otherwise false.
func LookExecutable ¶
LookExecutable looks for an executable binary named file in the directories named by the PATH environment variable.
func NewGUID ¶
func NewGUID() string
NewGUID method returns a new Globally Unique Identifier (GUID).
The 12-byte `UniqueId` consists of-
- 4-byte value representing the seconds since the Unix epoch,
- 3-byte machine identifier,
- 2-byte process id, and
- 3-byte counter, starting with a random value.
Uses Mongo Object ID algorithm to generate globally unique ids - https://docs.mongodb.com/manual/reference/method/ObjectId/
func RandomString ¶
RandomString method generates the random string for given length using `math/rand.Source` and byte mask.
func SecureRandomString ¶
SecureRandomString method generates the random string for given length using `crypto/rand`.
func StrToBytes ¶
StrToBytes method returns bytes value for given string value.
For e.g.: 2mb ==> 2097152 bytes 2MB ==> 2097152 bytes 2MiB ==> 2097152 bytes
func StripExt ¶
StripExt method returns name of the file without extension.
E.g.: index.html => index
Types ¶
type CallerInfo ¶
type CallerInfo struct { QualifiedName string FunctionName string FileName string File string Line int }
CallerInfo struct stores Go caller info
func GetCallerInfo ¶
func GetCallerInfo() *CallerInfo
GetCallerInfo method returns caller's QualifiedName, FunctionName, File, FileName, Line Number.
type Excludes ¶
type Excludes []string
Excludes is handly filepath match manipulation
type FmtFlagPart ¶
FmtFlagPart is indiviual flag details
For e.g.: part := FmtFlagPart{ Flag: FmtFlagTime, Name: "time", Format: "2006-01-02 15:04:05.000", }
func ParseFmtFlag ¶
func ParseFmtFlag(pattern string, fmtFlags map[string]FmtFlag) ([]FmtFlagPart, error)
ParseFmtFlag it parses the given pattern, format flags into format flag parts.
For e.g.: %time:2006-01-02 15:04:05.000 %level:-5 %message %clientip %reqid %reqtime %restime %resstatus %ressize %reqmethod %requrl %reqhdr:Referer %reshdr:Server
type FunctionInfo ¶
FunctionInfo structs Go function info
func GetFunctionInfo ¶
func GetFunctionInfo(f interface{}) (fi *FunctionInfo)
GetFunctionInfo method returns the function name for given interface value.