cbl

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2021 License: MIT Imports: 18 Imported by: 1

README

Go go.dev reference Go Report Card

cbl-go: Common Basic Library for Go

Install:

go get github.com/zhangjie2012/cbl-go@master # master version, recommand
go get -u github.com/zhangjie2012/cbl-go # new release version

Libraries include

  • Common
    • generator: uuid, session id, verfiy code
    • date expand
    • set operations: uniq, union, intersection, difference
    • yinyang: 公历、农历转换 deprecated
  • Data Structure
    • MultiError: muliple error expression
    • Pair: key value struct
  • cache: a redis wrapper, easy to use, include common operator, mq, dislock, set
  • datasize: byte to KB/MB/GB/TB/PB/EB, KiB/MiB/GiB/TiB/PiB/EiB and more elegent to string

Documentation

Overview

Package cbl is a common basic library for go.

  • `conv` 类型转换
  • `errcode` web 常用错误码
  • `gen` token/id 生成器
  • `ginext` gin 扩展(标准化)
  • `net` 网络扩展
  • `prom` prometheus middware
  • `strings` 字符串扩展
  • `wechat_mini` 微信小程序
  • `yinyang` 公历和农历转换
  • `set` set
  • `date` 日期扩展

More read `README.md`

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNone                = ""                      // no error 没错
	ErrBadRequest          = "Bad Request"           // bad request 错误的请求(参数错误)
	ErrInternalServerError = "Internal Server Error" // server logic error 服务器内部错误, 服务器自身逻辑问题
	ErrInvalidParams       = "Invalid Params"        // invalid params 参数格式错误
	ErrInvalidPassword     = "Invalid Password"      // invalid password 密码错误
	ErrInvalidVerifyCode   = "Invalid Verify Code"   // invalid verify code 验证码无效
	ErrLoginRequired       = "Login Required"        // need login 需要登录
	ErrNotFound            = "Not Found"             // resource not found 资源不存在
	ErrOutOfRange          = "Out Of Range"          // out of range 越界访问
	ErrPermissionDenied    = "Permission Denied"     // permission denied 权限不足
	ErrTooManyRequests     = "Too Many Requests"     // too many requests 请求过于频繁, 限流
)

Functions

func B2i

func B2i(b bool) int

B2i bool to int

func ConvYinYang

func ConvYinYang(year int, month int, leap int, day int) (time.Time, error)

ConvYinYang 农历转公历

func DayOfWeekCN added in v0.2.1

func DayOfWeekCN(n int) string

func DurationString added in v0.3.0

func DurationString(d time.Duration) string

func EndOfDay added in v0.1.11

func EndOfDay(t time.Time) time.Time

func EndOfMonth added in v0.3.1

func EndOfMonth(t time.Time) time.Time

EndOfMonth month end datetime

func EndOfWeek added in v0.3.1

func EndOfWeek(t time.Time) time.Time

EndOfWeek week last day

func EndOfYear added in v0.3.1

func EndOfYear(t time.Time) time.Time

func ErrorResponse

func ErrorResponse(c *gin.Context, err interface{})

func FormatDayTime added in v0.2.1

func FormatDayTime(t time.Time, showSec bool) string

func GenRSessionID

func GenRSessionID() string

GenRSessionID generate a random session id, most business scene recommand

func GenUSessionID

func GenUSessionID() string

GenSessionID generate a unique session id

func GenUUIDV1

func GenUUIDV1() string

GenUUIDV1 global unique id

func GenUUIDV4

func GenUUIDV4() string

GenUUIDV4 wikipedia said Randomly generated UUIDs have 122 random bits. One's annual risk of being hit by a meteorite is estimated to be one chance in 17 billion, that means the probability is about 0.00000000006 (6 x 10−11), equivalent to the odds of creating a few tens of trillions of UUIDs in a year and having one duplicate.

(most business scene recommand)

func GenVerifyCodeAny added in v0.2.8

func GenVerifyCodeAny(pool string, width int) string

GenVerifyCodeAny generate verify code, get from pool

func GenVerifyCodeCommon added in v0.2.8

func GenVerifyCodeCommon(width int) string

GenVerifyCodeCommon generate verify code, letter + number

func GenVerifyCodeNumber added in v0.2.8

func GenVerifyCodeNumber(width int) string

GenVerifyCodeNumber generate verify code, all is number

func GetOutboundIP

func GetOutboundIP() net.IP

