???
This commit is contained in:
parent
fc75f205ee
commit
da432bc1db
35
file_util.go
35
file_util.go
|
@ -14,24 +14,30 @@ var (
|
||||||
type fileUtil struct{}
|
type fileUtil struct{}
|
||||||
|
|
||||||
// ReadByLines reads a file and calls the handler for each line.
|
// ReadByLines reads a file and calls the handler for each line.
|
||||||
func (fu fileUtil) ReadByLines(filePath string, handler func(line string)) error {
|
func (fu fileUtil) ReadByLines(filePath string, handler func(line string) error) error {
|
||||||
if f, err := os.Open(filePath); err == nil {
|
var f *os.File
|
||||||
|
var err error
|
||||||
|
if f, err = os.Open(filePath); err == nil {
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
var line string
|
||||||
scanner := bufio.NewScanner(f)
|
scanner := bufio.NewScanner(f)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Text()
|
line = scanner.Text()
|
||||||
handler(line)
|
err = handler(line)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
return nil
|
return err
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadByChunks reads a file in `chunkSize` pieces, dispatched to the handler.
|
// ReadByChunks reads a file in `chunkSize` pieces, dispatched to the handler.
|
||||||
func (fu fileUtil) ReadByChunks(filePath string, chunkSize int, handler func(line []byte)) error {
|
func (fu fileUtil) ReadByChunks(filePath string, chunkSize int, handler func(line []byte) error) error {
|
||||||
if f, err := os.Open(filePath); err == nil {
|
var f *os.File
|
||||||
|
var err error
|
||||||
|
if f, err = os.Open(filePath); err == nil {
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
chunk := make([]byte, chunkSize)
|
chunk := make([]byte, chunkSize)
|
||||||
|
@ -41,10 +47,11 @@ func (fu fileUtil) ReadByChunks(filePath string, chunkSize int, handler func(lin
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
readData := chunk[:readBytes]
|
readData := chunk[:readBytes]
|
||||||
handler(readData)
|
err = handler(readData)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
return nil
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,21 +10,33 @@ type ValueFormatter func(v interface{}) string
|
||||||
|
|
||||||
// TimeValueFormatter is a ValueFormatter for timestamps.
|
// TimeValueFormatter is a ValueFormatter for timestamps.
|
||||||
func TimeValueFormatter(v interface{}) string {
|
func TimeValueFormatter(v interface{}) string {
|
||||||
return TimeValueFormatterWithFormat(v, DefaultDateFormat)
|
return formatTime(v, DefaultDateFormat)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TimeHourValueFormatter is a ValueFormatter for timestamps.
|
// TimeHourValueFormatter is a ValueFormatter for timestamps.
|
||||||
func TimeHourValueFormatter(v interface{}) string {
|
func TimeHourValueFormatter(v interface{}) string {
|
||||||
return TimeValueFormatterWithFormat(v, DefaultDateHourFormat)
|
return formatTime(v, DefaultDateHourFormat)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TimeMinuteValueFormatter is a ValueFormatter for timestamps.
|
// TimeMinuteValueFormatter is a ValueFormatter for timestamps.
|
||||||
func TimeMinuteValueFormatter(v interface{}) string {
|
func TimeMinuteValueFormatter(v interface{}) string {
|
||||||
return TimeValueFormatterWithFormat(v, DefaultDateMinuteFormat)
|
return formatTime(v, DefaultDateMinuteFormat)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TimeDateValueFormatter is a ValueFormatter for timestamps.
|
||||||
|
func TimeDateValueFormatter(v interface{}) string {
|
||||||
|
return formatTime(v, "2006-01-02")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TimeValueFormatterWithFormat returns a time formatter with a given format.
|
||||||
|
func TimeValueFormatterWithFormat(format string) ValueFormatter {
|
||||||
|
return func(v interface{}) string {
|
||||||
|
return formatTime(v, format)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TimeValueFormatterWithFormat is a ValueFormatter for timestamps with a given format.
|
// TimeValueFormatterWithFormat is a ValueFormatter for timestamps with a given format.
|
||||||
func TimeValueFormatterWithFormat(v interface{}, dateFormat string) string {
|
func formatTime(v interface{}, dateFormat string) string {
|
||||||
if typed, isTyped := v.(time.Time); isTyped {
|
if typed, isTyped := v.(time.Time); isTyped {
|
||||||
return typed.Format(dateFormat)
|
return typed.Format(dateFormat)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user