第一种方式 通过 file.WriteString 函数写入
package main
import "os"
func main() {
file, err := os.OpenFile("./t.txt", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0777)
defer file.Close()
if err == nil {
println("开始写入")
file.WriteString("hi..\r\n")
file.WriteString("golang..\r\n")
}
}
通过 bufio 来写入数据
package main
import (
"bufio"
"os"
)
func main() {
file, err := os.OpenFile("./t.txt", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0777)
defer file.Close()
if err == nil {
println("开始写入")
writer := bufio.NewWriter(file)
writer.WriteString("hi.......\r\n")
writer.Flush()
}
}
方式三 通过ioutil
package main
import (
"io/ioutil"
)
func main() {
var content string = "hi ... ioutil...."
ioutil.WriteFile("a.txt", []byte(content), 0777)
}