misc

package module
v0.0.0-...-9c8c08c Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2023 License: GPL-3.0 Imports: 20 Imported by: 0

Documentation

Overview

#### comments with mark

  • "//--" debug code
  • "//!!" warning
  • "//??" important

#### test and benchmark functions

  • test instances: TestFunc_t1, TestFunc_t2, TestFunc_t3
  • error test: TestFunc_err1, TestFunc_err2, TestFunc_err3
  • benchmark instances: BenchmarkFunc_b1, BenchmarkFunc_b2, BenchmarkFunc_b3

Index

Constants

View Source
const (
	RFC3339ms = "2006-01-02T15:04:05.000Z07:00"
)

Variables

View Source
var (
	Rand *rand.Rand
)

/

Functions

func AddHttpsScheme

func AddHttpsScheme(a string) string

/

func Base64Decode

func Base64Decode(src string) ([]byte, error)

replace +/ with -_

func Base64Encode

func Base64Encode(src []byte) string

replace +/ with -_

func BuildInfo

func BuildInfo(vars ...[2]string) (info [][2]string)

func DelayFunc

func DelayFunc(delay int64) http.HandlerFunc

func EqualVector

func EqualVector[T constraints.Ordered](arr1, arr2 []T) (ok bool)

func FileSaveName

func FileSaveName(p string) (out string, err error)

func FileSize2Str

func FileSize2Str(n int64) string

func GetOClock

func GetOClock(shift int) (clock time.Time)

func GetPanic

func GetPanic(n int)

func HttpErrorExtract

func HttpErrorExtract(err error) error

func HttpErrorResetCode

func HttpErrorResetCode(err error, code uint, codes ...int) (ok bool)

func ListenOSIntr

func ListenOSIntr(do func(), errch chan<- error, sgs ...os.Signal)

/

func NewRand

func NewRand() (rd *rand.Rand)

func ParseDate

func ParseDate(value string) (at time.Time, err error)

date format: "2021-06-24"

func ParseDatetime

func ParseDatetime(value string) (at time.Time, err error)

datetime format: "2021-06-24 09:10:11"

func PrintJSON

func PrintJSON(d interface{}) (err error)

func ProjectDir

func ProjectDir() (dir string, err error)

get root path of project by recursively match go.mod in parent directory

func ProjectFile

func ProjectFile(p2f ...string) (fp string, err error)

func QuoteString

func QuoteString(str string) string

/

func ResBadRequest

func ResBadRequest(writer http.ResponseWriter, code int, err error)

func ResJSON

func ResJSON(writer http.ResponseWriter, data interface{}, errs ...error)

?? data is ResData

func ResOk

func ResOk(writer http.ResponseWriter)

func SetLogRFC3339

func SetLogRFC3339()

/

func SimplifyDebugStack

func SimplifyDebugStack(bts []byte, n int) string

func StrsIndex

func StrsIndex(ss []string, s string) (i int)

/

func TimeCeil

func TimeCeil(at time.Time, tu string) (out time.Time, err error)

ceil time, e.g. TimeCeil('2020-12-01T17:39:07.123+08:00', "M") -> '2020-12-01T17:40:00+08:00'

valid unit(key or value)
H: hour, M: minute, S: second
y: year, s: season, m: month, w: week, d: day

func TimeFloor

func TimeFloor(at time.Time, tu string) (out time.Time, err error)

floor time, e.g. TimeFloor('2020-12-01T17:39:07.123+08:00', "M") -> '2020-12-01T17:39:00+08:00'

valid unit(key or value)
H: hour, M: minute, S: second
y: year, s: season, m: month, w: week, d: day

func UniqVector

func UniqVector[T constraints.Ordered](arr []T) (list []T)

func VectorIndex

func VectorIndex[T constraints.Ordered](list []T, v T) int

func WrapError

func WrapError(err error, skips ...int) error

Types

type ErrContainer

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

func NewErrContainer

func NewErrContainer(value interface{}, errmsg string) (ec *ErrContainer)

func (*ErrContainer) Error

func (ec *ErrContainer) Error() string

func (*ErrContainer) Level

func (ec *ErrContainer) Level() (level int)

func (*ErrContainer) SetLevel

func (ec *ErrContainer) SetLevel(level int) *ErrContainer

func (*ErrContainer) Value

func (ec *ErrContainer) Value() (value interface{})

type Error

type Error struct {
	Caller   string `json:"caller,omitempty"`
	Filepath string `json:"filepath,omitempty"`
	Line     int    `json:"line,omitempty"`
	Err      error  `json:"err,omitempty"`
}

func (Error) Error

func (err Error) Error() string

type Executor

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

func NewExecutor

func NewExecutor() *Executor

func (*Executor) Load

func (ex *Executor) Load(run func() error, onExit func(error))

func (*Executor) Wait

func (ex *Executor) Wait(dura time.Duration, sgs ...os.Signal) (ok bool)

type HttpError

