golang 逐行写文件
In the Go programming language, How to process a file line by line?
在Go 编程语言中,如何逐行处理文件?
An equivalent piece of Bash code could be
一段等效的Bash代码可能是
while read line ; do
echo $line
done < ./input.txt
bufioScan()
bufioScan()
// open the file filepath
f := os.Open(filepath)
// create a scanner
fs := bufio.NewScanner(f)
// scan filehttps://golang.org/pkg/bufio/#Scanner.Scan
for fs.Scan() {
txt := fs.Text()
// do anything with txt
}
If you are reading line by line from STDIN:
如果您正在从STDIN逐行阅读:
fs := bufio.NewScanner(os.Stdin)
for fs.Scan() {
txt := fs.Text()
// process txt
}
golang 逐行写文件