Go读取文件到字符串 / 写字符串到文件

读取文件

fileData, err := ioutil.ReadFile(filePath)
if err != nil {
    panic(err)
}
fileStr := string(fileData)

写文件

f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
  panic(err)
}
defer f.Close()

io.WriteString(f, "xxxx")

 

Leave a Reply

Your email address will not be published. Required fields are marked *