Golang file Operations

Source: Internet
Author: User

 
   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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.