utility

package
v0.0.0-...-59da782 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2020 License: GPL-3.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EQ = 0
	NE = 1
	GT = 2
	GE = 3
	LT = 4
	LE = 5
)

---------------版本比较-------------//

View Source
const RoomRandomId = "0123456789"

Variables

View Source
var (
	// ErrAlreadyStarted is returned when somebody tries to start an already
	// running service.
	ErrAlreadyStarted = errors.New("already started")
	// ErrAlreadyStopped is returned when somebody tries to stop an already
	// stopped service (without resetting it).
	ErrAlreadyStopped = errors.New("already stopped")
	// ErrNotStarted is returned when somebody tries to stop a not running
	// service.
	ErrNotStarted = errors.New("not started")
)

Functions

func BoolToInt

func BoolToInt(b bool) int

func CheckPhoneNumber

func CheckPhoneNumber(phone string) bool

func CheckPubkey

func CheckPubkey(key string) bool

---------------检查公钥 ed25519-------------//

func CheckToken

func CheckToken(startTime int64) (int64, bool)

判断token是否过期,按30天过期

func CheckYearly

func CheckYearly(date string) bool

func CompareTimeInterval

func CompareTimeInterval(first, second int64, day int) bool

true: first > second + day

func CreateSalt

func CreateSalt() string

func DayStartAndEndNowMillionSecond

func DayStartAndEndNowMillionSecond(t time.Time) (int64, int64)

时间戳 毫秒

func EncodeSha256

func EncodeSha256(src string) string

func EncodeSha256WithSalt

func EncodeSha256WithSalt(src string, salt string) string

----------加盐加密----------------//

func GetLocalIp

func GetLocalIp() (string, error)

func GetToken

func GetToken() string

随机生成Token (新用户注册)

func GetWeekStartAndEnd

func GetWeekStartAndEnd(tm time.Time) (int64, int64)

func MillionSecondAddDate

func MillionSecondAddDate(tm int64, years, months, days int) int64

func MillionSecondAddDuration

func MillionSecondAddDuration(tm int64, duration time.Duration) int64

func MillionSecondToDateTime

func MillionSecondToDateTime(ts int64) time.Time

2006-01-02 15:04

func MillionSecondToTimeString

func MillionSecondToTimeString(tm int64) string

func MillionSecondToTimeString2

func MillionSecondToTimeString2(tm int64) string

func NowMillionSecond

func NowMillionSecond() int64

func NowSecond

func NowSecond() int64

func PadLeft

func PadLeft(src string, num int, s byte) string

func ParseString

func ParseString(format string, args ...interface{}) string

func RFC3339ToTimeStampMillionSecond

func RFC3339ToTimeStampMillionSecond(rfc string) int64

func RandInt

func RandInt(min, max int) int

func RandomID

func RandomID() string

*

随机生成ID

*

func RandomRoomId

func RandomRoomId() string

func RandomUsername

func RandomUsername() string

func StringToJobj

func StringToJobj(val interface{}) map[string]interface{}

func StructToString

func StructToString(val interface{}) string

func ToBool

func ToBool(val interface{}) bool

func ToFloat32

func ToFloat32(v interface{}) float32

func ToFloat64

func ToFloat64(val interface{}) float64

func ToInt

func ToInt(val interface{}) int

func ToInt32

func ToInt32(o interface{}) int32

func ToInt64

func ToInt64(val interface{}) int64

func ToString

func ToString(val interface{}) string

func VisitorNameSplit

func VisitorNameSplit(src string) string

Types

type AutoSorter

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

multiSorter implements the Sort interface, sorting the changes within.

func OrderedBy

func OrderedBy(less lessFunc, desc bool) *AutoSorter

OrderedBy returns a Sorter that sorts using the less functions, in order. Call its Sort method to sort the data.

func (*AutoSorter) Len

func (ms *AutoSorter) Len() int

Len is part of sort.Interface.

func (*AutoSorter) Less

func (ms *AutoSorter) Less(i, j int) bool

Less is part of sort.Interface. It is implemented by looping along the less functions until it finds a comparison that discriminates between the two items (one is less than the other). Note that it can call the less functions twice per call. We could change the functions to return -1, 0, 1 and reduce the number of calls for greater efficiency: an exercise for the reader.

