Documentation ¶
Index ¶
- Variables
- func BoolToStr(b bool) string
- func ByteToStr(b []byte) string
- func ChangeStringWithIndex(str *string, index int, s byte) string
- func ChangeStringWithIndexCN(str *string, index int, s rune) string
- func ClearMap[K comparable, V any](a *map[K]V)
- func CopyFile(srcPath, destPath string, perm os.FileMode) error
- func CopyFileForLargeFile(srcPath, destPath string, perm os.FileMode) error
- func ErrorExit(err *error)
- func ErrorMethod()
- func FileExist(filePath string) (bool, error)
- func Float32ToStr(f float32, decimalLength ...int) string
- func Float64ToStr(f float64, decimalLength ...int) string
- func ForCN(str string, f func(string))
- func GetFileContent(filePath string) ([]byte, error)
- func GetFileReader(filePath string, size int, fun func(reader *bufio.Reader) error) error
- func GetFileWriter(filePath string, flag int, perm os.FileMode, size int, ...) error
- func GetTagValue(i interface{}, filedName string, tagName string) (string, error)
- func GoSleepSeconds(n int64, fun func())
- func GoUpdateWidthSeconds(n int64, fun func())
- func IntToBase[T int | int8 | int16 | int32 | int64](i T, base int) string
- func IntToStr(f int, base ...int) string
- func Log(a ...any)
- func LogAddress(str string, p any)
- func LogError(err *error)
- func LogTimeUntilDay(t ...time.Time)
- func LogTimeUntilSecond(t ...time.Time)
- func LogWithBrackets(a ...any)
- func MapHasKey[T comparable](m map[T]any, key T) bool
- func NewStruct[T any]() *T
- func NowNanoseconds() int64
- func NowSeconds() int64
- func OpenFile(fileName string, fun func(*os.File) error) error
- func RandFloat32() float32
- func RandFloat64() float64
- func RandInt() int
- func RandInt31N(n int32) int32
- func RandInt32() int32
- func RandInt63N(n int64) int64
- func RandInt64() int64
- func RandIntN(n int) int
- func RandUInt32() uint32
- func RandUint64() uint64
- func ReadFileBytes(filePath *string, fun func([]byte) error) error
- func ReadFileForString(fileName string, fun func(str string), bufferSize ...int) error
- func ReadWriteFile(fileName string, flag int, perm os.FileMode, ...) error
- func ReaderStringBorder(reader *bufio.Reader, fun func(str string)) error
- func RunMethodByName(i interface{}, methodName string, parameters ...any) ([]reflect.Value, error)
- func SetFiledValue[T comparable](i interface{}, filedName string, value T) error
- func SleepHour(i int64)
- func SleepMicrosecond(i int64)
- func SleepMillisecond(i int64)
- func SleepMillisecondWithRandom(i int64, r int64)
- func SleepMinute(i int64)
- func SleepNanosecond(i int64)
- func SleepSecond(i int64)
- func SleepSecondWithRandom(i int64, r int64)
- func StrToBool(str string) bool
- func StrToByte(str string) []byte
- func StrToFloat32(str string) float32
- func StrToFloat64(str string) float64
- func StrToInt(str string, base ...int) int
- func StrToInt16(str string, base ...int) int16
- func StrToInt32(str string, base ...int) int32
- func StrToInt64(str string, base ...int) int64
- func StrToInt8(str string, base ...int) int8
- func TimeDay(t ...time.Time) string
- func TimeSecond(t ...time.Time) string
- func TimeSecondPath() string
- func Trim(str *string) string
- func TryCatch(try func(), errFun func(err any)) bool
- func WriteFile(fileName string, flag int, perm os.FileMode, ...) error
- func WriteFileBytes(filePath *string, data []byte, perm os.FileMode) error
- type ChannelLock
- type MyChannel
- func (ch *MyChannel[T]) NewConsumer(fun func(value T))
- func (ch *MyChannel[T]) NewConsumerWithName(name string, fun func(name string, value T))
- func (ch *MyChannel[T]) NewProvider(fun func())
- func (ch *MyChannel[T]) NewProviderWithName(name string, fun func(name string))
- func (ch *MyChannel[T]) WaitConsumerFinish()
- func (ch *MyChannel[T]) WaitProviderFinish()
Constants ¶
This section is empty.
Variables ¶
var ( // AppPath app的绝对路径,末尾带有/ AppPath = "" File_R = os.O_RDONLY // 只读 File_W = os.O_WRONLY // 只写 File_RW = os.O_RDWR // 可读可写 File_WC = os.O_WRONLY | os.O_CREATE // 可写 没有就创建 File_RWC = os.O_RDWR | os.O_CREATE // 可读可写 没有就创建 File_RWCA = os.O_RDWR | os.O_CREATE | os.O_APPEND // 可读可写 没有就创建 附加到尾部 File_RWCT = os.O_RDWR | os.O_CREATE | os.O_APPEND | os.O_TRUNC // 可读可写 没有就创建 清空文件 )
Functions ¶
func ChangeStringWithIndex ¶
ChangeStringWithIndex 修改string的index对应的字符 只适用英文
func ChangeStringWithIndexCN ¶
ChangeStringWithIndexCN 修改string的index对应的字符 可以处理中文 str := "你好" s := common.ChangeStringWithIndex(&str, 0, '他') fmt.Println(s) // 他好
func ClearMap ¶
func ClearMap[K comparable, V any](a *map[K]V)
ClearMap 清空map
a := make(map[string]string) a["a"] = "A" ClearMap(&a) fmt.Println(a) // map[]
func CopyFile ¶ added in v1.0.2
CopyFile 复制文件,如果文件不存在会创建,适合小文件
err := CopyFile(common.AppPath+"a.txt", common.AppPath+"b.txt", 0755)
func CopyFileForLargeFile ¶ added in v1.0.2
CopyFileForLargeFile 复制大文件
err := CopyFileForLargeFile(common.AppPath+"a.txt", common.AppPath+"c.txt",0755)
func ErrorExit ¶
func ErrorExit(err *error)
ErrorExit 如果有异常就退出程序
func test() (err error) { return errors.New("自定义错误") } err := test() common.ErrorExit(&err)
func ErrorMethod ¶
func ErrorMethod()
ErrorMethod 如果有异常就退出函数
func test() { defer common.ErrorMethod() i := 0 fmt.Println("test()结束,a:", 3/i) }
test()
func FileExist ¶ added in v1.0.2
FileExist 判断文件是否存在
exist, err := FileExist(common.AppPath + "aa.txt") if err != nil { fmt.Println("发生错误") } else { fmt.Println(exist) }
func Float32ToStr ¶
Float32ToStr decimalLength:表示小数部分长度
func Float64ToStr ¶
func GetFileContent ¶ added in v1.0.2
GetFileContent 获取文件内容
content, err := GetFileContent(common.AppPath + "a.txt")
func GetFileReader ¶ added in v1.0.2
获取reader
func GetFileWriter ¶ added in v1.0.2
func GetFileWriter(filePath string, flag int, perm os.FileMode, size int, fun func(writer *bufio.Writer) error) error
获取writer
func GetTagValue ¶ added in v1.0.2
GetTagValue 获取属性的tag值
type User struct { Name string `json:"name"` Age int `json:"age"` } func main() { user := User{"张三", 20} value, err := GetTagValue(&user, "Name", "json") if err != nil { fmt.Println("发生错误:", err) } else { fmt.Println("取到值:", value) // name } }
func GoSleepSeconds ¶ added in v1.0.3
func GoSleepSeconds(n int64, fun func())
func GoUpdateWidthSeconds ¶ added in v1.0.3
func GoUpdateWidthSeconds(n int64, fun func())
GoUpdateWidthSeconds 立即执行一次,后面的每隔n秒执行一次
func MapHasKey ¶ added in v1.0.2
func MapHasKey[T comparable](m map[T]any, key T) bool
判断map是否有key
m := make(map[string]any) m["a"] = "a" m["b"] = nil fmt.Println(common.MapHasKey(m, "a")) // true fmt.Println(common.MapHasKey(m, "b")) // true fmt.Println(common.MapHasKey(m, "c")) // false
func NewStruct ¶ added in v1.0.2
func NewStruct[T any]() *T
创建结构体
type User struct { Name string Age int } func (user User) Eat(food string) string { return user.Name + "吃" + food } func main() { user := common.NewStruct[User]() user.Name = "张三" fmt.Println(user.Eat("西瓜")) // 张三吃西瓜 }
func NowNanoseconds ¶
func NowNanoseconds() int64
func NowSeconds ¶
func NowSeconds() int64
func RandFloat32 ¶
func RandFloat32() float32
func RandFloat64 ¶
func RandFloat64() float64
func RandInt31N ¶
func RandInt63N ¶
func RandUInt32 ¶
func RandUInt32() uint32
func RandUint64 ¶
func RandUint64() uint64
func ReadFileBytes ¶ added in v1.0.2
user := User{}
err := common.ReadFileBytes(&filePath, func(bytes []byte) error { return json.Unmarshal(bytes, &user) })
func ReadFileForString ¶
ReadFileForString 将文件内容的每一行读取出来 适合大文件
err := common.ReadFileForString(common.AppPath+"/a.txt", func(str string) { common.LogWithBrackets(str) }, 16)
func ReadWriteFile ¶ added in v1.0.2
func ReadWriteFile(fileName string, flag int, perm os.FileMode, fun func(reader *bufio.Reader, writer *bufio.Writer) error, bufferSize ...int) error
ReadWriteFile 可读可写
err := common.ReadWriteFile(common.AppPath+"a.txt", common.File_RWC, 0755, func(reader *bufio.Reader, writer *bufio.Writer) error { // 读取 err := common.ReaderStringBorder(reader, func(str string) { common.LogWithBrackets(str) }) if err != nil { return err } // 写入 _, err2 := writer.WriteString("你好\r\n") if err2 != nil { return err2 } return nil })
func ReaderStringBorder ¶ added in v1.0.2
ReaderStringBorder 从reader中读取字符串
err := ReaderStringBorder(reader, func(str string) { common.LogWithBrackets(str) })
func RunMethodByName ¶ added in v1.0.2
通过方法名称执行方法
type User struct { Name string Age int } func (user User) Eat(food string) string { return user.Name + "吃" + food } func main() { user := User{"张三", 20} values, err := common.RunMethodByName(&user, "Eat", "西瓜") if err != nil { fmt.Println(err) } else { fmt.Println("返回结果:", values) // [张三吃西瓜] } }
func SetFiledValue ¶ added in v1.0.2
func SetFiledValue[T comparable](i interface{}, filedName string, value T) error
设置值
type User struct { Name string Age int } func main() { user := User{"张三", 20} err := SetFiledValue(&user, "Name", "李四") if err != nil { fmt.Println("发生错误:", err) } fmt.Println(user) // {李四 20} } func main() { user := User{"张三", 20} err := SetFiledValue(&user, "Name", 1) if err != nil { fmt.Println("发生错误:", err) // 发生错误: Name类型不同,期望类型:string ,实际类型:int } fmt.Println(user) // {张三 20} }
func SleepMicrosecond ¶
func SleepMicrosecond(i int64)
func SleepMillisecond ¶
func SleepMillisecond(i int64)
func SleepMillisecondWithRandom ¶ added in v1.0.3
func SleepMinute ¶
func SleepMinute(i int64)
func SleepNanosecond ¶
func SleepNanosecond(i int64)
func SleepSecond ¶
func SleepSecond(i int64)
func SleepSecondWithRandom ¶ added in v1.0.3
func StrToFloat32 ¶
func StrToFloat64 ¶
func StrToInt16 ¶
func StrToInt32 ¶
func StrToInt64 ¶
func TimeSecond ¶ added in v1.0.2
func TimeSecondPath ¶ added in v1.0.7
func TimeSecondPath() string
func TryCatch ¶ added in v1.0.2
TryCatch 用于捕获异常
type HttpError struct { Code int } func (e *HttpError) Error() string { return fmt.Sprintf("status code %d", e.Code) } type PermissionError struct { Code int } func (e *PermissionError) Error() string { return fmt.Sprintf("status code %d", e.Code) } func main() { common.TryCatch(func() { panic(HttpError{404}) }, func(err any) { fmt.Println("发生异常:", err) switch err.(type) { case HttpError: fmt.Println("是HttpError") case PermissionError: fmt.Println("是PermissionError") } }) }
func WriteFile ¶ added in v1.0.2
func WriteFile(fileName string, flag int, perm os.FileMode, fun func(writer *bufio.Writer) error, bufferSize ...int) error
WriteFile 写入内容 fun中的异常全部往外抛,在WriteFile方法返回的err中处理异常
err := common.WriteFile(common.AppPath+"/a.txt",os.O_WRONLY|os.O_CREATE,0755, func(writer *bufio.Writer) error { for i := 0; i < 10; i++ { n, err := writer.WriteString("你好 " + common.IntToStr(i) + "\r\n") if err != nil { return err } fmt.Println("写入数量:", n) } return nil })
Types ¶
type ChannelLock ¶ added in v1.0.2
type ChannelLock struct { Name string // contains filtered or unexported fields }
ChannelLock 使用案例:
func set(c chan int) { for i := 0; i < 5; i++ { c <- i fmt.Println("set:", i) common.SleepSecond(1) } close(c) } func get(c chan int, lock *common.ChannelLock) { for v := range c { fmt.Println(" get:", v) } fmt.Println("管道已关闭") lock.Close() } func main() { c := make(chan int, 100) lock := common.NewChannelLock() go set(c) go get(c, lock) lock.WaitClose() }
func NewChannelLockName ¶ added in v1.0.2
func NewChannelLockName(n int, name string) *ChannelLock
type MyChannel ¶ added in v1.0.2
type MyChannel[T any] struct { Channel chan T // contains filtered or unexported fields }
func main() { ch := common.NewMyChannel[int](10, 2, 3) // ch.NewProvider(func() { // for i := 0; i < 5; i++ { // ch.channel <- i // time.Sleep(1 * time.Second) // } // }) ch.NewProviderWithName("set", func(name string) { for i := 0; i < 5; i++ { ch.Channel <- i fmt.Println(name, i) time.Sleep(1 * time.Second) } }) // ch.NewConsumer(func(v int) { // time.Sleep(200 * time.Millisecond) // fmt.Println(" ", v) // }) ch.NewConsumerWithName("get", func(name string, v int) { time.Sleep(200 * time.Millisecond) fmt.Println(" ", name, v) }) ch.WaitProviderFinish() ch.WaitConsumerFinish() }
func NewMyChannel ¶ added in v1.0.2
func (*MyChannel[T]) NewConsumer ¶ added in v1.0.2
func (ch *MyChannel[T]) NewConsumer(fun func(value T))
func (*MyChannel[T]) NewConsumerWithName ¶ added in v1.0.2
func (*MyChannel[T]) NewProvider ¶ added in v1.0.2
func (ch *MyChannel[T]) NewProvider(fun func())
func (*MyChannel[T]) NewProviderWithName ¶ added in v1.0.2
func (*MyChannel[T]) WaitConsumerFinish ¶ added in v1.0.2
func (ch *MyChannel[T]) WaitConsumerFinish()
func (*MyChannel[T]) WaitProviderFinish ¶ added in v1.0.2
func (ch *MyChannel[T]) WaitProviderFinish()