2 回答
TA贡献1895条经验 获得超3个赞
只要文件名之间没有空格,您也可以使用 fmt.Scan() 接受用户的文本输入(换行符算作空格)
package main
import (
"bufio"
"fmt"
"log"
"os"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
var puzzle []string
var fName string
fmt.Println("Please enter file name: ")
_, err := fmt.Scan(&fName)
if err != nil {
log.Println(err)
return
}
fmt.Println("Confirm file:" + fName)
readFile, err := os.Open(fName)
if err != nil {
log.Fatalf("Failed to open file: %s", err)
}
fileScanner := bufio.NewScanner(readFile)
fileScanner.Split(bufio.ScanLines)
for fileScanner.Scan() {
puzzle = append(puzzle, fileScanner.Text())
}
readFile.Close()
for _, eachLine := range puzzle {
fmt.Println(eachLine)
}
}
- 2 回答
- 0 关注
- 160 浏览
添加回答
举报