From 58537fc5192b96339cabc06e174769dd0a270705 Mon Sep 17 00:00:00 2001 From: KenjiTakahashi Date: Sun, 2 Nov 2014 01:13:40 +0100 Subject: [PATCH] readLinesOffsetN helper for reading only specific part of the file --- common.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/common.go b/common.go index a9d3112..b4523d0 100644 --- a/common.go +++ b/common.go @@ -17,8 +17,18 @@ import ( var NotImplementedError = errors.New("not implemented yet") -// readLines read contents from file and split by new line. +// readLines reads contents from file and splits them by new line. +// A convenience wrapper to readLinesOffsetN(filename, 0, -1). func readLines(filename string) ([]string, error) { + return readLinesOffsetN(filename, 0, -1) +} + +// readLines reads contents from file and splits them by new line. +// The offset tells at which line number to start. +// The count determines the number of lines to read (starting from offset): +// n >= 0: at most n lines +// n < 0: whole file +func readLinesOffsetN(filename string, offset uint, n int) ([]string, error) { f, err := os.Open(filename) if err != nil { return []string{""}, err @@ -28,10 +38,15 @@ func readLines(filename string) ([]string, error) { var ret []string r := bufio.NewReader(f) - line, err := r.ReadString('\n') - for err == nil { + for i := 0; i < n+int(offset) || n < 0; i++ { + line, err := r.ReadString('\n') + if err != nil { + break + } + if i < int(offset) { + continue + } ret = append(ret, strings.Trim(line, "\n")) - line, err = r.ReadString('\n') } return ret, nil