Documentation ¶
Overview ¶
Common and useful utils for the Go project development.
Inclusion criteria:
- Only rely on the Go standard package
- Functions or lightweight packages
- Non-business related general tools
Index ¶
- Constants
- Variables
- func AESDecrypt(cipherkey, ciphertext []byte) ([]byte, error)
- func AESEncrypt(cipherkey, src []byte) []byte
- func BytesToString(b []byte) string
- func CamelString(s string) string
- func CopyFile(dstName, srcName string) (written int64, err error)
- func EncryPassword(pwd string) (string, error)
- func ExtranetIP() (ip string, err error)
- func FileExists(name string) bool
- func GetFileSize(file *os.File) (int64, error)
- func GrepFile(patten string, filename string) (lines []string, err error)
- func IntranetIP() (string, error)
- func IsExportedName(name string) bool
- func IsExportedOrBuiltinType(t reflect.Type) bool
- func JsQueryEscape(s string) string
- func JsQueryUnescape(s string) (string, error)
- func Md5(b []byte) string
- func Md5string(in string) string
- func ObjectName(obj interface{}) string
- func PanicTrace(kb int) []byte
- func RandomBytes(n int) []byte
- func RelPath(targpath string) string
- func SearchFile(filename string, paths ...string) (fullpath string, err error)
- func SelfChdir()
- func SelfDir() string
- func SelfName() string
- func SelfPath() string
- func SnakeString(s string) string
- func StringToBytes(s string) []byte
- func TodayDir(file *os.File) string
- func TouchFile(fileName string) error
- func URLRandomString(n int) string
- func VerifyPassword(pwd string, encryptPwd string) (bool, error)
- func WalkDirs(targpath string, suffixes ...string) (dirlist []string)
- func WritePidFile(pidFile ...string)
- type Map
- type Random
Constants ¶
const (
Crs = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
)
Variables ¶
var DefSecretString = "rYtY0RD5hvN2T0McxjNWfH1MM7PExE0w"
DefSecretString ...
var DefaultPidFile = "logs/PID"
DEFAULT_PID_FILE the default PID file name
var Delimiter = func() string { if runtime.GOOS == "windows" { return "\\" } return "/" }()
Delimiter ... 文件夹目录分隔符
Functions ¶
func AESDecrypt ¶
AESDecrypt decrypts a piece of data. The cipherkey argument should be the AES key, either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256.
func AESEncrypt ¶
AESEncrypt encrypts a piece of data. The cipherkey argument should be the AES key, either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256.
func BytesToString ¶
BytesToString convert []byte type to string type.
func CamelString ¶
CamelString converts the accepted string to a camel string (xx_yy to XxYy)
func FileExists ¶
FileExists reports whether the named file or directory exists.
func GetFileSize ¶
GetFileSize 获取当前文件大小 kb >>10 , mb >>1e2
func GrepFile ¶
GrepFile like command grep -E for example: GrepFile(`^hello`, "hello.txt") \n is striped while read
func IsExportedName ¶
IsExportedName is this an exported - upper case - name?
func IsExportedOrBuiltinType ¶
IsExportedOrBuiltinType is this type exported or a builtin?
func JsQueryEscape ¶
JsQueryEscape escapes the string in javascript standard so it can be safely placed inside a URL query.
func JsQueryUnescape ¶
JsQueryUnescape does the inverse transformation of JsQueryEscape, converting %AB into the byte 0xAB and '+' into ' ' (space). It returns an error if any % is not followed by two hexadecimal digits.
func ObjectName ¶
func ObjectName(obj interface{}) string
ObjectName gets the type name of the object
func RandomBytes ¶
RandomBytes returns securely generated random bytes. It will panic if the system's secure random number generator fails to function correctly.
func SearchFile ¶
SearchFile Search a file in paths. this is often used in search config file in /etc ~/
func SnakeString ¶
SnakeString converts the accepted string to a snake string (XxYy to xx_yy)
func StringToBytes ¶
StringToBytes convert string type to []byte type. NOTE: panic if modify the member value of the []byte.
func TouchFile ¶
TouchFile If the file does not exist, it is created automatically. Do not do anything if the file already exists.
func URLRandomString ¶
URLRandomString returns a URL-safe, base64 encoded securely generated random string. It will panic if the system's secure random number generator fails to function correctly. The length n must be an integer multiple of 4, otherwise the last character will be padded with `=`.
func VerifyPassword ¶
VerifyPassword ... 验证一个密码
func WalkDirs ¶
WalkDirs traverses the directory, return to the relative path. You can specify the suffix.
func WritePidFile ¶
func WritePidFile(pidFile ...string)
WritePidFile writes the current PID to the specified file.
Types ¶
type Map ¶
type Map interface { // Load returns the value stored in the map for a key, or nil if no // value is present. // The ok result indicates whether value was found in the map. Load(key interface{}) (value interface{}, ok bool) // Store sets the value for a key. Store(key, value interface{}) // LoadOrStore returns the existing value for the key if present. // Otherwise, it stores and returns the given value. // The loaded result is true if the value was loaded, false if stored. LoadOrStore(key, value interface{}) (actual interface{}, loaded bool) // Range calls f sequentially for each key and value present in the map. // If f returns false, range stops the iteration. Range(f func(key, value interface{}) bool) // Random returns a pair kv randomly. // If exist=false, no kv data is exist. Random() (key, value interface{}, exist bool) // Delete deletes the value for a key. Delete(key interface{}) // Clear clears all current data in the map. Clear() // Len returns the length of the map. Len() int }
Map is a concurrent map with loads, stores, and deletes. It is safe for multiple goroutines to call a Map's methods concurrently.
type Random ¶
type Random struct {
// contains filtered or unexported fields
}
Random random string creater.
func (*Random) RandomString ¶
RandomString returns a base64 encoded securely generated random string. It will panic if the system's secure random number generator fails to function correctly. The length n must be an integer multiple of 4, otherwise the last character will be padded with `=`.