Skip to content

Instantly share code, notes, and snippets.

@anyu
Created September 9, 2021 15:13
Show Gist options
  • Save anyu/faac61c80fd8d57c7c9f74cf4053671c to your computer and use it in GitHub Desktop.
Save anyu/faac61c80fd8d57c7c9f74cf4053671c to your computer and use it in GitHub Desktop.
Basic file IO
package main
import (
"bufio"
"fmt"
"log"
"os"
)
func main() {
f, err := os.Open("sample.txt")
if err != nil {
log.Fatal(err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
fmt.Printf("Line: %s\n", scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment