mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-23 09:00:20 +08:00
fix golint issues in core/filex (#485)
This commit is contained in:
parent
802549ac7c
commit
c376ffc351
@ -7,6 +7,7 @@ import (
|
||||
|
||||
const bufSize = 1024
|
||||
|
||||
// FirstLine returns the first line of the file.
|
||||
func FirstLine(filename string) (string, error) {
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
@ -17,6 +18,7 @@ func FirstLine(filename string) (string, error) {
|
||||
return firstLine(file)
|
||||
}
|
||||
|
||||
// LastLine returns the last line of the file.
|
||||
func LastLine(filename string) (string, error) {
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
@ -69,11 +71,11 @@ func lastLine(filename string, file *os.File) (string, error) {
|
||||
|
||||
if buf[n-1] == '\n' {
|
||||
buf = buf[:n-1]
|
||||
n -= 1
|
||||
n--
|
||||
} else {
|
||||
buf = buf[:n]
|
||||
}
|
||||
for n -= 1; n >= 0; n-- {
|
||||
for n--; n >= 0; n-- {
|
||||
if buf[n] == '\n' {
|
||||
return string(append(buf[n+1:], last...)), nil
|
||||
}
|
||||
|
@ -5,12 +5,15 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// OffsetRange represents a content block of a file.
|
||||
type OffsetRange struct {
|
||||
File string
|
||||
Start int64
|
||||
Stop int64
|
||||
}
|
||||
|
||||
// SplitLineChunks splits file into chunks.
|
||||
// The whole line are guaranteed to be split in the same chunk.
|
||||
func SplitLineChunks(filename string, chunks int) ([]OffsetRange, error) {
|
||||
info, err := os.Stat(filename)
|
||||
if err != nil {
|
||||
|
@ -3,8 +3,11 @@ package filex
|
||||
import "gopkg.in/cheggaaa/pb.v1"
|
||||
|
||||
type (
|
||||
// A Scanner is used to read lines.
|
||||
Scanner interface {
|
||||
// Scan checks if has remaining to read.
|
||||
Scan() bool
|
||||
// Text returns next line.
|
||||
Text() string
|
||||
}
|
||||
|
||||
@ -14,6 +17,7 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
// NewProgressScanner returns a Scanner with progress indicator.
|
||||
func NewProgressScanner(scanner Scanner, bar *pb.ProgressBar) Scanner {
|
||||
return &progressScanner{
|
||||
Scanner: scanner,
|
||||
|
@ -5,12 +5,14 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// A RangeReader is used to read a range of content from a file.
|
||||
type RangeReader struct {
|
||||
file *os.File
|
||||
start int64
|
||||
stop int64
|
||||
}
|
||||
|
||||
// NewRangeReader returns a RangeReader, which will read the range of content from file.
|
||||
func NewRangeReader(file *os.File, start, stop int64) *RangeReader {
|
||||
return &RangeReader{
|
||||
file: file,
|
||||
@ -19,6 +21,7 @@ func NewRangeReader(file *os.File, start, stop int64) *RangeReader {
|
||||
}
|
||||
}
|
||||
|
||||
// Read reads the range of content into p.
|
||||
func (rr *RangeReader) Read(p []byte) (n int, err error) {
|
||||
stat, err := rr.file.Stat()
|
||||
if err != nil {
|
||||
|
@ -341,39 +341,38 @@ func TestRedis_GetBit(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestRedis_BitCount(t *testing.T) {
|
||||
func TestRedis_BitCount(t *testing.T) {
|
||||
runOnRedis(t, func(client *Redis) {
|
||||
for i := 0; i < 11; i++{
|
||||
for i := 0; i < 11; i++ {
|
||||
err := client.SetBit("key", int64(i), 1)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
_, err := NewRedis(client.Addr,"").BitCount("key",0,-1)
|
||||
_, err := NewRedis(client.Addr, "").BitCount("key", 0, -1)
|
||||
assert.NotNil(t, err)
|
||||
val, err := client.BitCount("key", 0,-1)
|
||||
val, err := client.BitCount("key", 0, -1)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, int64(11), val)
|
||||
|
||||
val, err = client.BitCount("key", 0,0)
|
||||
val, err = client.BitCount("key", 0, 0)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, int64(8), val)
|
||||
|
||||
val, err = client.BitCount("key", 1,1)
|
||||
val, err = client.BitCount("key", 1, 1)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, int64(3), val)
|
||||
|
||||
val, err = client.BitCount("key", 0,1)
|
||||
val, err = client.BitCount("key", 0, 1)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, int64(11), val)
|
||||
|
||||
val, err = client.BitCount("key", 2,2)
|
||||
val, err = client.BitCount("key", 2, 2)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, int64(0), val)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
func TestRedis_Persist(t *testing.T) {
|
||||
runOnRedis(t, func(client *Redis) {
|
||||
_, err := NewRedis(client.Addr, "").Persist("key")
|
||||
|
Loading…
Reference in New Issue
Block a user