我正在尝试从键盘获取输入,然后将其存储在文本文件中,但我对如何实际操作有点困惑。
我目前的代码如下:
// reads the file txt.txt
bs, err := ioutil.ReadFile("text.txt")
if err != nil {
panic(err)
}
// Prints out content
textInFile := string(bs)
fmt.Println(textInFile)
// Standard input from keyboard
var userInput string
fmt.Scanln(&userInput)
//Now I want to write input back to file text.txt
//func WriteFile(filename string, data []byte, perm os.FileMode) error
inputData := make([]byte, len(userInput))
err := ioutil.WriteFile("text.txt", inputData, )
“os”和“io”包中有很多功能。我很困惑我实际上应该使用哪一个来达到这个目的。
我也对 WriteFile 函数中的第三个参数应该是什么感到困惑。在文档中说“perm os.FileMode”类型,但由于我是编程新手,所以我有点无能为力。
有人对如何处理有任何提示吗?在此先感谢,玛丽