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, key ...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 InitCache()
- 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 MaxSliceInt64(slice []int64) int64
- func Md5(b []byte) string
- func Md5string(in string) string
- func MinSliceInt64(slice []int64) int64
- 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, key ...string) (bool, error)
- func WalkDirs(targpath string, suffixes ...string) (dirlist []string)
- type CacheInterface
- type CurrentIP
- type HTTPError
- type JSONTime
- func (p *JSONTime) GobDecode(data []byte) error
- func (p JSONTime) GobEncode() ([]byte, error)
- func (p JSONTime) MarshalJSON() ([]byte, error)
- func (p *JSONTime) Scan(v interface{}) error
- func (p JSONTime) String() string
- func (p *JSONTime) UnmarshalJSON(data []byte) error
- func (p JSONTime) Value() (driver.Value, error)
- type Logger
- type Logs
- type Map
- type MemoryCache
- func (m *MemoryCache) Bind(key string, bean interface{}) error
- func (m *MemoryCache) Del(key string) error
- func (m *MemoryCache) DelAll() error
- func (m *MemoryCache) Exists(key string) bool
- func (m *MemoryCache) Get(key string) ([]byte, error)
- func (m *MemoryCache) GetInt64(key string) (i int64, err error)
- func (m *MemoryCache) GetValue(key string) (string, error)
- func (m *MemoryCache) Remember(key string, set func() error) (b []byte, err error)
- func (m *MemoryCache) RememberBind(key string, bean interface{}, set func() error) error
- func (m *MemoryCache) Set(key string, value interface{}, exp time.Duration) error
- type Random
- type RedisCache
- func (r *RedisCache) Bind(key string, bean interface{}) error
- func (r *RedisCache) Del(key string) error
- func (r *RedisCache) DelAll() error
- func (r *RedisCache) Exists(key string) bool
- func (r *RedisCache) Get(key string) (b []byte, err error)
- func (r *RedisCache) GetInt64(key string) (int64, error)
- func (r *RedisCache) GetValue(key string) (string, error)
- func (r *RedisCache) JSONSet(key string, value interface{}, exp time.Duration) error
- func (r *RedisCache) Remember(key string, set func() error) (b []byte, err error)
- func (r *RedisCache) RememberBind(key string, bean interface{}, set func() error) error
- func (r *RedisCache) Set(key string, value interface{}, exp time.Duration) error
- type ReturnJSON
Constants ¶
const ( CONNECT = "CONNECT" DELETE = "DELETE" GET = "GET" HEAD = "HEAD" OPTIONS = "OPTIONS" PATCH = "PATCH" POST = "POST" PROPFIND = "PROPFIND" PUT = "PUT" TRACE = "TRACE" )
HTTP methods
const ( MIMEApplicationJSON = "application/json" MIMEApplicationJSONCharsetUTF8 = MIMEApplicationJSON + "; " + charsetUTF8 MIMEApplicationJavaScript = "application/javascript" MIMEApplicationJavaScriptCharsetUTF8 = MIMEApplicationJavaScript + "; " + charsetUTF8 MIMEApplicationXML = "application/xml" MIMEApplicationXMLCharsetUTF8 = MIMEApplicationXML + "; " + charsetUTF8 MIMETextXML = "text/xml" MIMETextXMLCharsetUTF8 = MIMETextXML + "; " + charsetUTF8 MIMEApplicationForm = "application/x-www-form-urlencoded" MIMEApplicationProtobuf = "application/protobuf" MIMEApplicationMsgpack = "application/msgpack" MIMETextHTML = "text/html" MIMETextHTMLCharsetUTF8 = MIMETextHTML + "; " + charsetUTF8 MIMETextPlain = "text/plain" MIMETextPlainCharsetUTF8 = MIMETextPlain + "; " + charsetUTF8 MIMEMultipartForm = "multipart/form-data" MIMEOctetStream = "application/octet-stream" )
MIME types
const ( HeaderAccept = "Accept" HeaderAcceptEncoding = "Accept-Encoding" HeaderAllow = "Allow" HeaderAuthorization = "Authorization" HeaderContentDisposition = "Content-Disposition" HeaderContentEncoding = "Content-Encoding" HeaderContentLength = "Content-Length" HeaderContentType = "Content-Type" HeaderCookie = "Cookie" HeaderSetCookie = "Set-Cookie" HeaderIfModifiedSince = "If-Modified-Since" HeaderLastModified = "Last-Modified" HeaderLocation = "Location" HeaderUpgrade = "Upgrade" HeaderVary = "Vary" HeaderWWWAuthenticate = "WWW-Authenticate" HeaderXForwardedFor = "X-Forwarded-For" HeaderXForwardedProto = "X-Forwarded-Proto" HeaderXForwardedProtocol = "X-Forwarded-Protocol" HeaderXForwardedSsl = "X-Forwarded-Ssl" HeaderXUrlScheme = "X-Url-Scheme" HeaderXHTTPMethodOverride = "X-HTTP-Method-Override" HeaderXRealIP = "X-Real-IP" HeaderXRequestID = "X-Request-ID" HeaderXRequestedWith = "X-Requested-With" HeaderServer = "Server" HeaderOrigin = "Origin" HeaderExpires = "Expires" HeaderCacheControl = "Cache-Control" HeaderPragma = "Pragma" // Access control HeaderAccessControlRequestMethod = "Access-Control-Request-Method" HeaderAccessControlRequestHeaders = "Access-Control-Request-Headers" HeaderAccessControlAllowOrigin = "Access-Control-Allow-Origin" HeaderAccessControlAllowMethods = "Access-Control-Allow-Methods" HeaderAccessControlAllowHeaders = "Access-Control-Allow-Headers" HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials" HeaderAccessControlExposeHeaders = "Access-Control-Expose-Headers" HeaderAccessControlMaxAge = "Access-Control-Max-Age" // Security HeaderStrictTransportSecurity = "Strict-Transport-Security" HeaderXContentTypeOptions = "X-Content-Type-Options" HeaderXXSSProtection = "X-XSS-Protection" HeaderXFrameOptions = "X-Frame-Options" HeaderContentSecurityPolicy = "Content-Security-Policy" HeaderXCSRFToken = "X-CSRF-Token" )
Headers
const ( KB int64 = 1 << (10 * iota) // 1 << (10*1) MB // 1 << (10*2) GB // 1 << (10*3) TB // 1 << (10*4) LogPackSize = 1 * MB LogBackUpDays = 3 )
const (
Crs = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
)
const (
Custom = "2006-01-02 15:04:05"
)
const (
UTF8 = "UTF-8"
)
Variables ¶
var ( DefCache CacheInterface MeCache *MemoryCache )
DefCache ...
var ( ErrUnsupportedMediaType = NewHTTPError(http.StatusUnsupportedMediaType) ErrNotFound = NewHTTPError(http.StatusNotFound) ErrForbidden = NewHTTPError(http.StatusForbidden) ErrMethodNotAllowed = NewHTTPError(http.StatusMethodNotAllowed) ErrStatusRequestEntityTooLarge = NewHTTPError(http.StatusRequestEntityTooLarge) ErrValidatorNotRegistered = errors.New("validator not registered") ErrRendererNotRegistered = errors.New("renderer not registered") ErrInvalidRedirectCode = errors.New("invalid redirect status code") ErrCookieNotFound = errors.New("cookie not found") )
Errors
var ( Cron = func() *cron.Cron { cb := cron.New() cb.Start() return cb }() )
var DefSecretString = "rYtY0RD5hvN2T0McxjNWfH1MM7PExE0w"
DefSecretString ...
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 EncryPassword ¶
EncryPassword ... 加密一个密码
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 ... 验证一个密码
Types ¶
type CacheInterface ¶
type CacheInterface interface { Get(key string) ([]byte, error) //if cannot get value,return 0 GetInt64(key string) (int64, error) //return "" if can't get value GetValue(key string) (string, error) //bind value to struct point Bind(key string, bean interface{}) error Set(key string, value interface{}, exp time.Duration) error Remember(key string, set func() error) ([]byte, error) RememberBind(key string, bean interface{}, set func() error) error Exists(key string) bool Del(key string) error DelAll() error }
CacheInterface ... 缓存接口
type CurrentIP ¶
func NewCurrentIP ¶
type HTTPError ¶
type HTTPError struct { Code int `json:"code"` Message interface{} `json:"message"` Internal error `json:"-"` //Errors returned by external dependencies can be stored }
HTTPError represents an error that occurred while handling a request.
func NewHTTPError ¶
NewHTTPError creates a new HTTPError instance.
type JSONTime ¶
JSONTime ...
func (*JSONTime) UnmarshalJSON ¶
UnmarshalJSON ...
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
func (*Logger) DelDirForDays ¶
func (l *Logger) DelDirForDays()
func (*Logger) GetGLogger ¶
type Logs ¶
type Logs interface { Printf(format string, args ...interface{}) Fatalf(format string, args ...interface{}) Panicf(format string, args ...interface{}) }
Logger defines the logging interface.
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 MemoryCache ¶
MemoryCache ...
func NewMCache ¶
func NewMCache(dbStr string) *MemoryCache
func (*MemoryCache) GetInt64 ¶
func (m *MemoryCache) GetInt64(key string) (i int64, err error)
GetInt64 ...
func (*MemoryCache) GetValue ¶
func (m *MemoryCache) GetValue(key string) (string, error)
GetValue ...
func (*MemoryCache) Remember ¶
func (m *MemoryCache) Remember(key string, set func() error) (b []byte, err error)
Remember ...
func (*MemoryCache) RememberBind ¶
func (m *MemoryCache) RememberBind(key string, bean interface{}, set func() error) error
RememberBind ...
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 `=`.
type RedisCache ¶
RedisCache ...
func (*RedisCache) GetValue ¶
func (r *RedisCache) GetValue(key string) (string, error)
GetValue ...
func (*RedisCache) JSONSet ¶
func (r *RedisCache) JSONSet(key string, value interface{}, exp time.Duration) error
JSONSet ... 将一个对象序列化成 json 字符串,并进行存储
func (*RedisCache) Remember ¶
func (r *RedisCache) Remember(key string, set func() error) (b []byte, err error)
Remember ...
func (*RedisCache) RememberBind ¶
func (r *RedisCache) RememberBind(key string, bean interface{}, set func() error) error
RememberBind ...
type ReturnJSON ¶
type ReturnJSON struct { Code int `json:"code"` StatusText string `json:"status_text"` Message interface{} `json:"message,omitempty"` }
func NewReturnJSON ¶
func NewReturnJSON() *ReturnJSON
func (*ReturnJSON) Ok ¶
func (r *ReturnJSON) Ok(i interface{}) *ReturnJSON
func (*ReturnJSON) Set ¶
func (r *ReturnJSON) Set(code int, msg interface{}) *ReturnJSON