fix golint issues in core/filex (#485)

master
Kevin Wan 4 years ago committed by GitHub
parent 802549ac7c
commit c376ffc351
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -7,6 +7,7 @@ import (
const bufSize = 1024 const bufSize = 1024
// FirstLine returns the first line of the file.
func FirstLine(filename string) (string, error) { func FirstLine(filename string) (string, error) {
file, err := os.Open(filename) file, err := os.Open(filename)
if err != nil { if err != nil {
@ -17,6 +18,7 @@ func FirstLine(filename string) (string, error) {
return firstLine(file) return firstLine(file)
} }
// LastLine returns the last line of the file.
func LastLine(filename string) (string, error) { func LastLine(filename string) (string, error) {
file, err := os.Open(filename) file, err := os.Open(filename)
if err != nil { if err != nil {
@ -69,11 +71,11 @@ func lastLine(filename string, file *os.File) (string, error) {
if buf[n-1] == '\n' { if buf[n-1] == '\n' {
buf = buf[:n-1] buf = buf[:n-1]
n -= 1 n--
} else { } else {
buf = buf[:n] buf = buf[:n]
} }
for n -= 1; n >= 0; n-- { for n--; n >= 0; n-- {
if buf[n] == '\n' { if buf[n] == '\n' {
return string(append(buf[n+1:], last...)), nil return string(append(buf[n+1:], last...)), nil
} }

@ -5,12 +5,15 @@ import (
"os" "os"
) )
// OffsetRange represents a content block of a file.
type OffsetRange struct { type OffsetRange struct {
File string File string
Start int64 Start int64
Stop 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) { func SplitLineChunks(filename string, chunks int) ([]OffsetRange, error) {
info, err := os.Stat(filename) info, err := os.Stat(filename)
if err != nil { if err != nil {

@ -3,8 +3,11 @@ package filex
import "gopkg.in/cheggaaa/pb.v1" import "gopkg.in/cheggaaa/pb.v1"
type ( type (
// A Scanner is used to read lines.
Scanner interface { Scanner interface {
// Scan checks if has remaining to read.
Scan() bool Scan() bool
// Text returns next line.
Text() string Text() string
} }
@ -14,6 +17,7 @@ type (
} }
) )
// NewProgressScanner returns a Scanner with progress indicator.
func NewProgressScanner(scanner Scanner, bar *pb.ProgressBar) Scanner { func NewProgressScanner(scanner Scanner, bar *pb.ProgressBar) Scanner {
return &progressScanner{ return &progressScanner{
Scanner: scanner, Scanner: scanner,

@ -5,12 +5,14 @@ import (
"os" "os"
) )
// A RangeReader is used to read a range of content from a file.
type RangeReader struct { type RangeReader struct {
file *os.File file *os.File
start int64 start int64
stop 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 { func NewRangeReader(file *os.File, start, stop int64) *RangeReader {
return &RangeReader{ return &RangeReader{
file: file, 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) { func (rr *RangeReader) Read(p []byte) (n int, err error) {
stat, err := rr.file.Stat() stat, err := rr.file.Stat()
if err != nil { if err != nil {

@ -373,7 +373,6 @@ func TestRedis_BitCount(t *testing.T) {
}) })
} }
func TestRedis_Persist(t *testing.T) { func TestRedis_Persist(t *testing.T) {
runOnRedis(t, func(client *Redis) { runOnRedis(t, func(client *Redis) {
_, err := NewRedis(client.Addr, "").Persist("key") _, err := NewRedis(client.Addr, "").Persist("key")

Loading…
Cancel
Save