Created
September 9, 2021 15:13
-
-
Save anyu/faac61c80fd8d57c7c9f74cf4053671c to your computer and use it in GitHub Desktop.
Basic file IO
This file contains 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" | |
"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