Skip to content

Instantly share code, notes, and snippets.

@OffXec
Created April 10, 2021 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OffXec/2123b8c1c3ca53e3242b0df153694b57 to your computer and use it in GitHub Desktop.
Save OffXec/2123b8c1c3ca53e3242b0df153694b57 to your computer and use it in GitHub Desktop.
GoLang doodles
func readFileWithReadString(fn string) (err error) {
fmt.Println("readFileWithReadString")
file, err := os.Open(fn)
if err != nil {
return err
}
defer file.Close()
// Start reading from the file with a reader.
reader := bufio.NewReader(file)
var line string
for {
line, err = reader.ReadString('\n')
if err != nil && err != io.EOF {
break
}
// Process the line here.
fmt.Printf(" > Read %d characters\n", len(line))
fmt.Printf(" > > %s\n", limitLength(line, 50))
if err != nil {
break
}
}
if err != io.EOF {
fmt.Printf(" > Failed with error: %v\n", err)
return err
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment