mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 02:16:48 +08:00
update book code
This commit is contained in:
37
eBook/exercises/chapter_12/remove_3till5char.go
Executable file
37
eBook/exercises/chapter_12/remove_3till5char.go
Executable file
@@ -0,0 +1,37 @@
|
||||
// remove_first6char.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"bufio"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
inputFile, _ := os.Open("goprogram")
|
||||
outputFile, _ := os.OpenFile("goprogramT", os.O_WRONLY|os.O_CREATE, 0666)
|
||||
defer inputFile.Close()
|
||||
defer outputFile.Close()
|
||||
inputReader := bufio.NewReader(inputFile)
|
||||
outputWriter := bufio.NewWriter(outputFile)
|
||||
for {
|
||||
// inputString, readerError := inputReader.ReadString('\n')
|
||||
inputString, _, readerError := inputReader.ReadLine()
|
||||
if readerError == io.EOF {
|
||||
fmt.Println("EOF")
|
||||
break
|
||||
}
|
||||
//fmt.Printf("The input was: --%s--", inputString)
|
||||
outputString := string([]byte(inputString)[2:5]) + "\r\n"
|
||||
//fmt.Printf("The output was: --%s--", outputString)
|
||||
_, err := outputWriter.WriteString(outputString)
|
||||
//fmt.Printf("Number of bytes written %d\n", n)
|
||||
if (err != nil) {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
outputWriter.Flush()
|
||||
fmt.Println("Conversion done")
|
||||
}
|
Reference in New Issue
Block a user