feat: add WriteBytesToFile and WriteStringToFile

This commit is contained in:
dudaodong committed 2023-05-31 16:56:18 +08:00
1 parent 2c71b6375c
commit 75ed359084
3 files changed
+153

No files matched your search

+56
View File
@@ -285,3 +285,59 @@ func ExampleReadCsvFile() {
// [[Bob 12 male] [Duke 14 male] [Lucy 16 female]]
// <nil>
}
func ExampleWriteStringToFile() {
filepath := "./test.txt"
file, err := os.Create(filepath)
if err != nil {
return
}
defer file.Close()
err = WriteStringToFile(filepath, "hello", true)
if err != nil {
return
}
content, err := ReadFileToString(filepath)
if err != nil {
return
}
os.Remove(filepath)
fmt.Println(content)
// Output:
// hello
}
func ExampleWriteBytesToFile() {
filepath := "./bytes.txt"
file, err := os.Create(filepath)
if err != nil {
return
}
defer file.Close()
err = WriteBytesToFile(filepath, []byte("hello"))
if err != nil {
return
}
content, err := ReadFileToString(filepath)
if err != nil {
return
}
os.Remove(filepath)
fmt.Println(content)
// Output:
// hello
}