ioKit

package
v2.9.108 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// NewReadWriter bytes.Buffer 结构体 实现了 io.ReadWriter 接口
	/*
		@param s 可以为nil
	*/
	NewReadWriter func(s []byte) *bytes.Buffer = bytes.NewBuffer

	// NewReadWriterFromString bytes.Buffer 结构体 实现了 io.ReadWriter 接口
	/*
		@param str 可以为""
	*/
	NewReadWriterFromString func(str string) *bytes.Buffer = bytes.NewBufferString
)
View Source
var NopCloser func(reader io.Reader) io.ReadCloser = io.NopCloser

NopCloser

PS: 返回值调用Close()将什么都不会做,直接返回nil.

Functions

func Close added in v2.1.7

func Close(closers ...io.Closer) error

Close

PS: 就算循环过程中返回了非nil的error,也要继续向下循环(尽可能多地关闭).

@param closers (1) 可以为nil(即不传参);(2) 其中可以有nil @return 发生error的话,返回第一个

func CloseSingle added in v2.1.7

func CloseSingle(closer io.Closer) error

func CountLines

func CountLines(r io.Reader) (count int, err error)

CountLines 读取内容的行数

PS: (1) 如果最后一行只有'\n',会被视作1行; (2) 不要重复调用,否则第二次开始行数将不准确.

func CountLines1

func CountLines1(r io.Reader) (int, error)

CountLines1 读取内容的行数

PS: (1) 如果最后一行只有'\n',不会被视作1行; (2) 不要重复调用,否则第二次开始行数将不准确.

func MultiReader

func MultiReader(readers ...io.Reader) io.Reader

func MultiWriteCloser

func MultiWriteCloser(writeClosers ...io.WriteCloser) io.WriteCloser

MultiWriteCloser

PS: 参考了 io.MultiWriter().

func MultiWriter

func MultiWriter(writers ...io.Writer) io.Writer

func NewDailyWriteCloser added in v2.1.9

func NewDailyWriteCloser(filePath string, options ...LumberJackOption) (io.WriteCloser, error)

NewDailyWriteCloser 每天凌晨0点,执行Rotate().

@param options 可选配置,参考 NewLumberJackWriteCloser()

func NewFileReader

func NewFileReader(filePath string) (*os.File, error)

NewFileReader

!!!:要在外部手动调用 *os.File 的Close方法.

@return os.File 结构体 实现了 io.Reader 接口

func NewLumberJackWriteCloser added in v2.1.32

func NewLumberJackWriteCloser(filePath string, maxSize int64, options ...LumberJackOption) (*lumberjack.Roller, error)

NewLumberJackWriteCloser 可rotate(依据传参maxSize)的io.WriteCloser.

PS: (1) 如果 当前目标文件 被人为删了,会丢失部分输出直至再次调用Rotate(). (2) 可以同时配置 MaxBackups 和 MaxAge.

@param maxSize unit: byte @param options 可选配置: WithCompress() [默认: 不压缩] 是否压缩被rotate的文件? WithMaxBackups() [默认: 保留所有旧日志] 旧日志保存的最多数量 WithMaxAge() [默认: 保留所有旧日志] 旧日志保存的最长时间 WithLocalTime() [默认: true] true: 使用本地时间; false: 使用UTC时间

func NewReader

func NewReader(s []byte) io.Reader

func NewReaderFromString

func NewReaderFromString(str string) io.Reader

func NewRotatableWriteCloserWithSpec added in v2.1.9

func NewRotatableWriteCloserWithSpec(filePath string, spec string, options ...LumberJackOption) (io.WriteCloser, error)

NewRotatableWriteCloserWithSpec 满足条件(spec),执行Rotate().

PS: (1) 可能存在情况,Rotate()后,生成的旧日志文件大小为0B.

func NopCloserToReadSeeker

func NopCloserToReadSeeker(readSeeker io.ReadSeeker) io.ReadSeekCloser

NopCloserToReadSeeker 给 io.ReadSeeker 实例加上Close()(虽然这个Close()什么都不做).

func ReadFromReader

func ReadFromReader(reader io.Reader) ([]byte, error)

ReadFromReader 读取io.Reader的内容(io.Reader => []byte)

func ReadStringFromReader added in v2.2.19

func ReadStringFromReader(reader io.Reader) (string, error)

ReadStringFromReader 读取io.Reader的内容(io.Reader => string)

func SeekToStart added in v2.2.20

func SeekToStart(seeker io.Seeker) (int64, error)

SeekToStart 回到最前面.

func ToBufioReader

func ToBufioReader(reader io.Reader) *bufio.Reader

ToBufioReader io.Reader 接口 => *bufio.Reader

作用: 可以调用 bufio.Reader 结构体的方法(因为 io.Reader 接口就一个Read方法).

func TryToClose

func TryToClose(objs ...any) error

TryToClose 尝试关闭(如果实现了io.Closer接口的话).

@param writers (1) 可以为nil(即不传参)

(2) 其中可以有nil

@return 发生error的话,返回第一个

Types

type DailyWriteCloser added in v2.1.9

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

func (*DailyWriteCloser) Close added in v2.1.9

func (dwc *DailyWriteCloser) Close() error

func (*DailyWriteCloser) Write added in v2.1.9

func (dwc *DailyWriteCloser) Write(p []byte) (int, error)

type LumberJackOption added in v2.1.32

type LumberJackOption func(opts *lumberjackOptions)

func WithCompress

func WithCompress(compress bool) LumberJackOption

func WithLocalTime

func WithLocalTime(localTime bool) LumberJackOption

func WithMaxAge

func WithMaxAge(maxAge time.Duration) LumberJackOption

func WithMaxBackups

func WithMaxBackups(maxBackups int) LumberJackOption

Jump to

Keyboard shortcuts

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