Package Main
Import (
"Bytes"
"FMT"
"IO"
"OS"
)
var file *os. File = &os. file{}
var filebuf *bytes. Buffer = bytes. Newbufferstring ("")
Func Main () {
var err error
File, err = OS. OpenFile ("TestData.txt", OS. O_create|os. O_append|os. O_rdwr|os. O_trunc, OS. Modeperm|os. Modetemporary)
Os. O_trunc empty files that already exist
If err! = Nil {
Panic (ERR)
}
Filebuf.writestring (FMT. Sprintf ("%d", 1000))
Filebuf.writestring ("\ n")
Filebuf.writestring ("Hello World")
Io. WriteString (file, filebuf.string ())
Defer file. Close ()
}
Open all kinds of existing and nonexistent files in various ways, how to do it depends on flag and perm
Flag selectable value (mask)
o_rdonly int = Syscall. O_rdonly//Read-only
o_wronly int = Syscall. O_wronly//write only
o_rdwr int = Syscall. O_RDWR//Read/write
o_append int = Syscall. O_append//Append at end of file, open cursor at end of file
o_create int = Syscall. O_creat//If it does not exist, create
o_excl int = Syscall. O_EXCL//With o_create to form a new file feature that requires that the file must not exist
O_sync int = Syscall. O_sync//Sync mode open, no cache, so write content directly to the hard disk, the system power down the file content has certain guarantee
O_trunc int = Syscall. O_trunc//Open and empty files
FileMode is Unix's permission bit: reference: http://www.ibm.com/developerworks/cn/aix/library/au-speakingunix4
Golang file Operations