func (*AutoSorter) Sort

func (ms *AutoSorter) Sort(changes []interface{})

Sort sorts the argument slice according to the less functions passed to OrderedBy.

func (*AutoSorter) Swap

func (ms *AutoSorter) Swap(i, j int)

Swap is part of sort.Interface.

type BaseService

type BaseService struct {
	Logger log15.Logger
	// contains filtered or unexported fields
}

Classical-inheritance-style service declarations. Services can be started, then stopped, then optionally restarted.

Users can override the OnStart/OnStop methods. In the absence of errors, these methods are guaranteed to be called at most once. If OnStart returns an error, service won't be marked as started, so the user can call Start again.

A call to Reset will panic, unless OnReset is overwritten, allowing OnStart/OnStop to be called again.

The caller must ensure that Start and Stop are not called concurrently.

It is ok to call Stop without calling Start first.

Typical usage:

type FooService struct {
	BaseService
	// private fields
}

func NewFooService() *FooService {
	fs := &FooService{
		// init
	}
	fs.BaseService = *NewBaseService(log, "FooService", fs)
	return fs
}

func (fs *FooService) OnStart() error {
	fs.BaseService.OnStart() // Always call the overridden method.
	// initialize private fields
	// start subroutines, etc.
}

func (fs *FooService) OnStop() error {
	fs.BaseService.OnStop() // Always call the overridden method.
	// close/destroy private fields
	// stop subroutines, etc.
}

func NewBaseService

func NewBaseService(logger log15.Logger, name string, impl Service) *BaseService

NewBaseService creates a new BaseService.

func (*BaseService) IsRunning

func (bs *BaseService) IsRunning() bool

IsRunning implements Service by returning true or false depending on the service's state.

func (*BaseService) OnReset

func (bs *BaseService) OnReset() error

OnReset implements Service by panicking.

func (*BaseService) OnStart

func (bs *BaseService) OnStart() error

OnStart implements Service by doing nothing. NOTE: Do not put anything in here, that way users don't need to call BaseService.OnStart()

func (*BaseService) OnStop

func (bs *BaseService) OnStop()

OnStop implements Service by doing nothing. NOTE: Do not put anything in here, that way users don't need to call BaseService.OnStop()

func (*BaseService) Quit

func (bs *BaseService) Quit() <-chan struct{}

Quit Implements Service by returning a quit channel.

func (*BaseService) Reset

func (bs *BaseService) Reset() error

Reset implements Service by calling OnReset callback (if defined). An error will be returned if the service is running.

func (*BaseService) SetLogger

func (bs *BaseService) SetLogger(l log15.Logger)

SetLogger implements Service by setting a logger.

func (*BaseService) Start

func (bs *BaseService) Start() error

Start implements Service by calling OnStart (if defined). An error will be returned if the service is already running or stopped. Not to start the stopped service, you need to call Reset.

func (*BaseService) Stop

func (bs *BaseService) Stop() error

Stop implements Service by calling OnStop (if defined) and closing quit channel. An error will be returned if the service is already stopped.

func (*BaseService) String

func (bs *BaseService) String() string

String implements Servce by returning a string representation of the service.

func (*BaseService) Wait

func (bs *BaseService) Wait()

Wait blocks until the service is stopped.

type Service

type Service interface {
	// Start the service.
	// If it's already started or stopped, will return an error.
	// If OnStart() returns an error, it's returned by Start()
	Start() error
	OnStart() error

	// Stop the service.
	// If it's already stopped, will return an error.
	// OnStop must never error.
	Stop() error
	OnStop()

	// Reset the service.
	// Panics by default - must be overwritten to enable reset.
	Reset() error
	OnReset() error

	// Return true if the service is running
	IsRunning() bool

	// Quit returns a channel, which is closed once service is stopped.
	Quit() <-chan struct{}

	// String representation of the service
	String() string

	// SetLogger sets a logger.
	SetLogger(log15.Logger)
}

Service defines a service that can be started, stopped, and reset.

type Version

type Version string

func (Version) Compare

func (v Version) Compare(t Version, tp int) bool

Jump to

Keyboard shortcuts

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