Skip to content

Instantly share code, notes, and snippets.

@panxue
Last active May 6, 2019 06:19
Show Gist options
  • Select an option

  • Save panxue/1a0a504ab663440a4544353b6b1c3c16 to your computer and use it in GitHub Desktop.

Select an option

Save panxue/1a0a504ab663440a4544353b6b1c3c16 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"io"
"os"
"bytes"
"fmt"
)
func readLines(path string) (lines [] string, err error) {
var (
file *os.File
part [] byte
prefix bool
)
if file, err = os.Open(path); err != nil {
return
}
reader := bufio.NewReader(file)
buffer := bytes.NewBuffer(make([]byte, 1024))
for {
if part, prefix, err = reader.ReadLine(); err != nil {
break
}
buffer.Write(part)
if !prefix {
lines = append(lines, buffer.String())
buffer.Reset()
}
}
if err == io.EOF {
err = nil
}
return
}
func main() {
lines, err := readLines("./demo.log")
for _, line := range lines {
fmt.Println(line);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment