forked from unknwon/the-way-to-go_ZH_CN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileinput.go
More file actions
34 lines (29 loc) · 685 Bytes
/
fileinput.go
File metadata and controls
34 lines (29 loc) · 685 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package main
import (
"bufio"
"fmt"
"io"
"os"
)
func main() {
// var inputFile *os.File
// var inputError, readerError os.Error
// var inputReader *bufio.Reader
// var inputString string
inputFile, inputError := os.Open("input.dat")
if inputError != nil {
fmt.Printf("An error occurred on opening the inputfile\n" +
"Does the file exist?\n" +
"Have you got acces to it?\n")
return // exit the function on error
}
defer inputFile.Close()
inputReader := bufio.NewReader(inputFile)
for {
inputString, readerError := inputReader.ReadString('\n')
fmt.Printf("The input was: %s", inputString)
if readerError == io.EOF {
return // error or EOF
}
}
}