如果文件不存在就創(chuàng)建文件锈玉,如果存在就續(xù)寫文件
package wrigeFile
import (
"fmt"
"github.com/golang/glog"
"os"
)
//注意OpenFile這個(gè)方法的第二個(gè)參數(shù)吕漂,一定要加上RDWR宝磨,可讀可寫的權(quán)限
func writeFile(s string) {
file1, err := os.OpenFile("test.txt", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0660)
if err != nil {
panic(err)
}
defer file1.Close()
if err != nil {
fmt.Println(file1, err)
return
}
glog.Infoln(s)
file1.WriteString(s)
file1.Sync()
}
os包相關(guān)資料:
http://blog.csdn.net/chenbaoke/article/details/42494851
http://blog.chinaunix.net/uid-24774106-id-3993609.html
http://www.widuu.com/archives/01/921.html