我想从键盘输入数据并将其存储在文本文件中,但是我对如何实际操作有些困惑。
目前我的代码如下:
// 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"类型,但由于我刚开始学习编程和Go语言,我有点茫然。
有没有任何提示可以告诉我该怎么做呢? 谢谢, 玛丽