cFunc

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2024 License: Apache-2.0 Imports: 30 Imported by: 0

README

公共基础函数

  • md5加密
  • 编码转换
  • 追加写入文件 便于调试
  • 获取IP
  • 判断本地IP是否内网【局限于常用环境】
  • 对数字求mod值
  • 获取一个可用的端口号

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckCustomStructPtrExport

func CheckCustomStructPtrExport(dataPtr any) error

CheckCustomStructPtrExport 检测自定义struct指针参数并且可导出

func CheckMobile

func CheckMobile(mobile string) bool

CheckMobile 校验手机号码

func ClientIP

func ClientIP(r *http.Request) string

ClientIP 尽最大努力实现获取客户端 IP 的算法。 解析 X-Real-IP 和 X-Forwarded-For 以便于反向代理(nginx 或 haproxy)可以正常工作。

func ComparePoint

func ComparePoint[T comparable](t1, t2 T) bool

ComparePoint 比较数据是否相同 相同返回true

> - 布尔型、数值类型、字符串类型、指针类型和channel是严格可比较的。

> - 如果结构体类型的所有字段的类型都是严格可比较的,那么该结构体类型就是严格可比较的。

> - 如果数组元素的类型是严格可比较的,那么该数组类型就是严格可比较的。

> - 如果类型形参的类型集合中的所有类型都是严格可比较的,那么该类型形参就是严格可比较的。

func ConvertStr

func ConvertStr(col string) string

ConvertStr 将数据库中的表字段 转换为go中使用的名称

func CreateOrderId

func CreateOrderId(id int64) string

func Daemon

func Daemon()

Daemon 讲进程放入后台执行

func Date

func Date(fmt string, unix int64) string

Date 返回格式化后的时间字符串 format 支持PHP的时间格式 unix 时间戳默认为当前时间

func Div

func Div(num1, num2, point int64) float64

Div 计算除法 保留小数位数

func FloatStringToInt

func FloatStringToInt(str string, point float64) int64

FloatStringToInt float类型的字符串转为整数 point 小数位数

func GBK2UTF8

func GBK2UTF8(str []byte) ([]byte, error)

GBK2UTF8 gbk编码 转 utf8编码

func GetFreePort

func GetFreePort() (string, error)

GetFreePort 获取一个可用的端口号

func GetPost

func GetPost(method string, sUrl string, data map[string]string, head map[string]string, cookie []*http.Cookie) (string, error)

GetPost 发送get 或 post请求 获取数据

func LocalIP

func LocalIP() bool

LocalIP 判断是否为本地局域网/内网ip

func LocalIPV4

func LocalIPV4() string

LocalIPV4 获取本地IPv4地址

func Md5

func Md5(in []byte) string

func Md5File

func Md5File(filePath string) (string, error)

Md5File 计算文件的md5值

func Mod

func Mod(id int64) int64

Mod 给数据库分表等计算

func ParseSimpleJson

func ParseSimpleJson(jsonStr string) (map[string]string, error)

ParseSimpleJson 解析简单json数据为map[string]string结构 参考格式:{"a":"b","c":123,"d":[1,2,3],"e":["h","i","j"]}

func RandRangeInt

func RandRangeInt(start, end int64) int64

RandRangeInt 生成随机数[n - m)

func SetTimeZone

func SetTimeZone(zoneStr string) error

SetTimeZone 全局修改该包的时区

func Sha256

func Sha256(in []byte) string

func SignPost

func SignPost(domain string, key string, secret string, control string, method string, data map[string]string) (string, error)

SignPost 内部使用 请求参数加密验证 发送post请求到接口

func SingleFlight

func SingleFlight(ctx context.Context, key string, method func(context.Context) (any, error)) (any, error)

SingleFlight 防止缓存击穿

适用场景: key的生成需要谨慎

1.单机模式下的生成缓存;如果分布式模式下,请使用其他方式

2.将多个请求合并成一个请求,比如post短时多次提交;get同一内容

eg: ret, err := cFunc.SingleFlight(context.Background(), key, jisuan)

eg: fmt.Println(ret.(int64), err)

eg: func jisuan(ctx context.Context) (any, error) {

eg: return time.Now().Unix(), nil

eg: }

func StampToTimeStamp

func StampToTimeStamp(stamp int64, phpFormat string) int64

StampToTimeStamp 将时间戳 按指定格式 转换为新的时间戳 stamp 时间戳 如果为0则处理为当前时间 仅支持最常用的 Y-m-d H:i:s 仅支持最常用的 Y-m-d

func StrToTime

func StrToTime(phpFormat string, timeStr string) (int64, error)

StrToTime 将时间字符串 转换为 时间戳 将时间戳 转换为 指定时间格式 对应的 时间戳 仅支持最常用的 Y-m-d H:i:s 和 Y-m-d stamp 时间戳 如果为0则处理为当前时间

func Time

func Time() int64

Time 返回时间戳

func UTF82GBK

func UTF82GBK(str []byte) ([]byte, error)

UTF82GBK utf8编码 转 gbk编码

func WriteFile

func WriteFile(fileName string, content []byte) error

WriteFile 追加写入文件内容 如果fileName为绝对路径则直接使用 如果为相对路径则获取当前程序路径

Types

This section is empty.

Jump to

Keyboard shortcuts

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