gofutils

package
v0.0.0-...-40c044f Latest Latest
Warning

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

Go to latest
Published: May 16, 2019 License: Apache-2.0 Imports: 30 Imported by: 0

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

View Source
const (
	Crs = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
)

Variables

View Source
var DefSecretString = "rYtY0RD5hvN2T0McxjNWfH1MM7PExE0w"

DefSecretString ...

View Source
var DefaultPidFile = "logs/PID"

DEFAULT_PID_FILE the default PID file name

View Source
var Delimiter = func() string {
	if runtime.GOOS == "windows" {
		return "\\"
	}
	return "/"
}()

Delimiter ... 文件夹目录分隔符

Functions

func AESDecrypt

func AESDecrypt(cipherkey, ciphertext []byte) ([]byte, error)

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

func AESEncrypt(cipherkey, src []byte) []byte

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

func BytesToString(b []byte) string

BytesToString convert []byte type to string type.

func CamelString

func CamelString(s string) string

CamelString converts the accepted string to a camel string (xx_yy to XxYy)

func CopyFile

func CopyFile(dstName, srcName string) (written int64, err error)

CopyFile ...

func EncryPassword

func EncryPassword(pwd string) (string, error)

EncryPassword ... 加密一个密码

func ExtranetIP

func ExtranetIP() (ip string, err error)

ExtranetIP get external IP addr.

func FileExists

func FileExists(name string) bool

FileExists reports whether the named file or directory exists.

func GetFileSize

func GetFileSize(file *os.File) (int64, error)

GetFileSize 获取当前文件大小 kb >>10 , mb >>1e2

func GrepFile

func GrepFile(patten string, filename string) (lines []string, err error)

GrepFile like command grep -E for example: GrepFile(`^hello`, "hello.txt") \n is striped while read

func IntranetIP

func IntranetIP() (string, error)

IntranetIP get internal IP addr.

func IsExportedName

func IsExportedName(name string) bool

IsExportedName is this an exported - upper case - name?

func IsExportedOrBuiltinType

func IsExportedOrBuiltinType(t reflect.Type) bool

IsExportedOrBuiltinType is this type exported or a builtin?

func JsQueryEscape

func JsQueryEscape(s string) string

JsQueryEscape escapes the string in javascript standard so it can be safely placed inside a URL query.

func JsQueryUnescape

func JsQueryUnescape(s string) (string, error)

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 Md5

func Md5(b []byte) string

Md5 returns the MD5 checksum string of the data.

func Md5string

func Md5string(in string) string

Md5string ...

func ObjectName

func ObjectName(obj interface{}) string

ObjectName gets the type name of the object

func PanicTrace

func PanicTrace(kb int) []byte

PanicTrace trace panic stack info.

func RandomBytes

func RandomBytes(n int) []byte

RandomBytes returns securely generated random bytes. It will panic if the system's secure random number generator fails to function correctly.

func RelPath

func RelPath(targpath string) string

RelPath gets relative path.

func SearchFile

func SearchFile(filename string, paths ...string) (fullpath string, err error)

SearchFile Search a file in paths. this is often used in search config file in /etc ~/

func SelfChdir

func SelfChdir()

SelfChdir switch the working path to my own path.

func SelfDir

func SelfDir() string

SelfDir gets compiled executable file directory.

func SelfName

func SelfName() string

SelfName ...

func SelfPath

func SelfPath() string

SelfPath gets compiled executable file absolute path.

func SnakeString

func SnakeString(s string) string

SnakeString converts the accepted string to a snake string (XxYy to xx_yy)

func StringToBytes

func StringToBytes(s string) []byte

StringToBytes convert string type to []byte type. NOTE: panic if modify the member value of the []byte.

func TodayDir

func TodayDir(file *os.File) string

TodayDir 获取当前日期目录

func TouchFile

func TouchFile(fileName string) error

TouchFile If the file does not exist, it is created automatically. Do not do anything if the file already exists.

func URLRandomString

func URLRandomString(n int) string

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

func VerifyPassword(pwd string, encryptPwd string) (bool, error)

VerifyPassword ... 验证一个密码

func WalkDirs

func WalkDirs(targpath string, suffixes ...string) (dirlist []string)

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.

func AtomicMap

func AtomicMap() Map

AtomicMap creates a concurrent map with amortized-constant-time loads, stores, and deletes. It is safe for multiple goroutines to call a atomicMap's methods concurrently. From go v1.9 sync.Map.

func RwMap

func RwMap(capacity ...int) Map

RwMap creates a new concurrent safe map with sync.RWMutex. The normal Map is high-performance mapping under low concurrency conditions.

type Random

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

Random random string creater.

func NewRandom

func NewRandom(encoderSeed string, ignore ...byte) *Random

NewRandom creates a new padded Encoding defined by the given alphabet.

func (*Random) RandomString

func (r *Random) RandomString(n int) string

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 `=`.

Directories

Path Synopsis
errors is improved errors package.
errors is improved errors package.

Jump to

Keyboard shortcuts

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