mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 06:36:43 +08:00
update book code
This commit is contained in:
34
eBook/examples/chapter_12/fileinput.go
Normal file
34
eBook/examples/chapter_12/fileinput.go
Normal file
@@ -0,0 +1,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')
|
||||
if readerError == io.EOF {
|
||||
return // error or EOF
|
||||
}
|
||||
fmt.Printf("The input was: %s", inputString)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user