type HttpError struct {
	Raw      error  `json:"raw"`      // program error for debug
	Message  string `json:"message"`  // message for frontend
	HttpCode int    `json:"httpCode"` // http response status code
	Code     int    `json:"code"`     // bussiness logical code
}

models return a error which will treated as a response to gin.Context

func NewHttpError

func NewHttpError(raw error, message string, httpCode, code int) (err *HttpError)

func (*HttpError) Error

func (err *HttpError) Error() string

func (*HttpError) ToResData

func (err *HttpError) ToResData(codes ...int) (rd *ResData)

type NetworkTimeResult

type NetworkTimeResult struct {
	T1    time.Time `json:"t1"`
	T2    time.Time `json:"t2"`
	T3    time.Time `json:"t3"`
	T4    time.Time `json:"t4"`
	Sigma int64     `json:"sigma"`
	Delta int64     `json:"delta"`
}

func GetNetworkTime

func GetNetworkTime(addr string, delay int64) (result *NetworkTimeResult, err error)

func (NetworkTimeResult) String

func (result NetworkTimeResult) String() string

type NetworkTimeServer

type NetworkTimeServer struct {
	*http.Server
	// contains filtered or unexported fields
}

https://en.wikipedia.org/wiki/Network_Time_Protocol https://en.wikipedia.org/wiki/File:NTP-Algorithm.svg

func NewNetworkTimeServer

func NewNetworkTimeServer(addr string, delay int64) (ser *NetworkTimeServer, err error)

func (*NetworkTimeServer) Run

func (ser *NetworkTimeServer) Run() error

type PX

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

func NewPx

func NewPx() (px *PX)

func (*PX) Run

func (px *PX) Run(fn func() error)

func (*PX) Wait

func (px *PX) Wait() (m int)

type Pprof

type Pprof struct {
	Server *http.Server
	// contains filtered or unexported fields
}

web browser address

http://localhost:5060/debug/pprof/

get profiles and view in browser

$ go tool pprof -http=:8080 http://localhost:5060/debug/pprof/allocs?seconds=30
$ go tool pprof http://localhost:5060/debug/pprof/block?seconds=30
$ go tool pprof http://localhost:5060/debug/pprof/goroutine?seconds=30
$ go tool pprof http://localhost:5060/debug/pprof/heap?seconds=30
$ go tool pprof http://localhost:5060/debug/pprof/mutex?seconds=30
$ go tool pprof http://localhost:5060/debug/pprof/profile?seconds=30
$ go tool pprof http://localhost:5060/debug/pprof/threadcreate?seconds=30

download profile file and convert to svg image

$ wget -O profile.out localhost:5060/debug/pprof/profile?seconds=30
$ go tool pprof -svg profile.out > profile.svg

get pprof in 30 seconds and save to svg image

$ go tool pprof -svg http://localhost:5060/debug/pprof/allocs?seconds=30 > allocs.svg

get trace in 5 seconds

$ wget -O trace.out http://localhost:5060/debug/pprof/trace?seconds=5
$ go tool trace trace.out

get cmdline and symbol binary data

$ wget -O cmdline.out http://localhost:5060/debug/pprof/cmdline
$ wget -O symbol.out http://localhost:5060/debug/pprof/symbol

func NewPprof

func NewPprof(addr string) (pp *Pprof)

create new Pprof and run server

func (*Pprof) Err

func (pp *Pprof) Err() (err error)

get err field

func (*Pprof) Run

func (pp *Pprof) Run() (err error)

func (*Pprof) Shutdown

func (pp *Pprof) Shutdown() (err error)

showdown server

func (*Pprof) String

func (pp *Pprof) String() string

print add, status and err in json format

type ResData

type ResData struct {
	Code    int                    `json:"code"`
	Message string                 `json:"message"`
	Data    map[string]interface{} `json:"data"`

	RequestId string `json:"requestId,omitempty"` // unique request id for log
	Err       error  `json:"-"`                   // error for debug
}

func NewResData

func NewResData(code int, message string) (rd *ResData)

factory method

func (*ResData) Bytes

func (rd *ResData) Bytes() []byte

/

func (*ResData) Error

func (rd *ResData) Error() string

func (*ResData) OK

func (rd *ResData) OK() bool

func (*ResData) Pretty

func (rd *ResData) Pretty() string

func (*ResData) Reset

func (rd *ResData) Reset(code int, message string)

func (*ResData) Set

func (rd *ResData) Set(key string, value interface{})

func (ResData) String

func (rd ResData) String() string

type Result

type Result[T any] struct {
	Data *T
	Err  error
}

func Err

func Err[T any](err error) Result[T]

func Ok

func Ok[T any](d *T) Result[T]

func (*Result[T]) Ok

func (result *Result[T]) Ok() bool

type Vector

type Vector[T any] []T

func (Vector[T]) First

func (v Vector[T]) First() *T

func (Vector[T]) Last

func (v Vector[T]) Last() *T

Jump to

Keyboard shortcuts

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