GetOutboundIP get IP address code via https://stackoverflow.com/a/37382208/802815

func I2b

func I2b(i int) bool

I2b int t bool

func Int64SliceToString added in v0.2.9

func Int64SliceToString(a []int64, delim string) string

Int64SliceToString a int64 slice to string Ref: https://stackoverflow.com/questions/37532255/one-liner-to-transform-int-into-string

func IntSliceToString added in v0.2.9

func IntSliceToString(a []int, delim string) string

IntSliceToString a int64 slice to string Ref: https://stackoverflow.com/questions/37532255/one-liner-to-transform-int-into-string

func PromGinMiddleware

func PromGinMiddleware() gin.HandlerFunc

PromGinMiddleware prometheus for gin, register code:

router.GET("/metrics", gin.WrapH(promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{})))

func RangeMonth added in v0.3.1

func RangeMonth(t time.Time) []time.Time

RangeMonth get month every day date

func RangeWeek added in v0.3.1

func RangeWeek(t time.Time) []time.Time

func RangeYear added in v0.3.1

func RangeYear(t time.Time) []time.Time

func SliceDifference added in v0.1.8

func SliceDifference(a []string, b []string) []string

SliceDifference 差集

func SliceIntersection added in v0.1.8

func SliceIntersection(a []string, b []string) []string

SliceIntersection 交集

func SliceUnion added in v0.1.12

func SliceUnion(a []string, b []string) []string

SliceUnion 并集

func SliceUniq added in v0.1.12

func SliceUniq(a []string) []string

SliceUniq 去重

func SliceUniqInt added in v0.2.9

func SliceUniqInt(a []int) []int

func SliceUniqInt64 added in v0.2.9

func SliceUniqInt64(a []int64) []int64

func StartOfDay added in v0.1.8

func StartOfDay(t time.Time) time.Time

func StartOfMonth added in v0.3.1

func StartOfMonth(t time.Time) time.Time

StartOfMonth month start datetime

func StartOfWeek added in v0.3.1

func StartOfWeek(t time.Time) time.Time

StartOfWeek week first day

func StartOfYear added in v0.3.1

func StartOfYear(t time.Time) time.Time

func StringSplit added in v0.1.6

func StringSplit(source string, sep string) []string

StringSplit string split by `sep` the standard library `Split` when `source` is a empty string, will return a `[""]` slice. In fact, most of time, we want return a length equal 0 slice, `[]`

func SuccessResponse

func SuccessResponse(c *gin.Context, data interface{})

func TruncateString added in v0.2.8

func TruncateString(str string, length int) string

TruncateString Ref: https://dev.to/takakd/go-safe-truncate-string-9h0

Types

type Resp

type Resp struct {
	Code  int         `json:"code"`
	Data  interface{} `json:"data"`
	Error string      `json:"error"`
}

type WXMiniSession

type WXMiniSession struct {
	OpenID     string `json:"openid"`
	SessionKey string `json:"session_key"`
	UnionID    string `json:"unionid"`
}

type YangDate

type YangDate struct {
	Year  int `json:"year"`
	Month int `json:"month"`
	Day   int `json:"day"`
}

type YinDate

type YinDate struct {
	YearNum    int    `json:"year_num"`
	YearTian   string `json:"year_tian"`
	YearDi     string `json:"year_di"`
	YearZodiac string `json:"year_zodiac"`
	MonthNum   int    `json:"month_num"`
	MonthName  string `json:"month_name"`
	MonthLeap  bool   `json:"month_leap"`
	DayNum     int    `json:"day_num"`
	DayName    string `json:"day_name"`
	WeekDay    string `json:"weekday"`
	SolarTerm  string `json:"solarterm"`
}

func ConvYangYin

func ConvYangYin(year int, month int, day int) (*YinDate, error)

ConvYangYin 公历转农历

func (*YinDate) ToString1

func (y *YinDate) ToString1() string

func (*YinDate) ToString2

func (y *YinDate) ToString2() string

func (*YinDate) ToString3

func (y *YinDate) ToString3() string

Directories

Path Synopsis
Package cache based on NoSQL Redis, encapsulated common scenario.
Package cache based on NoSQL Redis, encapsulated common scenario.
Package datasize provide computer unit converter.
Package datasize provide computer unit converter.
package datastruct data structure extends for go standard library.
package datastruct data structure extends for go standard library.

Jump to

Keyboard shortcuts

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