GoKit

package module
v0.0.0-...-88827a4 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2017 License: MIT Imports: 11 Imported by: 9

README

GoKit

Build Status codecov

我的go语言工具箱,把日常开发中经常用到的函数,统一放在这里。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DateOf

func DateOf(t int64) string

DateOf 返回会把 timestamp 普通的日期格式 例如:把 1136185384 转换成 "2006-01-02 15:03:04 +0800 CST"

func Exist

func Exist(filename string) bool

Exist 判断文件是否存在

func Init

func Init(pidFile string)

Init 初始化的方法汇总,提供了以下功能: 1.利用CPU全部内核 2.保存当前程序pid号到pidFile文件中。关闭程序, 可在命令行使用 $ kill `cat $pidFile`

func IsSameType

func IsSameType(a, b interface{}) bool

IsSameType 判断两个interface{}类型的变量的原始类型是否相等

func Message

func Message(msgAndArgs ...interface{}) string

Message 把msgAndArgs 格式化成消息 NOTICE: 使用方法 Message(msgAndArgs...)

func Mkdir

func Mkdir(name string) error

Mkdir 简化了os.Mkdir的操作 会按照name,新建一个权限为0755的目录

func PWD

func PWD() string

PWD like shell cmd pwd PWD 返回当前工作目录路径 只是忽略了可能发生的错误

func ParseLocalTime

func ParseLocalTime(strTime string) (*time.Time, error)

ParseLocalTime 把string格式的时间,转换成time.time NOTICE: 我写这个函数的原因是 1. 添加更详解的错误说明。 2. 编写单元测试,查看是否真的转换成功了。

func SleepFunc

func SleepFunc(duration time.Duration) func()

SleepFunc 返回一个等待sleep函数, 使程序暂停一个duration。 SleepFunc是以上WaitFunc的简化版本,通常运用于API访问限制 利用闭包,把beginTime变量包裹到了sleep函数内。

func WaitFunc

func WaitFunc(checkCycle time.Duration, name string) (chan<- time.Duration, func())

WaitFunc 返回 一个 cycleCh chan<- time.Duration 和 一个 Wait 函数 如果 cycleCh <- d ,则 Wait() 的sleep时间被修改为 d sleep时间的修改会立即生效,不用等到此waitDuration结束。 例如:当100秒的waitDuration时间已过51秒时,把waitDuration修改为50秒,程序会立刻结束。

不用等到100秒才结束。

例如:100秒的waitDuration在结束前,把waitDuration修改为200秒,

此次Wait()会sleep 200秒才结束。

checkCycle是检查是否到期的时间段,也是最小等待时间段。

当checkCycle较大时,Wait()的sleep时间会和waitDuration,有较大的差距, 程序的实际等待时间,总是 checkCycle × int(waitDuration/checkCycle + 1)

func WaitingKill

func WaitingKill() chan struct{}

WaitingKill 会返回一个channel,当程序接收到kill信号时,会关闭此通道。

Types

type Error

type Error struct {
	Info string
	Prev error
}

Error 是自定义的错误类型

func Err

func Err(err error, msgAndArgs ...interface{}) *Error

Err 对Error添加新的信息,以便于追踪错误。

func (*Error) Error

func (e *Error) Error() string

Error() 帮助 Error struct 实现 error 接口

Jump to

Keyboard shortcuts

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