utils

package module
v0.0.0-...-f0534e9 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2020 License: MIT Imports: 19 Imported by: 0

README

utils

common tools for golang

Documentation

Index

Constants

View Source
const (
	// sniff Length, use for detect file mime type
	MimeSniffLen = 512
)

Variables

View Source
var (
	ErrConvertFail = errors.New("convert data type is failure")
)

Functions

func Base62

func Base62(s int) string

Base64 generates a random base62 string with length of n

func Base64

func Base64(n int) string

Base64 generates a random base64 string with length of n

func Bytes

func Bytes(n int) []byte

Bytes generates n random bytes

func CheckPasswordHash

func CheckPasswordHash(password, hash string) bool

func CreateFile

func CreateFile(fpath string, filePerm, dirPerm os.FileMode) (*os.File, error)

CreateFile if not exists Usage:

CreateFile("path/to/file.txt", 0664, 0666)

func DeleteIfFileExist

func DeleteIfFileExist(fpath string) error

DeleteIfFileExist operate

func FileExists

func FileExists(path string) bool

FileExists reports whether the named file or directory exists. Deprecated

please use PathExists() or IsFile() instead it

func Float64

func Float64(in interface{}) (float64, error)

func GetEnv

func GetEnv(env map[string]string, key string, defaultVal string) string

func GetEnvAsBool

func GetEnvAsBool(env map[string]string, name string, defaultVal bool) bool

func GetEnvAsInt

func GetEnvAsInt(env map[string]string, name string, defaultVal int) int

func GetEnvAsSlice

func GetEnvAsSlice(env map[string]string, name string, defaultVal []string, sep string) []string

func GetEnvAsUInt

func GetEnvAsUInt(env map[string]string, name string, defaultVal uint) uint

func HashPassword

func HashPassword(password string) (string, error)

func Hex

func Hex(n int) string

Hex generates a random hex string with length of n e.g: 67aab2d956bd7cc621af22cfb169cba8

func HowLongAgo

func HowLongAgo(sec int64) string

HowLongAgo format a seconds, get how lang ago

func Int

func Int(in interface{}) (int, error)

func Int64

func Int64(in interface{}) (int64, error)

func IsAbsPath

func IsAbsPath(filepath string) bool

IsAbsPath is abs path.

func IsDir

func IsDir(path string) bool

IsDir reports whether the named directory exists.

func IsFile

func IsFile(path string) bool

IsFile reports whether the named file or directory exists.

func IsImageFile

func IsImageFile(path string) bool

IsImageFile check file is image file.

func IsNum

func IsNum(a string) bool

The IsNum judges string is number or not.

func IsZipFile

func IsZipFile(filepath string) bool

IsZipFile check is zip file. from https://blog.csdn.net/wangshubo1989/article/details/71743374

func JsonpToJson

func JsonpToJson(json string) string

JsonpToJson modify jsonp string to json string Example: foobar({a:"1",b:2}) to {"a":"1","b":2}

func MimeType

func MimeType(path string) (mime string)

MimeType get File Mime Type name. eg "image/png"

func Mkdir

func Mkdir(dirPath string, perm os.FileMode) error

Mkdir alias of os.MkdirAll()

func MustFloat64

func MustFloat64(in interface{}) float64

func MustInt

func MustInt(in interface{}) int

func MustInt64

func MustInt64(in interface{}) int64

func MustUint

func MustUint(in interface{}) uint

func MustUint64

func MustUint64(in interface{}) uint64

func PathExists

func PathExists(path string) bool

PathExists reports whether the named file or directory exists.

func RandomNumber

func RandomNumber(min, max int) string

func ReaderMimeType

func ReaderMimeType(r io.Reader) (mime string)

ReaderMimeType get the io.Reader mimeType Usage:

file, err := os.Open(filepath)
if err != nil {
	return
}
mime := ReaderMimeType(file)

func String

func String(n int, letters ...string) string

String generates a random string using only letters provided in the letters parameter if user ommit letters parameters, this function will use defLetters instead

func ToFloat64

func ToFloat64(in interface{}) (f64 float64, err error)

func ToInt

func ToInt(in interface{}) (iVal int, err error)

func ToInt64

func ToInt64(in interface{}) (i64 int64, err error)

func ToUint

func ToUint(in interface{}) (u uint, err error)

func ToUint64

func ToUint64(in interface{}) (u64 uint64, err error)

func Uint

func Uint(in interface{}) (uint, error)

func Uint64

func Uint64(in interface{}) (uint64, error)

func Unzip

func Unzip(archive, targetDir string) (err error)

Unzip a zip archive from https://blog.csdn.net/wangshubo1989/article/details/71743374

func XML2MapStr

func XML2MapStr(xmlDoc string) map[string]string

simple xml to string support utf8

Types

This section is empty.

Jump to

Keyboard shortcuts

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