mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-03 00:38:40 +08:00
fix golint issues in core/iox (#488)
This commit is contained in:
parent
0c094cb2d7
commit
1f92bfde6a
@ -5,11 +5,13 @@ import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// A BufferPool is a pool to buffer bytes.Buffer objects.
|
||||
type BufferPool struct {
|
||||
capability int
|
||||
pool *sync.Pool
|
||||
}
|
||||
|
||||
// NewBufferPool returns a BufferPool.
|
||||
func NewBufferPool(capability int) *BufferPool {
|
||||
return &BufferPool{
|
||||
capability: capability,
|
||||
@ -21,12 +23,14 @@ func NewBufferPool(capability int) *BufferPool {
|
||||
}
|
||||
}
|
||||
|
||||
// Get returns a bytes.Buffer object from bp.
|
||||
func (bp *BufferPool) Get() *bytes.Buffer {
|
||||
buf := bp.pool.Get().(*bytes.Buffer)
|
||||
buf.Reset()
|
||||
return buf
|
||||
}
|
||||
|
||||
// Put returns buf into bp.
|
||||
func (bp *BufferPool) Put(buf *bytes.Buffer) {
|
||||
if buf.Cap() < bp.capability {
|
||||
bp.pool.Put(buf)
|
||||
|
@ -10,6 +10,7 @@ func (nopCloser) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// NopCloser returns a io.WriteCloser that does nothing on calling Close.
|
||||
func NopCloser(w io.Writer) io.WriteCloser {
|
||||
return nopCloser{w}
|
||||
}
|
||||
|
@ -16,9 +16,11 @@ type (
|
||||
omitPrefix string
|
||||
}
|
||||
|
||||
// TextReadOption defines the method to customize the text reading functions.
|
||||
TextReadOption func(*textReadOptions)
|
||||
)
|
||||
|
||||
// DupReadCloser returns two io.ReadCloser that read from the first will be written to the second.
|
||||
// The first returned reader needs to be read first, because the content
|
||||
// read from it will be written to the underlying buffer of the second reader.
|
||||
func DupReadCloser(reader io.ReadCloser) (io.ReadCloser, io.ReadCloser) {
|
||||
@ -27,6 +29,7 @@ func DupReadCloser(reader io.ReadCloser) (io.ReadCloser, io.ReadCloser) {
|
||||
return ioutil.NopCloser(tee), ioutil.NopCloser(&buf)
|
||||
}
|
||||
|
||||
// KeepSpace customizes the reading functions to keep leading and tailing spaces.
|
||||
func KeepSpace() TextReadOption {
|
||||
return func(o *textReadOptions) {
|
||||
o.keepSpace = true
|
||||
@ -49,6 +52,7 @@ func ReadBytes(reader io.Reader, buf []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadText reads content from the given file with leading and tailing spaces trimmed.
|
||||
func ReadText(filename string) (string, error) {
|
||||
content, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
@ -58,6 +62,7 @@ func ReadText(filename string) (string, error) {
|
||||
return strings.TrimSpace(string(content)), nil
|
||||
}
|
||||
|
||||
// ReadTextLines reads the text lines from given file.
|
||||
func ReadTextLines(filename string, opts ...TextReadOption) ([]string, error) {
|
||||
var readOpts textReadOptions
|
||||
for _, opt := range opts {
|
||||
@ -90,12 +95,14 @@ func ReadTextLines(filename string, opts ...TextReadOption) ([]string, error) {
|
||||
return lines, scanner.Err()
|
||||
}
|
||||
|
||||
// WithoutBlank customizes the reading functions to ignore blank lines.
|
||||
func WithoutBlank() TextReadOption {
|
||||
return func(o *textReadOptions) {
|
||||
o.withoutBlanks = true
|
||||
}
|
||||
}
|
||||
|
||||
// OmitWithPrefix customizes the reading functions to ignore the lines with given leading prefix.
|
||||
func OmitWithPrefix(prefix string) TextReadOption {
|
||||
return func(o *textReadOptions) {
|
||||
o.omitPrefix = prefix
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
|
||||
const bufSize = 32 * 1024
|
||||
|
||||
// CountLines returns the number of lines in file.
|
||||
func CountLines(file string) (int, error) {
|
||||
f, err := os.Open(file)
|
||||
if err != nil {
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// A TextLineScanner is a scanner that can scan lines from given reader.
|
||||
type TextLineScanner struct {
|
||||
reader *bufio.Reader
|
||||
hasNext bool
|
||||
@ -13,6 +14,7 @@ type TextLineScanner struct {
|
||||
err error
|
||||
}
|
||||
|
||||
// NewTextLineScanner returns a TextLineScanner with given reader.
|
||||
func NewTextLineScanner(reader io.Reader) *TextLineScanner {
|
||||
return &TextLineScanner{
|
||||
reader: bufio.NewReader(reader),
|
||||
@ -20,6 +22,7 @@ func NewTextLineScanner(reader io.Reader) *TextLineScanner {
|
||||
}
|
||||
}
|
||||
|
||||
// Scan checks if scanner has more lines to read.
|
||||
func (scanner *TextLineScanner) Scan() bool {
|
||||
if !scanner.hasNext {
|
||||
return false
|
||||
@ -37,6 +40,7 @@ func (scanner *TextLineScanner) Scan() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// Line returns the next available line.
|
||||
func (scanner *TextLineScanner) Line() (string, error) {
|
||||
return scanner.line, scanner.err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user