Last active
May 6, 2019 06:19
-
-
Save panxue/1a0a504ab663440a4544353b6b1c3c16 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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